python - 在python中以不等间隔加入不同长度的列表?
问题描述
我正在尝试加入两个具有不同数据长度的列表。
list1 = [a,b,c,d,e,f]
list2 = [1,2,3,4,5,6,7,8,9,10,11,12]
我想要的输出是这样的:
[a12, b34, c56, d78, e910, f1112
我的第一个想法是在不同的时间间隔做一些像增量 i
for i in printlist:
for i in stats:
print(printlist[i] + stats[i] + stats[i+1])
但这会吐出来TypeError: list indices must be integers or slices, not str
,我认为这不起作用,因为我会为两个列表错误地增加。
任何帮助,将不胜感激
解决方案
尝试这个,
list1 = ["a","b","c","d","e","f"]
list2 = [1,2,3,4,5,6,7,8,9,10,11,12]
list3 = [str(i)+str(j) for i,j in zip(list2[::2], list2[1::2])]
["".join(list(x)) for x in zip(list1, list3)]
输出
['a12', 'b34', 'c56', 'd78', 'e910', 'f1112']
推荐阅读
- ruby - 使用 GitHub 存储库中的更改更新本地存储库?不是“git pull origin master”
- python - pandas groupby 两个数据框不合并
- javascript - Ant Design - 以编程方式从 Menu.Item 取消选择/删除活动状态
- android - 如何使用 remove_widget() 函数?
- azure - 我能否创建具有 5 个知识库和基本定价层的 Azure QnA Maker?
- jira - Jira JQL - 显示所有子任务
- css - 匹配动画光标的颜色和内部跨度
- laravel - SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列'标题'(SQL:选择计数(*)
- jquery - jQuery .trigger() 未在 Internet Explorer 上触发
- hive - 动态分区插入 Hive