python - Finding occurence in list of lists -
I have a list of different description lists that have populated in such a way:
['Ski', '3', 'Bachid', 4], ['scooo', '9', 'bachid', 5], ['scooo', '3', 'bachid', 5] , ['Ski', '3', 'Bachid', 5]]I need to find the phenomenon of 4 elements in the list of lists and display them in one Appear more often. Any list of lists will be appreciated. Expected Output:
{"3": [['' '9', 'Bachid', 5], ['' '' '' '', '3' ['' '' '' '', '3', 'Bachid', 5] ['Skew', 5], ['' 3 '' '' Bachid '', 5]], "1" , '3', 'bachid', 5]]}
count the fourth element Import from import operator import item from counter to counter (counter (map (3, LCT))) # counter ({5: 3, 4: 1}) and This will work as a standard dictionary
or get the most common list
Print out the imported import items (maximum (lst, key = lst count)) # ['scooo', '3', 'bachid', 5]
Update:
If your expected product
{"3": [['' '' '' '9', 'Bachid', 5] , ['Scooo', '3', 'bachid', 5], ['scooo', '3', 'batchie', 'd', 5]], "1": [['' 'Batch', 4]]}
Then this is the solution
itertools import groupby lst.sort (key = lambda x: x [ 3]) for d = {} x, y in the group (lst, key = lamb da da x: x [3]): y = list (y) d [lane (y)] = y print (d) that output
[1: [[' , '3', 'bachid', 4]], 3: [['scooo', '9', 'bachid', 5], ['scooo', '3' 'bachid', 5] In the case of several elements, with the same number of events:
from the collections: ',' 3 ',' bachid ', 5]]}
Import import by default lst.sort (key = lambda x: x [3]) d = defaultdict (list) for x, y by group (lst, key = lambda x: x [3]): y = List (Y) D [Lane (Y)]. Expansion (y) print (d)
Comments
Post a Comment