如何在元组列表中加入元组列表?

时间:2019-10-16 18:35:41

标签: python list tuples

元组列表,每个元组中都有一个列表:

[('22', ['de', 're']), ('11', ['c']), ('8, ['a', 'b', 'd', 'e'])]

我把字典排序后就知道了

dictionary = sorted(dictionary.items(), key=lambda x: x[0])

return dictionary

我想知道是否真的可以完成以下操作 目标是从中获得帮助:

[('8, ['a', 'b', 'd', 'e']), ('11', ['c']), ('22', ['de', 're'])]

此:

[('8', 'a, b, d, e'), ('11', 'c'), ('22', 'de, re')]

4 个答案:

答案 0 :(得分:0)

您是否考虑过使用tuple()函数?该函数需要某种迭代(在您的情况下为列表),并返回具有该列表中值的元组。

答案 1 :(得分:0)

尝试一下:

lst=[('11', ['a', 'b', 'd', 'e']), ('22', ['c']), ('8', ['de', 're'])]

res=list(map(lambda x: tuple([x[0]]+ [", ".join([el for el in x[1]])]), lst))

print(res)

输出:

[('11', 'a, b, d, e'), ('22', 'c'), ('8', 'de, re')]
[Program finished]

答案 2 :(得分:0)

这种理解应该是您想要的:

lst = [('11', ['a', 'b', 'd', 'e']), ('22', ['c']), ('8', ['de', 're'])]
res = [(n, ', '.join(s)) for n, s in lst]

print(res)

打印:

[('11', 'a, b, d, e'), ('22', 'c'), ('8', 'de, re')]

答案 3 :(得分:0)

def myFun(t):
    return int(t[0])
a = [('22', ['de', 're']), ('11', ['c']), ('8', ['a', 'b', 'd', 'e'])]
sorted(a, key=myFun)

输出-[('8',['a','b','d','e']),('11',['c']),('22',['de ', '回覆'])] 你可以试试这个