简体   繁体   中英

Convert 2 lists in a python dictionary

I got a long list of information through sqlite3. I've created 2 lists, the first one is made up of the first elements (Vercelli, Vercelli ecc). The second list is composed by the seconds elements (viale dell'Aeronautica, piazza Cesare Battisti). I would like to create a dictionary which join the first list's elements as keys and the second list's element as value. But I would also like to create a dictionary that groups values under a single key (see the example below the code). When I try python it shows me the key with only the last value. I would be very happy if someone tried to help me

conntta = sqlite3.connect("Database.db")
cursortta = conntt.cursor()
sqltta = cursortt.execute("select np,id from orari")




#Sqlite3

    ('Vercelli', "viale dell'Aeronautica")
    ('Vercelli', 'piazza Cesare Battisti')
    ('Vercelli', 'Autostazione corso Gastaldi')
    ('Caresanablot', 'SP230/via Aldo Moro')
    ('Quinto v.se', 'Regione Bivio')
    ('Oldenico', 'SS594 (peso pubblico)')
    ('Albano', 'piazza Roma (chiesa)')
    ('Greggio', 'piazza Roma (posta)')
    ('Arborio', 'corso Umberto I (chiesa)')
    ('Ghislarengo', 'piazza Umberto I (posta)')
    ('Lenta', 'via XXV Luglio (municipio)')
    ('Gattinara', 'corso Vercelli (ospedale)')
    ('Gattinara', 'piazza Mazzini (stazione)')
    ('Gattinara', 'via Volta (ist.Ragion.-Geom.)')
    ('Romagnano', 'SS142 Ponte Sesia')
    ('Romagnano', 'via XXV Luglio')
    ('Serravalle', 'Vintebbio (fr.chiesa)')
    ('Serravalle', 'Piane via dei Ceri/S.Giacomo')
    ('Serravalle', 'p.za I Maggio (scuole)')
    ('Serravalle', 'Bornate Corso Valsesia')
    ('Borgosesia', 'Agnona (ITIS)')
    ('Borgosesia', 'San Rocco c.so Vercelli')
    ('Borgosesia', "v.le Duca d'Aosta (IPSIA)")
    ('Borgoseisa', 'via Antongini')
    ('Borgosesia', 'v.le Varallo/p.le Milanaccio')
    ('Borgosesia', 'Centro sportivo Milanaccio')
    ('Quarona', 'piazza Combattenti')
    ('Quarona', 'Doccio (scuole)')
    ('Varallo', 'Roccapietra via Varalli (posta)')
    ('Varallo', 'piazza Marconi (stazione)')
    ('Varallo', 'piazza Marconi (stazione)')
    ('Varallo', 'Istituto Caimi')
    ('Varallo', 'Istituto Alberghiero')
    ('Varallo', 'piazza Marconi (stazione)')
    ('Varallo', 'Valmaggia bivio Cantone')
    ('Vocca', 'Chiesa (fr.municipio)')
    ('Balmuccia', 'bivio Rimasco')
    ('Balmuccia', 'via Roma (scuole)')
    ('Scopa', 'Salterana (fr. posta)')
    ('Scopello', 'piazzale (bivio per Mera)')
    ('Pila', 'SS299/via Centro')
    ('Piode', 'SS299 via Umberto I')
    ('Campertogno', 'SS299 piazzale (fr.posta)')
    ('Mollia', 'SS299 bivio Rusa')
    ('Riva Valdobbia', 'Piazza Mercato')
    ('Alagna', 'piazzale (lungo Sesia)')

list1 = []
list2 = []
for cd in sqltta:
    list1.append(cd[0])
    list2.append(cd[1])


#Here I can't go further

Current Output:

{'Vercelli' : 'Autostazione corso Gastaldi', 'Caresanablot', 'SP230/via Aldo Moro'ecc... } #Here the other values ​​of vercelli are missing

Example (of what i want):

{'Vercelli': 'viale dell'Aeronautica', 'piazza Cesare Battisti', 'Autostazione corso Gastaldi  ecc}
conntta = sqlite3.connect("Database.db")
cursortta = conntt.cursor()
sqltta = cursortt.execute("select np,id from orari")

your_first_dict = dict(sqltta) # yes, that's all that is needed!

from collections import defaultdict
your_second_dict = defaultdict(list)
for k,v in sqltta:
    your_second_dict[k].append(v)

Be warned though that your_first_dict will contain only the last entry for a given key - that's the nature of dict type, you can't have multiple entries with the same key. your_second_dict will have lists as values.

You can just build your dictionary by appending id values into lists indexed by np values:

result = {}
sqltta = cursortt.execute("select np,id from orari")
for np, id in sqltta:
    if np in result:
        result['np'].append(id)
    else:
        resul['np'] = [id]

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