【问题标题】:How to change this list tuple into list only in Python? [closed]如何仅在 Python 中将此列表元组更改为列表? [关闭]
【发布时间】:2013-03-08 03:42:43
【问题描述】:

我有一个这样的列表:

  [[(u'hello@hello.com',)], [(u'hello@hello.com',)]]

我想把它转换成:

[['hello@hello.com'], ['hello@hello.com']]

我该怎么做?

另一个例子: 输入:[[(u'hello',), (u'hello',)], [(u'hello',)]] 应该返回[['hello@hello.com', 'hello@hello.com'], ['hello@hello.com']]

【问题讨论】:

标签: python list tuples


【解决方案1】:
test = [[(u'hello',), (u'hello',)], [(u'hello',)]]
for i, part_i in enumerate(test):
    for j, part_j in enumerate(part_i):
        test[i][j] = str(part_j[0])

或者,如果您更喜欢单行版本:

test = [[(u'hello',), (u'hello',)], [(u'hello',)]]
result = [[str(j[0]) for j in i] for i in test]

【讨论】:

    【解决方案2】:
    new = [[j[0].encode('utf-8') for j in i] for i in old]
    

    UTF-8 只是一个例子,你真的应该确保你得到正确的编码。

    【讨论】:

    • 还是 unicode?如何删除?
    • 不适用于:[[(u'hello',), (u'hello',)], [(u'hello',)]] 应该返回 [['hello@ hello.com', hello@hello.com], ['hello@hello.com']]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2021-11-18
    • 1970-01-01
    • 2013-01-31
    • 2018-02-01
    • 1970-01-01
    相关资源
    最近更新 更多