2017-06-13 20 views
0

Python 3.6.0append Listenwerte zu einem Wörterbuch

Ich bin ein kleines Programm zu schreiben, die Benutzereingaben in Form nimmt: Stadt, Land

ich dann einen Wörterbuch des Schlüssels erstellen: Wert-Paare wo das Land ist der Schlüssel und die Städte sind die Werte.

Allerdings möchte ich den Wert (Stadt) Stück eine Liste sein, so dass ein Benutzer in mehreren Städten für das gleiche Land eingeben kann.

Beispiel:

city1, country1 city1, country2 city2, country1

Ich bin in der Nähe mit diesem Code:

destinations = {} 
while True: 
    query = input("Tell me where you went: ") 
    if query == '': 
     break 
    temp = query.split(',') 
    if len(temp) != 2: 
     temp = [] 
     continue 
    city = [query.split(',')[0]] 
    country = query.split(',')[1] 
    if country not in destinations: 
     destinations[country] = city 
    else: 
     destinations[country].append(city) 

Mein Problem ist, dass hängten Städte ihre eigene Liste auch sind . Dies ist von PyCharm:

destinations = {' country1': ['city1', ['city2']], ' country2': ['city1']} 

Was ich will, ist dies:

destinations = {' country1': ['city1', 'city2'], ' country2': ['city1']} 

ich, warum dies geschieht, aber ich kann nicht scheinen, um herauszufinden, wie man die weitere Städte anhängen Liste, ohne dass jede Stadt in ihrer eigenen Liste ist.

Wenn der Benutzer jetzt eingibt: City3, country1 dann Zielen {} sollte sein:

destinations = {' country1': ['city1', 'city2', 'city3'], ' country2': ['city1']} 

Sie bekommen die Idee.

Danke.

+0

einfach die Listenerstellung bewegen - 'Stadt = query.split ('') [0]' (oder 'Stadt, Land = Abfrage .split (',') ') dann' Ziele [Land] = [Stadt] '. Oder benutze 'collections.defaultdict (list)'. – jonrsharpe

Antwort

1

Wenn Sie eine Liste mit [].append([]) anhängen, wird die Liste selbst angehängt und nicht der tatsächliche Inhalt. Was Sie tun können, ist ziemlich ähnlich zu dem, was Sie derzeit haben, aber wenn Sie die Variable city setzen, setzen Sie es auf den eigentlichen Text selbst und passen Sie dann den Code in der if Anweisung an.

destinations = {} 
while True: 
    query = input("Tell me where you went: ") 
    if query == '': 
     break 
    temp = query.split(',') 
    if len(temp) != 2: 
     temp = [] 
     continue 
    city = query.split(',')[0] //set city to the string and not the string in a list 
    country = query.split(',')[1] 
    if country not in destinations: 
     destinations[country] = [city] //now the value for the key becomes an array 
    else: 
     destinations[country].append(city) 
1

Ändern Sie einfach an die Stelle der Listenerstellung

destinations = {} 
while True: 
    query = input("Tell me where you went: ") 
    if query == '': 
     break 
    temp = query.split(',') 
    if len(temp) != 2: 
     temp = [] 
     continue 
    city = query.split(',')[0] 
    country = query.split(',')[1] 
    if country not in destinations: 
     destinations[country] = [city] # <-- Change this line 
    else: 
     destinations[country].append(city)