Es fällt mir schwer, eine Liste von Tupeln mit Schlüsselwerten in ein Wörterbuch umzuwandeln.

Ich habe eine Liste von Tupeln:

[('season', '1', 'episode', '1', 'showkey', '1736'),
('season', '1', 'episode', '2', 'showkey', '1737'),
('season', '1', 'episode', '3', 'showkey', '1738'),
('season', '1', 'episode', '4', 'showkey', '1739'),
('season', '1', 'episode', '5', 'showkey', '1740'),
('season', '1', 'episode', '6', 'showkey', '1741'),
('season', '1', 'episode', '7', 'showkey', '1742'),
('season', '1', 'episode', '8', 'showkey', '1743'),
('season', '1', 'episode', '9', 'showkey', '1744'),
('season', '1', 'episode', '10', 'showkey', '1745'),
('season', '1', 'episode', '11', 'showkey', '1746'),
('season', '1', 'episode', '12', 'showkey', '1747'),
('season', '1', 'episode', '13', 'showkey', '1748')]

Wie kann ich ein Wörterbuch mit folgenden Schlüsselwertpaaren erstellen:

{'season':1, 'episode':2, 'showkey':1736}

Danke für jede Hilfe!

-1
Swatty43 12 Aug. 2015 im 07:45

4 Antworten

x=[('season', '1', 'episode', '1', 'showkey', '1736'),
('season', '1', 'episode', '2', 'showkey', '1737'),
('season', '1', 'episode', '3', 'showkey', '1738'),
('season', '1', 'episode', '4', 'showkey', '1739'),
 ('season', '1', 'episode', '5', 'showkey', '1740'),
  ('season', '1', 'episode','6', 'showkey', '1741'),
 ('season', '1', 'episode', '7', 'showkey', '1742'),
('season', '1', 'episode', '8', 'showkey', '1743'),
('season', '1', 'episode', '9', 'showkey', '1744'),
('season', '1', 'episode', '10', 'showkey', '1745'),
('season', '1', 'episode', '11', 'showkey', '1746'),
('season', '1', 'episode', '12', 'showkey', '1747'),
('season', '1', 'episode', '13', 'showkey', '1748')]
d={}
m=0
for i in x:
    d1={}
    for j in i[::2]:

        d1[j]=i[i.index(j)+1]

    d[m]=d1
    m=m+1

print d

Sie können ein Wörterbuch des Wörterbuchs erstellen, um alle Daten zu speichern.

0
vks 12 Aug. 2015 im 05:00
inputd = [('season', '1', 'episode', '1', 'showkey', '1736'),
('season', '1', 'episode', '2', 'showkey', '1737'),
('season', '1', 'episode', '3', 'showkey', '1738'),
('season', '1', 'episode', '4', 'showkey', '1739'),
('season', '1', 'episode', '5', 'showkey', '1740'),
('season', '1', 'episode', '6', 'showkey', '1741'),
('season', '1', 'episode', '7', 'showkey', '1742'),
('season', '1', 'episode', '8', 'showkey', '1743'),
('season', '1', 'episode', '9', 'showkey', '1744'),
('season', '1', 'episode', '10', 'showkey', '1745'),
('season', '1', 'episode', '11', 'showkey', '1746'),
('season', '1', 'episode', '12', 'showkey', '1747'),
('season', '1', 'episode', '13', 'showkey', '1748')]


>>> map(dict, map(lambda x: zip(x[::2],x[1::2]), inputd))
[{'season': '1', 'episode': '1', 'showkey': '1736'}, {'season': '1', 'episode': '2', 'showkey': '1737'}, {'season': '1', 'episode': '3', 'showkey': '1738'}, {'season': '1', 'episode': '4', 'showkey': '1739'}, {'season': '1', 'episode': '5', 'showkey': '1740'}, {'season': '1', 'episode': '6', 'showkey': '1741'}, {'season': '1', 'episode': '7', 'showkey': '1742'}, {'season': '1', 'episode': '8', 'showkey': '1743'}, {'season': '1', 'episode': '9', 'showkey': '1744'}, {'season': '1', 'episode': '10', 'showkey': '1745'}, {'season': '1', 'episode': '11', 'showkey': '1746'}, {'season': '1', 'episode': '12', 'showkey': '1747'}, {'season': '1', 'episode': '13', 'showkey': '1748'}]

Über Python slice

  1. x[::2] - bedeutet Start bis Ende, wobei jeweils 2 Stellen übersprungen werden Zeit
  2. x[1::2] - bedeutet, dass Sie mit Index 1 bis Ende beginnen und 2 überspringen Orte jedes Mal
0
Suku 12 Aug. 2015 im 05:31

Ich würde ein Listenverständnis verwenden und dabei Diktate aus dem Tupel erstellen, indem ich jeweils 2 aufeinanderfolgende Elemente darin nehme.

Code -

d = [dict((i[j],i[j+1]) for j in range(0,len(i),2)) for i in lt]

Hier ist lt die Liste der Tupel.

Beispiel -

>>> lt = [('season', '1', 'episode', '1', 'showkey', '1736'),
... ('season', '1', 'episode', '2', 'showkey', '1737'),
... ('season', '1', 'episode', '3', 'showkey', '1738'),
... ('season', '1', 'episode', '4', 'showkey', '1739'),
... ('season', '1', 'episode', '5', 'showkey', '1740'),
... ('season', '1', 'episode', '6', 'showkey', '1741'),
... ('season', '1', 'episode', '7', 'showkey', '1742'),
... ('season', '1', 'episode', '8', 'showkey', '1743'),
... ('season', '1', 'episode', '9', 'showkey', '1744'),
... ('season', '1', 'episode', '10', 'showkey', '1745'),
... ('season', '1', 'episode', '11', 'showkey', '1746'),
... ('season', '1', 'episode', '12', 'showkey', '1747'),
... ('season', '1', 'episode', '13', 'showkey', '1748')]
>>>
>>> d= [dict((i[j],i[j+1]) for j in range(0,len(i),2)) for i in lt]
>>> d
[{'showkey': '1736', 'episode': '1', 'season': '1'}, {'showkey': '1737', 'episode': '2', 'season': '1'}, {'showkey': '1738', 'episode': '3', 'season': '1'}, {'showkey': '1739', 'episode': '4', 'season': '1'}, {'showkey': '1740', 'episode':
 'season': '1'}, {'showkey': '1744', 'episode': '9', 'season': '1'}, {'showkey': '1745', 'episode': '10', 'season': '1'}, {'showkey': '1746', 'episode': '11', 'season': '1'}, {'showkey': '1747', 'episode': '12', 'season': '1'}, {'showkey':
1
Anand S Kumar 12 Aug. 2015 im 04:51
map(lambda datum: dict(zip(datum[::2], datum[1::2])), data)

In Zeitlupe:

results = []
for datum in data:
    # pick off every alternate item from the tuple, starting with the zeroth.
    keys = datum[::2]
    # pick off every alternate item from the tuple, starting with the oneth.
    values = datum[1::2]
    kv = zip(keys, values)
    new_dict = dict(kv)
    results.append(new_dict)

Bitte entschuldigen Sie Tippfehler und tippen Sie diese auf meinem Telefon ein.

0
Danver Braganza 12 Aug. 2015 im 04:59