Python中如何将字符串和列表结合起来,一个元组
问题描述:
我有一个字符串和一个列表Python中如何将字符串和列表结合起来,一个元组
a=100
b=["abc","def"]
如何合并这些到一个元组,看起来像(ABC,100),(闪避,100 )?我试图
>>> for i in file:
... tuple(file, uid)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: tuple() takes at most 1 argument (2 given)
答
a=100
b=["abc","def"]
print [(i,a) for i in b]
为此,您可以通过简单的list comprehension
答
为回溯说,tuple
需要1种说法,在你的代码,只是改变tuple(file, uid)
到tuple([file, uid])
。也就是说,tuple
需要一个可迭代的参数
谢谢@vks。 – file2cable