簡體   English   中英

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采用一個可迭代的參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM