Python 有没有更好的方法来合并具有子列表的元组项,而不创建如下新函数?

Python 有没有更好的方法来合并具有子列表的元组项,而不创建如下新函数?,python,nested-lists,Python,Nested Lists,我有一份清单: 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')] 或 我提出了以下解决方案: new_list =[] def removeNesting(nest): for e in

我有一份清单:

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')] 

我提出了以下解决方案:

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”对象不可编辑

如何解决这个问题


感谢您的阅读和提前的帮助

简单地通过列表理解:

>>> [(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')]

简单地用列表理解:

>>> [(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')]

考虑到最后一个嵌套项始终是一个iterable(独立于内部项的总数)

输出:

[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]

考虑到最后一个嵌套项始终是一个iterable(独立于内部项的总数)

输出:

[[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')]


如果将非列表项包装在列表中,则可以使用双精度循环一致地迭代它们,以便于理解。 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]
这将返回:

[[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]
这将返回:

[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]

使用
*
解包操作员。试试
(1,2,3,*['a','b','c'])
。另外,在将来,使用
isinstance(obj,list)
not
type(obj)=list
。最好使用
collections.abc.Sequence
而不是
list
。使用
*
解包操作符。试试
(1,2,3,*['a','b','c'])
。另外,在将来,使用
isinstance(obj,list)
not
type(obj)=list
。更好的是,使用
collections.abc.Sequence
,而不是
list
。对于上面的列表效果很好,但是如果列表的整数部分太长:[(1,2,3,4,5,6,7,8,9,10,['a','b','c'],(6,7,8,9,10,11,12,13,14,15['a','b','c']),或者整数长度是一个类似于:[(1,2,3,4,5,['a','b','c','a','c']7']对于我发布的原始列表,这个问题的答案一点也不适合我,说*的无效语法对我有效。对于上面的列表,您可能想升级到PythonWorks great的最新版本,但是如果列表的整数部分太长:[(1,2,3,4,5,6,7,8,9,10,['a','b','c'],(6,7,8,9,10,11,12,13,14,15['a','b']),或者整数长度是一个类似于:[(1,2,3,4,5,['a','b','c'],(6,7,['a','c']对于我发布的原始列表,这个问题的答案一点也不适合我,说*的无效语法对我有效。您可能需要升级到Python的最新版本