Mam łączenie dwóch listy listy jednym wspólnym elementem w Pythonie.

Na przykład mam poniższą listę listy:

[[1.0, 'Software Developer', 1256],
 [1.0, 'Software Developer', 1329],
 [1.0, 'Software Developer', 1469],
 [1.0, 'Software Developer', 2086],
 [0.9230769230769231, 'United States', 1256],
 [0.9230769230769231, 'United States', 1329],
 [0.9230769230769231, 'United States', 1469],
 [0.9230769230769231, 'United States', 2086]]

I moje pragnienie w następujący sposób:

{'ID': 1469,
 'Location': 'United States',
 'Location_score': 0.9230769230769231,
 'title': 'Software Developer',
 'title_score': 1.0}

{'ID': 1256,
 'Location': 'United States',
 'Location_score': 0.9230769230769231,
 'title': 'Software Developer',
 'title_score': 1.0}

Jest to przykładowe wyjście, które chcę zrobić dla wszystkich.

Czy ktoś może powiedzieć mi, jak połączyć cały wspólny element, który jest ostatni na wszystkich liście. a następnie konwertuj listę w słowniku.

Próbowałem użyć funkcji 'Union'. I zrobiłem kilka Google, ale nie mam odpowiedniej odpowiedzi.

Czy ktoś może mi pomóc.

Z góry dziękuję

2
Pmsheth 4 czerwiec 2018, 16:21

3 odpowiedzi

Najlepsza odpowiedź

Używanie collections.defaultdict:

from collections import defaultdict

lst = [[1.0, 'Software Developer', 1256],
       [1.0, 'Software Developer', 1329],
       [1.0, 'Software Developer', 1469],
       [1.0, 'Software Developer', 2086],
       [0.9230769230769231, 'United States', 1256],
       [0.9230769230769231, 'United States', 1329],
       [0.9230769230769231, 'United States', 1469],
       [0.9230769230769231, 'United States', 2086]]

# initialize defaultdict of dicts
d = defaultdict(dict)

# calculate half length of list
n = int(len(lst)/2)

# iterate first part of list
for title_score, title, ID in lst[:n]:
    d[ID]['title_score'] = title_score
    d[ID]['title'] = title

# iterate second part of list
for Location_score, Location, ID in lst[n: len(lst)]:
    d[ID]['Location_score'] = Location_score
    d[ID]['Location'] = Location

Wynik

defaultdict(dict,
            {1256: {'Location': 'United States', 'Location_score': 0.9230769230769231,
                    'title': 'Software Developer', 'title_score': 1.0},
             1329: {'Location': 'United States', 'Location_score': 0.9230769230769231,
                    'title': 'Software Developer', 'title_score': 1.0},
             1469: {'Location': 'United States', 'Location_score': 0.9230769230769231,
                    'title': 'Software Developer', 'title_score': 1.0},
             2086: {'Location': 'United States', 'Location_score': 0.9230769230769231,
                    'title': 'Software Developer', 'title_score': 1.0}})

Jeśli potrzebujesz listy słowników, możesz użyć rozumienia listy:

res = [{**{'ID': k}, **v} for k, v in d.items()]
1
jpp 4 czerwiec 2018, 13:29

Korzystając z zwykłego dyktatu, zakładając tylko, że pojawiają się zapis "tytuł" przede wszystkim:

>>> lol = [[1.0, 'Software Developer', 1256],
...  [1.0, 'Software Developer', 1329],
...  [1.0, 'Software Developer', 1469],
...  [1.0, 'Software Developer', 2086],
...  [0.9230769230769231, 'United States', 1256],
...  [0.9230769230769231, 'United States', 1329],
...  [0.9230769230769231, 'United States', 1469],
...  [0.9230769230769231, 'United States', 2086]]
>>> 
>>> keys = [(gr + '_score', gr, 'ID') for gr in ('title', 'Location')]
>>> 
>>> out = {}
>>> for L in lol:
...     d = out.setdefault(L[-1], {})
...     d.update(zip(keys[bool(d)], L))
... 
>>> out # dict of dicts
{1256: {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1256, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, 1329: {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1329, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, 1469: {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1469, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, 2086: {'title_score': 1.0, 'title': 'Software Developer', 'ID': 2086, 'Location_score': 0.9230769230769231, 'Location': 'United States'}}
>>> list(out.values()) # list of dicts
[{'title_score': 1.0, 'title': 'Software Developer', 'ID': 1256, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1329, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1469, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, {'title_score': 1.0, 'title': 'Software Developer', 'ID': 2086, 'Location_score': 0.9230769230769231, 'Location': 'United States'}]

Lub --- Jeśli sprawy zlecenia dyktatu (Python 3.6+ Inoficial, Python 3.7+ urzędnik):

>>> out = {}
>>> for l in lol:
...     d = out.setdefault(l[-1], {})
...     d.update(zip(*map(reversed, (keys[bool(d)], l))))
... 
>>> out
{1256: {'ID': 1256, 'title': 'Software Developer', 'title_score': 1.0, 'Location': 'United States', 'Location_score': 0.9230769230769231}, 1329: {'ID': 1329, 'title': 'Software Developer', 'title_score': 1.0, 'Location': 'United States', 'Location_score': 0.9230769230769231}, 1469: {'ID': 1469, 'title': 'Software Developer', 'title_score': 1.0, 'Location': 'United States', 'Location_score': 0.9230769230769231}, 2086: {'ID': 2086, 'title': 'Software Developer', 'title_score': 1.0, 'Location': 'United States', 'Location_score': 0.9230769230769231}}
0
Paul Panzer 4 czerwiec 2018, 13:51

To jedno podejście przy użyciu kolekcji.

data = [[1.0, 'Software Developer', 1256],
 [1.0, 'Software Developer', 1329],
 [1.0, 'Software Developer', 1469],
 [1.0, 'Software Developer', 2086],
 [0.9230769230769231, 'United States', 1256],
 [0.9230769230769231, 'United States', 1329],
 [0.9230769230769231, 'United States', 1469],
 [0.9230769230769231, 'United States', 2086]]

from collections import defaultdict
d = defaultdict(list)
for i in data:
    d[i[-1]].extend(i)
res = []
for i in d.values():
    res.append({"ID": i[-1], 'title_score': i[0], 'title': i[1],'Location_score':i[3], 'Location': i[4]})
print(res)

Wynik:

[{'Location_score': 0.9230769230769231, 'Location': 'United States', 'ID': 1256, 'title_score': 1.0, 'title': 'Software Developer'}, {'Location_score': 0.9230769230769231, 'Location': 'United States', 'ID': 1329, 'title_score': 1.0, 'title': 'Software Developer'}, {'Location_score': 0.9230769230769231, 'Location': 'United States', 'ID': 1469, 'title_score': 1.0, 'title': 'Software Developer'}, {'Location_score': 0.9230769230769231, 'Location': 'United States', 'ID': 2086, 'title_score': 1.0, 'title': 'Software Developer'}]
1
Rakesh 4 czerwiec 2018, 13:29