简体   繁体   English

转换 python 字典中的 2 个列表

[英]Convert 2 lists in a python dictionary

I got a long list of information through sqlite3.我通过 sqlite3 得到了一长串信息。 I've created 2 lists, the first one is made up of the first elements (Vercelli, Vercelli ecc).我创建了 2 个列表,第一个列表由第一个元素(Vercelli、Vercelli ecc)组成。 The second list is composed by the seconds elements (viale dell'Aeronautica, piazza Cesare Battisti).第二个列表由秒元素组成(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.当我尝试 python 时,它向我显示只有最后一个值的键。 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:当前 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_first_dict仅包含给定键的最后一个条目 - 这是dict类型的性质,您不能有多个具有相同键的条目。 your_second_dict will have lists as values. your_second_dict将列表作为值。

You can just build your dictionary by appending id values into lists indexed by np values:您可以通过将id值附加到由np值索引的列表中来构建字典:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM