如何在Python中遍历一个列表的列表?

15
我有一个像这样的列表列表。
documents = [['Human machine interface for lab abc computer applications','4'],
             ['A survey of user opinion of computer system response time','3'],
             ['The EPS user interface management system','2']]

现在我需要遍历上面的列表并输出一个字符串列表,如下所示(不包括原始列表中的数字)

documents = ['Human machine interface for lab abc computer applications',
             'A survey of user opinion of computer system response time',
             'The EPS user interface management system']

4
Python 中没有数组,你指的应该是“列表”(list)。 - juliomalegria
这个回答解决了你的问题吗?在Python中迭代列表的列表 - Jim G.
7个回答

35

实现您所需的最简单的解决方案是:

documents = [sub_list[0] for sub_list in documents]

这基本上相当于迭代版本:
temp = []
for sub_list in documents:
    temp.append(sub_list[0])
documents = temp

这种方式并不是迭代任意维度的多维列表的通用方法,因为嵌套的列表推导式/嵌套for循环可能会变得丑陋;但是你可以安全地对2或3维列表使用它。
如果你决定需要展开超过3个维度,则建议实现一个 递归遍历函数 来展平所有非平面层。

它只是降低了每个子数组的第一个参数,这不是问题所要求的! - Anu
1
可以简单地解压容器,即一个列表(列表的列表!)*documents,然后您可以使用range()简单地迭代(跳过)。 - Anu

8
如果您只想简单地遍历循环并对元素执行操作(而不是问题中请求的特定结果),则可以使用基本的for循环。
for row in documents:
  #do stuff with the row
  print(row)

  for column in row:
    #do stuff with the columns for a particular row
    print(column)

  if(row[1] > 10):
    print('The value is much too large!!')

这是一种被称为“流程控制”的语言特性。
请注意,如果您只想得到问题中给出的结果,像机器学习提供的列表推导式是最好的方法。
documents = [doc[0] for doc in documents]

注意,它会丢弃您的原始文档列表(您正在覆盖原始变量),因此如果您想要第一列的副本以及原始列表的副本,请使用以下内容:
document_first_row = [doc[0] for doc in documents]

5

1

**编辑。谢谢DSM指出错误。这只是将列表压平了。我没有注意到OP想要忽略的文本后面还有额外的数据。

好的,我会让它变得非常简单!

itertools.chain.from_iterable(documents)

正如其他人所说,这取决于您需要的最终行为。因此,如果您需要的东西比这更复杂,请使用递归遍历或者如果您像我一样,请使用迭代遍历。如果您需要帮助,我可以帮您实现。


1
这将使列表扁平化,而不是提取第一个元素。 - DSM
确实。我误解了他想要的行为。遍历帮助的提供仍然开放。 - KobeJohn

1
这个问题已经过时了,但知道另一种方法也无妨:
 documents = [['Human machine interface for lab abc computer applications','4'],
         ['A survey of user opinion of computer system response time','3'],
         ['The EPS user interface management system','2']]

document = []
for first,*remaining in documents:
    document.append(first)

print(document)
['Human machine interface for lab abc computer applications',
 'A survey of user opinion of computer system response time', 
 'The EPS user interface management system'
]

1
它只是降低了每个子数组的第一个参数,这不是问题所要求的! - Anu
1
我不明白,用户希望从一个列表中得到仅包含字符串的列表作为最后输出。你能告诉我我哪里没有理解这个问题吗? - Vaibhav Singh

0
您还可以使用具有参数解压缩的 zip 方法将“行”列表转换为列列表:
rows=[[1,'a','foo'],
      [2,'b','bar'],
      [3,'c','baz']]

columns=zip(*rows)
print columns
#[(1,2,3),
# ('a','b','c'),
# ('foo','bar','baz')]
print columns[0]
#(1,2,3)

星号运算符将所有行作为单独的参数传递给zip函数

zip(*rows) == zip(row1,row2,row3,...)

zip函数将所有行中的元素组合成列,每个列由来自各个列表中相同位置的元素组成。


0
你可以使用NumPy数组。
例如。
document = [['the quick brown fox', '2' ],['jumped over the lazy fox ','3']]

#

import numpy as np 
document = np.array(document)
document=document[:,0]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接