Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带元组的列表列表到列表列表_Python_List_Tuples - Fatal编程技术网

Python 带元组的列表列表到列表列表

Python 带元组的列表列表到列表列表,python,list,tuples,Python,List,Tuples,我有一个列表,如下所示: a = [['he', 'goes'], ['he does'], ['one time'], [('he','is'), ('she', 'went'), ('they', 'are')], ['he', 'must'], ['they use']] 我试图将列表展平,使其成为一个没有元组的列表列表。例如: a = [['he', 'goes'], ['he does'], ['one time'

我有一个列表,如下所示:

a = [['he', 'goes'],
     ['he does'],
     ['one time'],
     [('he','is'), ('she', 'went'), ('they', 'are')],
     ['he', 'must'],
     ['they use']]
我试图将列表展平,使其成为一个没有元组的列表列表。例如:

a = [['he', 'goes'],
     ['he does'],
     ['one time'],
     ['he','is'], 
     ['she', 'went'],
     ['they', 'are'],
     ['he', 'must'],
     ['they use']]
a = [list(strings) for sublist in a for strings in
     ([sublist] if isinstance(sublist[0], str) else sublist)]

我尝试过使用
itertools.chain.from\u iterable()
,但是这会使所有嵌套列表变平

这解决了您的示例:

a = [['he', 'goes'],
     ['he does'],
     ['one time'],
     ['he','is'], 
     ['she', 'went'],
     ['they', 'are'],
     ['he', 'must'],
     ['they use']]
a = [list(strings) for sublist in a for strings in
     ([sublist] if isinstance(sublist[0], str) else sublist)]
对于已经是字符串列表的每个子列表,只需使用该子列表。否则,遍历该子列表


这就足够了,还是您的实际数据更复杂?

使用和python3的收益率:

b = []
for x in a:
    if isinstance(x[0], tuple):
        b.extend([list(y) for y in x])
    else:
        b.append(x)
from collections import Iterable
def conv(l):
    for ele in l:
        if isinstance(ele[0], Iterable) and not isinstance(ele[0],str):
            yield from map(list,ele)
        else:
            yield ele

print(list(conv(a)))
[['he', 'goes'], ['he does'], ['one time'], ['he', 'is'], ['she', 'went'], ['they', 'are'], ['he', 'must'], ['they use']]
对于
python2
,可以迭代itertools.imap对象:

from collections import Iterable
from itertools import imap

def conv(l):
    for ele in l:
        if isinstance(ele[0], Iterable) and not isinstance(ele[0],basestring):
            for sub in imap(list, ele):
                yield sub
        else:
            yield ele

print(list(conv(a)))

如果您有嵌套元组,则需要添加更多逻辑。

请向我们展示您迄今为止的尝试。您的实际问题是什么?很好的解决方案。您也可以将其设置为
[list(c)for b in…]
,以确保元组转换为列表。我没有否决投票,但是,这非常难理解。也许使用不止一个字母的变量会使答案更便于用户使用,或者是一种解释。@BeowulfOF现在可以了吗?目前为止最聪明的答案是:)将字符串列表转换为字符串列表,这样大列表中的每个元素都可以被视为字符串列表中的一个iterable。@Shashank,如果我没有编写代码,描述可能会让我困惑:-PYeah,循环更清晰的情况之一。还可以做
list(map(list,x))
,顺便说一句。不确定是否更好。我尝试过将“python改为”改为“python2”,但因为太小,所以不允许这样做。。。(我不敢再改变了)。