简体   繁体   中英

How may I create list of lists in Python to store list of texts?

I am trying to open a bunch of files from a directory and trying to put the results in list of lists that is to say,

that is to say, I have a list of file names of a directory, I want to read each one of them. After reading each one of them, I want to put the results of each file in a list. These lists would again be inserted to create a list of lists.

to do this I am trying to write it as follows:

list_of_files = glob.glob('C:\Python27\*.*')
    print list_of_files
    list1=[]
    list2=[]
    list_N=[list1,list2]
    for i,j in zip(list_of_files,list_N):
        print i,j
        x1=open(i,"r").read()
        x2=j.append(x1)
    all_sent=list_N
    print all_sent

Am I doing anything wrong? If any one may kindly suggest? Is there any smarter way to do it? I am using Python2.7 on Windows 7 Professional edition. You have many list of lists question in Python I have generally reviewed them. I am posting as I did not match. Apology for cross posting. If you may direct me to a previous post, I would surely delete my post.

Try using the following list comprehension:

list_of_files = glob.glob('C:\Python27\*.*')
data = [open(file, 'r').read() for file in list_of_files]

Assuming that you mean you want each the inner arrays to be made up of each line of the file you're reading, you can do it in just one line (modified from the other answer here) after getting the list of files:

data = [open(f, 'r').readlines().split('\n') for f in list_of_files]

To break it down:

open(f, 'r').readlines()

This is opening a file and reading in all the characters in that whole file as a temporarily stored string.

.split('\n')

This is telling Python to turn the string into an array and use the endline character to know where one element of the array ends.

This turns

This is a document.

It contains useful info.

That is all.

into

["This is a document", "It contains useful info.", "That is all."]

And finally, having

for file in list_of_files

inside the array braces means that you're creating an array with the loop, so that each iteration returns a new element to add to the array.

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