21番茄网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 21番茄网
查看: 871|回复: 0

列表相加的方法

[复制链接]

650

主题

805

帖子

2706

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2706
发表于 2021-2-22 10:30:25 | 显示全部楼层 |阅读模式
列表相加的方法
#-----------两个纯数字的列表一一对应相加-----------
# 创建两个原始列表
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 求多个列表的交集、并集、差集
下一篇:列表和字典都可以使用直接赋值的方法修改内部元素
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|21番茄网 ( 粤ICP备10099428号 ) 奇远富

GMT+8, 2024-5-15 22:26 , Processed in 0.218406 second(s), 34 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表