R
R
Roman Alexandrov2020-08-21 11:45:18
Flask
Roman Alexandrov, 2020-08-21 11:45:18

Why does groupby work like this?

I decided to use groupby to group by a common feature (the id of the related object).

The result was the following code

for k, v in groupby(order.proposal.proposal_products, key=lambda prod_cp: prod_cp.request_product_id):
     lv = list(v)
     print(type(k), k, lv)


Result

<class 'int'> 25 [<ProposalProduct 11>]
<class 'int'> 28 [<ProposalProduct 12>]
<class 'int'> 24 [<ProposalProduct 10>]
<class 'int'> 28 [<ProposalProduct 14>]



Why are the keys duplicated?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alexandrov, 2020-08-21
@AlexandrovRoman

I figured it out. For groupby to work correctly, the array must be sorted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question