百万年薪python之路 -- 基础数据类型的补充练习

时间:2022-02-02 18:11:24

1.看代码写结果

v1 = [1,2,3,4,5]
v2 = [v1,v1,v1]
v1.append(6)
print(v1)
print(v2)
[1,2,3,4,5,6]
[[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]

2.看代码写结果

v1 = [1,2,3,4,5]
v2 = [v1,v1,v1]
v2[1][0] = 111
v2[2][0] = 222
print(v1)
print(v2)
[222,2,3,4,5]
[[222,2,3,4,5],[222,2,3,4,5],[222,2,3,4,5]]

3.看代码写结果,并解释每一步的流程。

v1 = [1,2,3,4,5,6,7,8,9]
v2 = {}
for item in v1:
    if item < 6:
        continue
    if 'k1' in v2:
        v2['k1'].append(item)
    else:
        v2['k1'] = [item ]
print(v2)
{'k1':[6,7,8,9]}

4.简述赋值和深浅拷贝?

赋值是让变量都指向一块内存地址
浅拷贝:只会拷贝第一层. 第二层的内容不会拷贝. 所以被称为浅拷贝
深拷贝:本质是不可变数据类型共用一个,可变数据类型另开辟一块空间(复制一份) 不会产生一个改变另一个跟着改变的问题

5.看代码写结果

import copy
v1 = "alex"
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)
print(v1 is v2)
print(v1 is v3)
True
True

6.看代码写结果

import copy
v1 = [1,2,3,4,5]
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)
print(v1 is v2)
print(v1 is v3)
False
False

7.看代码写结果

import copy
v1 = [1,2,3,4,5]
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)

print(v1[0] is v2[0])
print(v1[0] is v3[0])
print(v2[0] is v3[0])
True
True
True

8.看代码写结果

import copy

v1 = [1,2,3,4,[11,22]]
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)

print(v1[-1] is v2[-1])
print(v1[-1] is v3[-1])
print(v2[-1] is v3[-1])
True
False
False

9.看代码写结果

import copy

v1 = [1,2,3,{"name":'太白',"numbers":[7,77,88]},4,5]
v2 = copy.copy(v1)

print(v1 is v2)

print(v1[0] is v2[0])
print(v1[3] is v2[3])

print(v1[3]['name'] is v2[3]['name'])
print(v1[3]['numbers'] is v2[3]['numbers'])
print(v1[3]['numbers'][1] is v2[3]['numbers'][1])
False
True
True
True
True
True

10.看代码写结果

import copy
v1 = [1,2,3,{"name":'太白',"numbers":[7,77,88]},4,5]
v2 = copy.deepcopy(v1)
print(v1 is v2)
print(v1[0] is v2[0])
print(v1[3] is v2[3])

print(v1[3]['name'] is v2[3]['name'])
print(v1[3]['numbers'] is v2[3]['numbers'])
print(v1[3]['numbers'][1] is v2[3]['numbers'][1])
False
True
False
True
False
True

11.请说出下面a,b,c三个变量的数据类型。
a = ('太白金星')
b = (1,)
c = ({'name': 'barry'})

字符串
元组
字典

12.按照需求为列表排序:

l1 = [1, 3, 6, 7, 9, 8, 5, 4, 2]
# 从大到小排序
# 从小到大排序
# 反转l1列表
l1 = [1, 3, 6, 7, 9, 8, 5, 4, 2]
l1.sort(reverse=True)
print(l1)
l1 = [1, 3, 6, 7, 9, 8, 5, 4, 2]
l1.sort()
print(l1)
l1 = [1, 3, 6, 7, 9, 8, 5, 4, 2]
l1.reverse()
print(l1)

13.利用python代码构建一个这样的列表(升级题):

[['_','_','_'],['_','_','_'],['_','_','_']]
lst = [[]]
new_lst = lst * 3
for i in range(3):
    new_lst[0].append("_")
print(new_lst)

14.看代码写结果:

l1 = [1,2,]
l1 += [3,4]
print(l1)
[1,2,3,4]

15.看代码写结果:

dic = dict.fromkeys('abc',[])
dic['a'].append(666)
dic['b'].append(111)
print(dic)
{"a":[666,111],"b":[666,111],"c":[666,111]}

16.l1 = [11, 22, 33, 44, 55],请把索引为奇数对应的元素删除(不能一个一个删除)

l1 = [11, 22, 33, 44, 55]
lst = l1.copy()
for i in range(len(l1)):
    if i % 2 == 1:
        del lst[i]
print(lst)

dic = {'k1':'太白','k2':'barry','k3': '白白', 'age': 18} 请将字典中所有键带k元素的键值对删除.

dic = {'k1':'太白','k2':'barry','k3': '白白', 'age': 18}
new_dic = dic.copy()
for k in new_dic.keys():
    # print(k)
    if "k" in k:
        del dic[k]
print(dic)

17.完成下列需求:
s1 = '太白金星'
将s1转换成utf-8的bytes类型。

s1 = '太白金星'
s1 = s1.encode("utf-8")
print(s1)

将s1转化成gbk的bytes类型。
b = b'\xe5\xae\x9d\xe5\x85\x83\xe6\x9c\x80\xe5\xb8\x85'
b为utf-8的bytes类型,请转换成gbk的bytes类型。

b = b'\xe5\xae\x9d\xe5\x85\x83\xe6\x9c\x80\xe5\xb8\x85'
b = b.decode("UTF-8")
b = b.encode("gbk")
print(b)
  1. 用户输入一个数字,判断一个数是否是水仙花数。
    水仙花数是一个三位数, 三位数的每一位的三次方的和还等于这个数. 那这个数就是一个水仙花数,
    例如: 153 = 1 ** 3 + 5 ** 3 + 3 ** 3

    inp = int(input("请输入数字(三位数):"))
    if 99 < inp < 1000:
        a,b,c = inp // 100,inp // 10 % 10,inp % 10
        if a ** 3 + b ** 3 + c ** 3 == inp:
            print("是水仙花数")
        else:
            print("不是水仙花数")
    else:
        print("请输入三位数")
    
  2. 把列表中所有姓周的⼈的信息删掉(此题有坑, 请慎重):
    lst = ['周⽼⼆', '周星星', '麻花藤', '周扒⽪']
    结果: lst = ['麻花藤']

lst = ['周⽼⼆', '周星星', '麻花藤', '周扒⽪']
new_lst = lst.copy()
for i in new_lst:
    if i[0] == "周":
        lst.remove(i)
print(lst)

20.车牌区域划分, 现给出以下车牌. 根据车牌的信息, 分析出各省的车牌持有量. (选做题)
cars = ['鲁A32444','鲁B12333','京B8989M','⿊C49678','⿊C46555','沪 B25041']
locals = {'沪':'上海', '⿊':'⿊⻰江', '鲁':'⼭东', '鄂':'湖北', '湘':'湖南'}
结果: {'⿊⻰江':2, '⼭东': 2, '上海': 1}

cars = ['鲁A32444','鲁B12333','京B8989M','⿊C49678','⿊C46555','沪 B25041']
locals = {'沪':'上海', '⿊':'⿊⻰江', '鲁':'⼭东', '鄂':'湖北', '湘':'湖南'}
dic = {}
count = 0
for i in cars:
    for j in locals.keys():
        if i[0] == j:
            if locals[j] in dic.keys():
                count += 1
                dic[locals[j]] = count
            else:
                dic[locals[j]] = 0
print(dic)