繁体   English   中英

将元组列表转换为字符串列表

[英]convert a list of tuples to a list of strings

我有这个元组列表:

listoftuples=[(0, 0), (0, 1), (1, 0), (1, 1)]

或者也许是这样:

  listoftuples=[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]

我试图得到这个:

['00','01','10','11']

使用此代码:

listofstrings = ','.join(str(v) for v in listoftuples)

我究竟做错了什么?

>>> ['%d%d' % t for t in listoftuples]
['00', '01', '10', '11']

或更一般地说

>>> ['%d' * len(t) % t for t in listoftuples]
['00', '01', '10', '11']

这应该有效:

list("".join(str(x) for x in v) for v in listoftuples)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM