Python 如何将字符串和列表组合成元组

Python how to combine a string and list to a tuple

我有一个字符串和一个列表

a=100
b=["abc","def"]

如何将它们组合成一个元组,看起来像 (abc, 100), (def, 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 接受一个可迭代的参数