简体   繁体   中英

How to iterate through a list of lists in python?

I have a list of lists like this.

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

Now i need to iterate through the above list and output a list of strings, as shown below (without the numbers in the original list)

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

The simplest solution for doing exactly what you specified is:

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

This is basically equivalent to the iterative version:

temp = []
for sub_list in documents:
    temp.append(sub_list[0])
documents = temp

This is however not really a general way of iterating through a multidimensional list with an arbitrary number of dimensions, since nested list comprehensions / nested for loops can get ugly; however you should be safe doing it for 2 or 3-d lists.

If you do decide to you need to flatten more than 3 dimensions, I'd recommend implementing a recursive traversal function which flattens all non-flat layers.

If you want to simply iterate over the loop and do things with the elements (rather than the specific results requested in the question), you could use a basic for loop

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

This is a language feature known as " flow control ".

Note that if you only want the result given in the question, a list comprehension like machine yearning provided is the best way to do it.

documents = [doc[0] for doc in documents]

Note that it discards your original documents list (you are overwriting the original variable) so use the following if you want to have a copy of the first column as well as a copy of your original list:

document_first_row = [doc[0] for doc in documents]

As explained in http://docs.python.org/library/operator.html#operator.itemgetter , You can also try with

from operator import itemgetter
documents = map(itemgetter(0), documents)

that should be faster than using an explicit loop.

**edit. thanks DSM. This is wrong as it just flattens the lists. I didn't notice the extra data inside the list after the text that OP wants to ignore.

Ok I'll make it really easy for you!

itertools.chain.from_iterable(documents)

As others have said, it depends on what final behavior your need. So if you need something more complex than that, use recursive traversal or if you are like me, use an iterative traversal. I can help you with that if you need it.

You can also use zip with argument unpacking to transform a list of "rows" into a list of columns:

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)

the * operator passes all the rows in as seperate arguments to zip

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

zip takes all the rows and assembles columns with one item from each list

you could use numpy array

for instance

document = [['the quick brown fox', '2' ],['jumped over the lazy fox ','3']]

#

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

The question is dead but still knowing one more way doesn't hurt:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM