简体   繁体   中英

Python - How to split a list into two separate lists dynamically

I am using Python-3 and I am reading a text file which can have multiple paragraphs separated by '\\n'. I want to split all those paragraphs into a separate list. There can be n number of paragraphs in the input file.

So this split and output list creation should happen dynamically thereby allowing me to view a particular paragraph by just entering the paragraph number as list[2] or list[3], etc....

So far I have tried the below process :

input = open("input.txt", "r")  #Reading the input file
lines = input.readlines()       #Creating a List with separate sentences
str = ''                        #Declaring a empty string

for i in range(len(lines)):
    if len(lines[i]) > 2:       #If the length of a line is < 2, It means it can be a new paragraph
        str += lines[i]

This method will not store paragraphs into a new list (as I am not sure how to do it). It will just remove the line with '\\n' and stores all the input lines into str variable. When I tried to display the contents of str, it is showing the output as words. But I need them as sentences.

And my code should store all the sentences until first occurence of '\\n' into a separate list and so on.

Any ideas on this ?

UPDATE I found a way to print all the lines that are present until '\\n'. But when I try to store them into the list, it is getting stored as letters, not as whole sentences. Below is the code snippet for reference

input = open("input.txt", "r")
lines = input.readlines()
input_ = []

for i in range(len(lines)):
    if len(lines[i]) <= 2:
        for j in range(i):
            input_.append(lines[j]) #This line is storing as letters.

even "input_ += lines" is storing as letters, Not as sentences.

Any idea how to modify this code to get the desired output ?

Don't forgot to do input.close() , or the file won't save.

Alternatively you can use with .

#Using "with" closes the file automatically, so you don't need to write file.close()
with open("input.txt","r") as file:
    file_ = file.read().split("\n")

file_ is now a list with each paragraph as a separate item.

It's as simple as 2 lines.

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