|
列表相加的方法
#-----------两个纯数字的列表一一对应相加-----------
# 创建两个原始列表
temp_list1 = [1, 2, 3, 4]
temp_list2 = [5, 6, 2, 3]
# 创建新列表 temp_list_new
temp_list_new = []
# 将两个原始列表的元素之和添加进 temp_list_new
for i in range(len(temp_list1)):
temp_list_new.append(temp_list1 + temp_list2)
# 查看新列表 temp_list_new
print(temp_list_new)
#-----------两个数字字符串的列表一一对应相加-----------
# 创建两个原始列表
temp_list1 = ['1', '2', '3', '4']
temp_list2 = ['5', '6', '2', '3']
# 创建新列表 temp_list_new
temp_list_new = []
# 将两个原始列表的元素之和添加进 temp_list_new
for i in range(len(temp_list1)):
temp_list_new.append(temp_list1 + temp_list2)
# 查看新列表 temp_list_new
print(temp_list_new)
#-----------两个字母和数字字符串的列表一一对应相加-----------
# 创建两个原始列表
temp_list1 = ['a', 'b', 'c', 'd']
temp_list2 = ['5', '6', '2', '3']
# 创建新列表 temp_list_new
temp_list_new = []
# 将两个原始列表的元素之和添加进 temp_list_new
for i in range(len(temp_list1)):
temp_list_new.append(temp_list1 + temp_list2)
# 查看新列表 temp_list_new
print(temp_list_new)
#-----------两个字母和数字字符串的列表相加-----------
# 创建两个原始列表
temp_list1 = ['a', 'b', 'c', 'd']
temp_list2 = ['5', '6', '2', '3']
temp_list_new=temp_list1+temp_list2
print(temp_list_new)
结果:
[6, 8, 5, 7]
['15', '26', '32', '43']
['a5', 'b6', 'c2', 'd3']
['a', 'b', 'c', 'd', '5', '6', '2', '3']
|
上一篇:python 求多个列表的交集、并集、差集下一篇:列表和字典都可以使用直接赋值的方法修改内部元素
|