1、列表中使用“+”和append()函数的区别?速度如何?写出测试代码。
append(): 可添加任何对象,但无论是什么对象,统统算一个元素在列表末尾添加;原地修改列表,不会引起id的变化;
+ : 只能执行列表之间的操作;会拷贝成一个新对象,引起id的变化;
append()函数速度更快
>>> # append往后添加单独一个元素
>>> A = ["a", "b"]
>>> A.append(1)
>>> A
['a', 'b', 1]
>>> # append往后添加一整个列表
>>> A = ["a", "b"]
>>> A.append(["c", "d"])
>>> A
['a', 'b', ['c', 'd']]
>>> # append往后添加一个迭代对象
>>> A = ["a", "b"]
>>> A.append(range(5))
>>> A
['a', 'b', range(0, 5)]
>>> # +
>>> A = ["a", "b"]
>>> # + 往后添加单独一个元素
>>> B = 1
>>> A + B
Traceback (most recent call last):
File "", line 1, in
A + B
TypeError: can only concatenate list (not "int") to list
>>> # + 往后添加一整个列表
>>> B = ["c", "d"]
>>> A + B
['a', 'b', 'c', 'd']
>>> # + 往后添加一个迭代对象
>>> A + range(5)
Traceback (most recent call last):
File "", line 1, in
A + range(5)
TypeError: can only concatenate list (not "range") to list
>>> # 从id看操作对象的变化
>>> A = ["a", "b"]
>>> B = ["c", "d"]
>>> id(A)
1954148186952
>>> A.append(B)
>>> A
['a', 'b', ['c', 'd']]
>>> id(A)
1954148186952
>>> A = ["a", "b"]
>>> B = ["c", "d"]
>>> id(A)
1954150999432
>>> A = A + B
>>> A
['a', 'b', 'c', 'd']
>>> id(A)
1954151178120
>>>
比较速度代码:
import time
import random
start1 = time.time()
a1 = list()
for i in range(1, 10000):
a1 = a1 + [random.randint(0, 100)]
stop1 = time.time()
print("使用+耗用的时间:", stop1 - start1)
start2 = time.time()
a2 = list()
for j in range(1, 10000):
a2.append(random.randint(0, 100))
stop2 = time.time()
print("使用append()耗用的时间:", stop2 - start2)
print()
2、列表的删除函数有哪些?写代码将x = [3,2,1,2,3,3,3]中的3删除。
列表的删除函数有:
remove();del();pop()
# remove(),del(),pop()
# 列表名.remove()
# 第一种
x = [3, 2, 1, 2, 3, 3, 3]
x.remove(3)
x.remove(3)
x.remove(3)
x.remove(3)
print(x)
# 第二种
"""x = [3, 2, 1, 2, 3, 3, 3]x.remove(x[0])print(x)x.remove(x[3])print(x)x.remove(x[3])print(x)x.remove(x[3])print(x)"""
# del(列表名[索引])
"""x = [3, 2, 1, 2, 3, 3, 3]del(x[0])print(x)del(x[3])print(x)del(x[3])print(x)del(x[3])print(x)"""
# 列表名.pop()
"""x = [3, 2, 1, 2, 3, 3, 3]x.pop()print(x)x.pop()print(x)x.pop()print(x)x.pop(0)print(x)"""
3、使用列表推导式实现0、1、2任意两个元素的元组组合。
nums = [0, 1, 2]
new = [(a, b) for a in nums for b in nums if a != b]
print(new)
4、首先生成包含1000个随机字符的字符串,然后统计每个字符的出现次数。(使用字典)
import random
import string
# string.digits 数字0123456789
# string.punctuation 符号!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
# string.ascii_letters 26个英文字母(大小写)
x = string.digits + string.ascii_letters + string.punctuation
# 生成一个包含1000位随机字符的列表
y = [random.choice(x) for i in range(1000)]
# 将生成的列表y转化为字符串
y = "".join(y)
print("生成的1000个随机字符为:\n", y)
# 统计每个字符出现的次数,组成一个字典{'字符':次数, '字符':次数}
g = dict() # 创建一个空字典
for gf in y:
g[gf] = g.get(gf, 0) + 1
# 字典items()方法以列表返回可遍历的(键, 值)元组数组。
for i, j in g.items():
print(i, "出现的次数: ", j)
5、举例说明集合的交、并、差和对称差集的求法。(两种不同的方法,|、&、-、^和union()、intersection()、difference()、symmetric_difference())
# 创建两个集合
set1 = {"a", "b", "c", 1, 5}
set2 = {"a", 1, 2, 3}
print("集合set1=", set1)
print("集合set2=", set2)
print()
# 集合的合并:union()与|
print("集合set1或set2中包含的所有元素:")
set3 = set.union(set1, set2)
print("使用'|'得到的并集为: ", set1 | set2)
print("使用union()得到的并集为:", set3)
print()
# 集合的交集:intersection()与&
print("集合set1和set2中都包含了的元素:")
set4 = set.intersection(set1, set2)
print("使用'&'得到的交集为: ", set1 & set2)
print("使用intersection()得到的交集为:", set4)
print()
# 集合的差集:difference()与-
print("集合set1中包含而集合set2中不包含的元素:")
set5 = set.difference(set1, set2)
print("使用'-'得到的差集为: ", set1 - set2)
print("使用difference()得到的差集为:", set5)
print()
# 集合的对称差集:symmetric_difference()与^
print("不同时包含于set1和set2的元素:")
set6 = set.symmetric_difference(set1, set2)
print("使用'^'得到的对称差集为: ", set1 ^ set2)
print("使用symmetric_difference()得到的对称差集为:", set6)
求个赞=-=