【问题标题】:converting list inside tuple and adding on to the tuple转换元组内的列表并添加到元组
【发布时间】:2020-06-01 02:25:34
【问题描述】:
[('Username', 'INFO', 'ERROR'), ('ac', [2, 2]), ('hello', [4,3])]

如何获得:[('Username', 'INFO', 'ERROR'), ('ac', 2, 2), ('hello', 4,3)]

所以基本上如何摆脱元组内的列表!!!!


or:

如何将其转换为 {'ac': [2, 2], 'hello': [4, 3]}{'ac': (2, 2), 'hello': (4, 3)}

收件人:[('ac', 2, 2), ('hello', 4,3)]

【问题讨论】:

    标签: python-3.x list tuples


    【解决方案1】:

    对于字典

    • *v 将解压values
    y = {'ac': [2, 2], 'hello': [4, 3]}
    
    [(k, *v) for k, v in y.items()]
    
    >>> [('ac', 2, 2), ('hello', 4, 3)]
    

    对于元组列表

    z = [('Username', 'INFO', 'ERROR'), ('ac', [2, 2]), ('hello', [4,3])]
    
    xx = list()
    for x in z:
        tt = tuple()
        for y in x:
            if not type(y) == list:
                tt += (y,)
            else:
                tt += (*y,)
        xx.append(tt)
    
    xx = [('Username', 'INFO', 'ERROR'), ('ac', 2, 2), ('hello', 4, 3)]
    

    【讨论】:

    • 嗨,@Trenton 这对我帮助很大,非常感谢。我也为其他人使用了你的 for 循环的串联版本,它就像一个魅力!真的很感激!
    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多