2017-07-08 9 views
0

Ich versuche, eine verschachtelte Liste in Python zu machen, indem Sie den Benutzer bitten, die Textdatei einzugeben. Die Eingabedatei wird wie unten dargestellt:Anfügen einer Liste zum Erstellen einer geschachtelten Liste in Python

1.3 2.6 3.2 4.1 1 -3 2 -4.1

Und schließlich sollte die Ausgabe sein: [[1.3, 2.6, 3.2, 4.1], [1.0, -3.0, 2.0], [-4,1] ]

Mein Code kann die einzelne Liste untereinander anzeigen, aber ich habe Schwierigkeiten beim Anhängen der Listen. Da ich neu bei Python bin, wird jede Hilfe sehr geschätzt. Danke im Voraus. Mein Code ist wie folgt:

#Ask the user to input a file name 
file_name=input("Enter the Filename: ") 

#Opening the file to read the content 
infile=open(file_name,'r') 

#Iterating for line in the file 
for line in infile: 
    line_str=line.split() 

    for element in range(len(line_str)): 
     line_str[element]=float(line_str[element]) 

    nlist=[[] for line in range(3)] 
    nlist=nlist.append([line_str]) 
    print(nlist) 
+0

vergessen Sie nicht, die Datei zu schließen! – PYA

+0

gibt es mehrere Zeilen in Ihrer Datei richtig? –

Antwort

1
file_name=input("Enter the Filename: ") 

# use "with" block to open file 
# to ensure the file is closed once your code is done with it 
with open(file_name,'r') as infile: 
    # create "nlist" here and append each element to it during iteration 
    nlist=[] 
    for line in infile: 
    line_str=line.split() 
    # no need to iterate with range(len()); use a list comprehension 
    # to map "float" to each list element 
    line_str = [float(element) for element in line_str] 
    nlist.append(line_str) 
    print(nlist) 
+0

Danke, es hat perfekt funktioniert @cmaher – eye

-1

[EDIT] Sorry, ich falsch verstanden vorher Ihre Frage und angenommen, dass die Eingabedatei alle in separaten Zeilen, aber ich denke, es ist wie

strukturiert ist

1.3 2.6 3.2 4.1 1 -3 2 -4.1

Wenn Sie einfach fragen, wie eine Liste, um eine Liste anhängen eine verschachtelte Liste in Python zu erstellen, hier ist, wie Sie es tun:

list1 = [1,2,3,4] 
list2 = [5,6,7,8] 
nested_list = [] 
nested_list.append(list1) 
nested_list.append(list2) 

Dies führt zu:

[ [1,2,3,4] , [5,6,7,8] ]

ich die Antwort oben gab denken eine ziemlich kurze Antwort mit Listenkomprehensionen, aber man kann auch etwas mit Lambda-Funktionen als solche tun:

# Ask the user to input a file name 
file_name = input("Enter the Filename: ") 

nlist = [] 

# Opening the file to read the content 
with open(file_name, 'r') as infile: 
    for line in infile: 
     ls = list(map(lambda x: float(x), line.split())) 
     nlist.append(ls) 

print(nlist) 

Darüber hinaus können Sie diese in eine Einleiner als solche noch kürzer machen:

# Ask the user to input a file name 
file_name = input("Enter the Filename: ") 

# Opening the file to read the content 
with open(file_name, 'r') as infile: 
    ls = list(map(lambda line: list(map(lambda x: float(x), line.split())), infile)) 

Ich hoffe, das hat geholfen.

Verwandte Themen