[英]How to interleave these two lists into one list?
a = ['a', 'b', 'c']
b = [10, 20, 30]
output should be like [[a:10], [b:20], [c:30]] output 应该像 [[a:10], [b:20], [c:30]]
I do know how to use the zip to interweave two lists我知道如何使用 zip 来交织两个列表
l = []
for x,y in zip(a,b):
l.append([x,y])
And the output is: [['a', 10], ['b', 20], ['c', 30]] instead of [[a:10], [b:20], [c:30]]而 output 是: [['a', 10], ['b', 20], ['c', 30]] 而不是 [[a:10], [b:20], [c:30] ]
How should I make like this with ':' Thanks我应该如何用':'做这样的谢谢
Assuming that you mean to create a list of singleton dicts, you can zip
the two lists, convert sequence of value pairs to singletons with another zip
, and map the resulting sequence to the dict
constructor:假设您要创建一个包含 singleton 个字典的列表,您可以zip
这两个列表,使用另一个zip
将值对序列转换为单例,并将结果序列 map 转换为dict
构造函数:
list(map(dict, zip(zip(a, b))))
Or use a list comprehension:或者使用列表理解:
[{i: j} for i, j in zip(a, b)]
Both return:两者都返回:
[{'a': 10}, {'b': 20}, {'c': 30}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.