Lets say I have a list of tuples like the following
l: list[tuple] = [
('bobs trucking', 'ID1'),
('bobs trucking', 'ID2'),
('bobs trucking', 'ID3'),
('bobs groceries', 'ID6'),
('bobs groceries', 'ID7'),
('bobs groceries', 'ID8'),
('bobs groceries', 'ID9')
]
I'd like to group the repeated first tuple elements (e.g. 'bobs trucking') into a dictionary with values that have the grouped IDs concatenated like a CSV but for every n values for each item in a list
# An example of how I'd accomplish this (but need a way to do it for every n elements)
l2: list[str] = ['ID1','ID2','ID3']
csv_delim: str = ','.join(l2)
Every unique key has a list where each list item is a CSV string of size n=3
# Desired ultimate result
bobs_stuff: dict = {
'bobs trucking': ['ID1,ID2,ID3'],
'bobs groceries': ['ID6,ID7,ID8','ID9']
}
Note: I'd like to do this without Pandas.
CodePudding user response:
Iterate over your list of tuples and populate a dictionary with its contents as follows:
list_of_tuples = [
('bobs trucking', 'ID1'),
('bobs trucking', 'ID2'),
('bobs trucking', 'ID3'),
('bobs groceries', 'ID6'),
('bobs groceries', 'ID7'),
('bobs groceries', 'ID8'),
('bobs groceries', 'ID9')
]
bobs_stuff = {}
N = 3
td = {}
for k, v in list_of_tuples:
td.setdefault(k, []).append(v)
for k, v in td.items():
for i in range(0, len(td[k]), N):
bobs_stuff.setdefault(k, []).append(','.join(v[i:i N]))
print(bobs_stuff)
Output:
{'bobs trucking': ['ID1,ID2,ID3'], 'bobs groceries': ['ID6,ID7,ID8', 'ID9']}
CodePudding user response:
Try:
def chunker(seq, size):
return (seq[pos : pos size] for pos in range(0, len(seq), size))
l: list[tuple] = [
("bobs trucking", "ID1"),
("bobs trucking", "ID2"),
("bobs trucking", "ID3"),
("bobs groceries", "ID6"),
("bobs groceries", "ID7"),
("bobs groceries", "ID8"),
("bobs groceries", "ID9"),
]
n = 3
out = {}
for k, v in l:
out.setdefault(k, []).append(v)
for v in out.values():
v[:] = [",".join(chunk) for chunk in chunker(v, n)]
print(out)
Prints:
{
"bobs trucking": ["ID1,ID2,ID3"],
"bobs groceries": ["ID6,ID7,ID8", "ID9"]
}