[英]Python: zipping tuple of numbers and lists of strings
I've got a list of lists and a tuple. 我有一个列表列表和一个元组。 I'm trying to zip them into a list of lists with three "columns".
我正在尝试将它们压缩到带有三个“列”的列表中。 Here's what I have:
这是我所拥有的:
splitKeys=[['259', ' 46'], ['281', ' 71'], ['49', ' 13378']]
data=(2.2217806892532197, 1.1757204348143286, 0.55)
b=zip(splitKeys, data)
but, I get this: 但是,我得到这个:
[(['259', ' 46'], 2.2217806892532197), (['281', ' 71'], 1.1757204348143286),
(['49', '13378'], 0.55)]
when I really want this: 当我真的想要这个时:
[('259', ' 46', 2.2217806892532197), ('281', ' 71', 1.1757204348143286),
('49', '13378], 0.55)]
I've tried a few different methods, using both map
and lambda
, but am still getting the undesirable output I noted above. 我尝试了几种不同的方法,同时使用
map
和lambda
,但是仍然得到上面提到的不良输出。
Thoughts? 思考?
你要:
b = [(a, b, c) for (a, b), c in zip(splitKeys, data)]
You can temporarily unpack the lists before zipping with generator expressions: 您可以在生成器表达式压缩之前临时解压缩列表:
b = zip((k[0] for k in splitKeys), (k[1] for k in splitKeys), data)
Output: 输出:
b
Out[4]:
[('259', ' 46', 2.2217806892532197),
('281', ' 71', 1.1757204348143286),
('49', ' 13378', 0.55)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.