【问题标题】:Convert each item of list of tuples to string将元组列表的每一项转换为字符串
【发布时间】:2015-11-12 16:13:27
【问题描述】:

如何转换

[('a',1),('b',3)]

[('a','1'),('b','3')]

我的最终目标是:

['a 1','b 3']

我试过了:

[' '.join(col).strip() for col in [('a',1),('b',3)]]

[' '.join(str(col)).strip() for col in [('a',1),('b',3)]]

【问题讨论】:

    标签: python string list tuples


    【解决方案1】:

    应该这样做:

    >>> x = [('a',1),('b',3)]
    >>> [' '.join(str(y) for y in pair) for pair in x]
    ['a 1', 'b 3']
    

    【讨论】:

      【解决方案2】:

      如果你想避免 jme 回答中的列表推导:

      mylist = [('a',1),('b',3)]
      map(lambda xs: ' '.join(map(str, xs)), mylist)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多