I have a list of dictionaries, I sort it by a certain parameter (EPС). Now I need to randomly select from this list 20-30% of dictionaries that do not need to be sorted. So it should be sort 1-2-3-4-5 by EPC, and then 20-30% of objects selected at random, and then the rest sorted by EPC.
For example: We have 10 records, take 30% (3 records) randomly (before sorting), then sort the rest of 7, then place 3 randomly selected records in the n-place of sorted (for example in the middle of the sorted list)
My list example
OFFERS = [
{
"id":"43136bb6-c5e5-4d34-acff-605ff64e4d85",
"impressions":2000,
"clicks":481,
"leads":1,
"payout":1768,
"epc":3
},
{
"id":"f7025432-e7c3-414a-b817-833c0a520c69",
"impressions":3241,
"clicks":467,
"leads":48,
"payout":2486,
"epc":255
},
{
"id":"53c567be-86aa-4eb1-8448-2a701bc9bff0",
"impressions":1083,
"clicks":588,
"leads":19,
"payout":3195,
"epc":103
},
{
"id":"0d45d6cd-ead9-407c-aa13-3e9e9ed2898d",
"impressions":2670,
"clicks":569,
"leads":24,
"payout":2801,
"epc":118
},
{
"id":"fd470c97-a9c2-4171-8327-a7e2151a6d76",
"impressions":3975,
"clicks":592,
"leads":39,
"payout":2410,
"epc":158
},
{
"id":"a8f8b25a-b52b-4d76-b3e0-0c517d75d9dd",
"impressions":1917,
"clicks":317,
"leads":5,
"payout":3443,
"epc":54
},
{
"id":"1ce96ecc-b034-4809-a990-c2279cd78e66",
"impressions":3531,
"clicks":411,
"leads":22,
"payout":1681,
"epc":89
},
{
"id":"d634df9b-5dd2-484c-8298-1788f66891df",
"impressions":2959,
"clicks":489,
"leads":19,
"payout":1106,
"epc":42
},
{
"id":"1169d632-25f2-4241-a6e5-37ba94469d0e",
"impressions":2201,
"clicks":566,
"leads":16,
"payout":1773,
"epc":50
},
{
"id":"683842d4-e212-4f66-a729-2e70ef0c2837",
"impressions":3875,
"clicks":511,
"leads":49,
"payout":3289,
"epc":315
},
{
"id":"fbe95639-e03d-4a65-89b8-c7c21628b744",
"impressions":4070,
"clicks":409,
"leads":61,
"payout":1965,
"epc":293
},
{
"id":"54f4dfa5-dbd5-439f-a6a6-534214a60b45",
"impressions":2728,
"clicks":223,
"leads":15,
"payout":2906,
"epc":195
},
{
"id":"e751efb9-ad53-4635-89db-b7eee4b9c586",
"impressions":2005,
"clicks":506,
"leads":2,
"payout":1457,
"epc":5
},
{
"id":"467cbed3-121c-4c8e-a094-d43b371a677f",
"impressions":3320,
"clicks":286,
"leads":45,
"payout":3136,
"epc":493
},
{
"id":"8d726f55-4570-4827-99b4-909ebd9dd166",
"impressions":3913,
"clicks":588,
"leads":20,
"payout":3436,
"epc":116
}
]
Now im sorting by epc
sorted_data = sorted(OFFERS, key=lambda d: d['epc'], reverse=True)
Can someone help with the implementation of random selection without sorting?