Need to get nested lists from the list in python
list_values=[('BNB', '161221'),
('BNB', '171221'),
('BNB', '241221'),
('BNB', '280122'),
('BNB', '311221'),
('BTC', '161221'),
('BTC', '171221'),
('BTC', '241221'),
('BTC', '250222'),
('BTC', '250322'),
('BTC', '280122'),
('BTC', '311221')]
The output of lists needed is
List_op=[[('BNB', '161221'),
('BNB', '171221'),
('BNB', '241221'),
('BNB', '280122'),
('BNB', '311221')],
[('BTC', '161221'),
('BTC', '171221'),
('BTC', '241221'),
('BTC', '250222'),
('BTC', '250322'),
('BTC', '280122'),
('BTC', '311221')]]
I have tried with some list comprehension methods, but couldnt get this. Help is appreciated.
CodePudding user response:
A simple solution:
from collections import defaultdict
list_values = [
('BNB', '161221'),
('BNB', '171221'),
('BNB', '241221'),
('BNB', '280122'),
('BNB', '311221'),
('BTC', '161221'),
('BTC', '171221'),
('BTC', '241221'),
('BTC', '250222'),
('BTC', '250322'),
('BTC', '280122'),
('BTC', '311221')
]
# a dict might be a better data type here
result = defaultdict(list)
for k, v in list_values:
result[k].append(v)
print(result)
# but if you need a list of lists instead of a dictionary:
result = defaultdict(list)
for k, v in list_values:
result[k].append((k, v))
list_op = [*result.values()]
print(list_op)
Output:
defaultdict(<class 'list'>, {'BNB': ['161221', '171221', '241221', '280122', '311221'], 'BTC': ['161221', '171221', '241221', '250222', '250322', '280122', '311221']})
[[('BNB', '161221'), ('BNB', '171221'), ('BNB', '241221'), ('BNB', '280122'), ('BNB', '311221')], [('BTC', '161221'), ('BTC', '171221'), ('BTC', '241221'), ('BTC', '250222'), ('BTC', '250322'), ('BTC', '280122'), ('BTC', '311221')]]
CodePudding user response:
Not the most graceful way to do this, but you could iterate your starting list with a for each loop and build out the 2D output:
list_values=[('BNB', '161221'),
('BNB', '171221'),
('BNB', '241221'),
('BNB', '280122'),
('BNB', '311221'),
('BTC', '161221'),
('BTC', '171221'),
('BTC', '241221'),
('BTC', '250222'),
('BTC', '250322'),
('BTC', '280122'),
('BTC', '311221')]
key = ''
List_op = []
for lv in list_values:
if lv[0] != key: # new key encountered
if key != '':
List_op.append(op) # add previous 1D list to 2D output
key = lv[0] # assign new key value
op = [] # reinitialize 1D list
op.append(lv) # append current tuple to 1D list
List_op.append(op) # add final 1D list to 2D
print(List_op)
This prints:
[[('BNB', '161221'), ('BNB', '171221'), ('BNB', '241221'),
('BNB', '280122'), ('BNB', '311221')],
[('BTC', '161221'), ('BTC', '171221'), ('BTC', '241221'),
('BTC', '250222'), ('BTC', '250322'), ('BTC', '280122'),
('BTC', '311221')]]
CodePudding user response:
Taking advantage of the fact that the first elements are sorted, you can iterate over the list and add new empty list or add an iem to the last subllist depending on where you are.
out = [[]]
for t in list_values:
if out[-1] and out[-1][0][0]!=t[0]:
out.append([])
out[-1].append(t)
Output:
[[('BNB', '161221'),
('BNB', '171221'),
('BNB', '241221'),
('BNB', '280122'),
('BNB', '311221')],
[('BTC', '161221'),
('BTC', '171221'),
('BTC', '241221'),
('BTC', '250222'),
('BTC', '250322'),
('BTC', '280122'),
('BTC', '311221')]]