gpt4 book ai didi

python - 随机异常的字典排序列表(不排序)

转载 作者:行者123 更新时间:2023-12-04 07:24:32 25 4
gpt4 key购买 nike

我有一个字典列表,我按某个参数 (EPС) 对其进行排序。现在我需要从这个列表中随机选择 20-30% 不需要排序的字典。所以应该按EPC排序1-2-3-4-5,然后随机选择20-30%的对象,然后其余的按EPC排序。

For example: We have 10 records, take 30% (3 records) randomly (beforesorting), then sort the rest of 7, then place 3 randomly selectedrecords in the n-place of sorted (for example in the middle of thesorted list)


我的列表示例
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
}
]
现在我按 epc 排序
sorted_data = sorted(OFFERS, key=lambda d: d['epc'], reverse=True)
有人可以在不排序的情况下帮助实现随机选择吗?

最佳答案

尝试:

import random

# select N offers:
N = int(len(OFFERS) * 0.3)
selected = list(random.sample(range(len(OFFERS)), N))

# sort the remaining by EPC
out = sorted(
[o for i, o in enumerate(OFFERS) if i not in selected],
key=lambda k: k["epc"],
reverse=True,
)

# put selected in some place (in the middle):
place = len(out) // 2
for s in selected:
out.insert(place, OFFERS[s])

print(out)
打印(例如):
[
{
"id": "467cbed3-121c-4c8e-a094-d43b371a677f",
"impressions": 3320,
"clicks": 286,
"leads": 45,
"payout": 3136,
"epc": 493,
},
{
"id": "54f4dfa5-dbd5-439f-a6a6-534214a60b45",
"impressions": 2728,
"clicks": 223,
"leads": 15,
"payout": 2906,
"epc": 195,
},
{
"id": "fd470c97-a9c2-4171-8327-a7e2151a6d76",
"impressions": 3975,
"clicks": 592,
"leads": 39,
"payout": 2410,
"epc": 158,
},
{
"id": "0d45d6cd-ead9-407c-aa13-3e9e9ed2898d",
"impressions": 2670,
"clicks": 569,
"leads": 24,
"payout": 2801,
"epc": 118,
},
{
"id": "8d726f55-4570-4827-99b4-909ebd9dd166",
"impressions": 3913,
"clicks": 588,
"leads": 20,
"payout": 3436,
"epc": 116,
},
{
"id": "683842d4-e212-4f66-a729-2e70ef0c2837",
"impressions": 3875,
"clicks": 511,
"leads": 49,
"payout": 3289,
"epc": 315,
},
{
"id": "f7025432-e7c3-414a-b817-833c0a520c69",
"impressions": 3241,
"clicks": 467,
"leads": 48,
"payout": 2486,
"epc": 255,
},
{
"id": "a8f8b25a-b52b-4d76-b3e0-0c517d75d9dd",
"impressions": 1917,
"clicks": 317,
"leads": 5,
"payout": 3443,
"epc": 54,
},
{
"id": "fbe95639-e03d-4a65-89b8-c7c21628b744",
"impressions": 4070,
"clicks": 409,
"leads": 61,
"payout": 1965,
"epc": 293,
},
{
"id": "53c567be-86aa-4eb1-8448-2a701bc9bff0",
"impressions": 1083,
"clicks": 588,
"leads": 19,
"payout": 3195,
"epc": 103,
},
{
"id": "1ce96ecc-b034-4809-a990-c2279cd78e66",
"impressions": 3531,
"clicks": 411,
"leads": 22,
"payout": 1681,
"epc": 89,
},
{
"id": "1169d632-25f2-4241-a6e5-37ba94469d0e",
"impressions": 2201,
"clicks": 566,
"leads": 16,
"payout": 1773,
"epc": 50,
},
{
"id": "d634df9b-5dd2-484c-8298-1788f66891df",
"impressions": 2959,
"clicks": 489,
"leads": 19,
"payout": 1106,
"epc": 42,
},
{
"id": "e751efb9-ad53-4635-89db-b7eee4b9c586",
"impressions": 2005,
"clicks": 506,
"leads": 2,
"payout": 1457,
"epc": 5,
},
{
"id": "43136bb6-c5e5-4d34-acff-605ff64e4d85",
"impressions": 2000,
"clicks": 481,
"leads": 1,
"payout": 1768,
"epc": 3,
},
]

关于python - 随机异常的字典排序列表(不排序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68288292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com