有没有更好的方法来合并具有子列表的元组的项目而不创建如下所示的新函数?
Is there a better way to merge items of a tuple that has a sublist without creating a new function like below?
我有一个列表:
k_list = [(1,2,3,['a','b','c']), (4,5,6,['d','e','f']), (7,8,9,['g','h','i'])]
想要合并每个元组的子列表,如:
[(1, 2, 3, 'a', 'b', 'c'), (4, 5, 6, 'd', 'e', 'f'), (7, 8, 9, 'g', 'h', 'i')]
或
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]
我想出了以下解决方案:
new_list =[]
def removeNesting(nest):
for e in nest:
if type(e) == list:
removeNesting(e)
else:
output.append(e)
return output
for i in k_list:
output = []
new_list.append(removeNesting(i))
print new_list
但我觉得这不是一个理想的解决方案,所以尝试在不使用函数的情况下做一些事情,当列表中没有整数时,下面的代码工作正常:
new_list1 = []
for e in k_list:
total = []
for i in e:
total += i
new_list1.append(total)
print new_list1
但是当列表中有整数时,我在这一行收到错误:total += i
TypeError: 'int' object is not iterable
如何解决这个问题?
提前感谢您的阅读和帮助!!
简单地理解列表:
>>> [(a,b,c,*d) for a,b,c,d in k_list]
[(1, 2, 3, 'a', 'b', 'c'), (4, 5, 6, 'd', 'e', 'f'), (7, 8, 9, 'g', 'h', 'i')]
考虑到最后一个嵌套项始终是可迭代的(独立于内部项的总数)
k_list = [(1,2,3,['a','b','c']), (4,5,6,['d','e','f']), (7,8,9,['g','h','i'])]
res = [list(i[:-1]) + i[-1] for i in k_list]
print(res)
输出:
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]
如果将非列表项目包装在列表中,您可以始终使用双精度迭代它们以进行理解。
1个
不过读起来并不容易:
>>> [
... tuple(
... [
... sublist_element for subsublist_or_number in sublist for sublist_element in (
... subsublist_or_number # either a list
... if isinstance(subsublist_or_number, list)
... else [subsublist_or_number] # or a number
... )
... ]
... )
... for sublist in k_list
... ]
[(1, 2, 3, 'a', 'b', 'c'), (4, 5, 6, 'd', 'e', 'f'), (7, 8, 9, 'g', 'h', 'i')]
您可以使用广义解包操作符将子列表之前的项目打包到另一个列表中,以便您可以使用+
操作符合并它们:
[a + b for *a, b in k_list]
这个returns:
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]
我有一个列表:
k_list = [(1,2,3,['a','b','c']), (4,5,6,['d','e','f']), (7,8,9,['g','h','i'])]
想要合并每个元组的子列表,如:
[(1, 2, 3, 'a', 'b', 'c'), (4, 5, 6, 'd', 'e', 'f'), (7, 8, 9, 'g', 'h', 'i')]
或
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]
我想出了以下解决方案:
new_list =[]
def removeNesting(nest):
for e in nest:
if type(e) == list:
removeNesting(e)
else:
output.append(e)
return output
for i in k_list:
output = []
new_list.append(removeNesting(i))
print new_list
但我觉得这不是一个理想的解决方案,所以尝试在不使用函数的情况下做一些事情,当列表中没有整数时,下面的代码工作正常:
new_list1 = []
for e in k_list:
total = []
for i in e:
total += i
new_list1.append(total)
print new_list1
但是当列表中有整数时,我在这一行收到错误:total += i
TypeError: 'int' object is not iterable
如何解决这个问题?
提前感谢您的阅读和帮助!!
简单地理解列表:
>>> [(a,b,c,*d) for a,b,c,d in k_list]
[(1, 2, 3, 'a', 'b', 'c'), (4, 5, 6, 'd', 'e', 'f'), (7, 8, 9, 'g', 'h', 'i')]
考虑到最后一个嵌套项始终是可迭代的(独立于内部项的总数)
k_list = [(1,2,3,['a','b','c']), (4,5,6,['d','e','f']), (7,8,9,['g','h','i'])]
res = [list(i[:-1]) + i[-1] for i in k_list]
print(res)
输出:
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]
如果将非列表项目包装在列表中,您可以始终使用双精度迭代它们以进行理解。 1个 不过读起来并不容易:
>>> [
... tuple(
... [
... sublist_element for subsublist_or_number in sublist for sublist_element in (
... subsublist_or_number # either a list
... if isinstance(subsublist_or_number, list)
... else [subsublist_or_number] # or a number
... )
... ]
... )
... for sublist in k_list
... ]
[(1, 2, 3, 'a', 'b', 'c'), (4, 5, 6, 'd', 'e', 'f'), (7, 8, 9, 'g', 'h', 'i')]
您可以使用广义解包操作符将子列表之前的项目打包到另一个列表中,以便您可以使用+
操作符合并它们:
[a + b for *a, b in k_list]
这个returns:
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]