簡體   English   中英

為 -if 循環優化 python

[英]Optimizing python for -if loops

我有一個包含多個 if 條件的 for 循環。 這個 function 的速度對我的應用程序來說並不重要,但我想學習並有一些技巧來優化我的代碼以使其運行得更快。 這是循環:

from collections import OrderedDict

sorted_list = sorted(odl, key=lambda item: (item['date'], item['time']))
sl_list = []
type_a = 0
type_b = 0
type_c_1 = 0
type_c_2 = 0
for s in sorted_list:
    if s['type'] == 'A':
        if type_a < 5:
            sl_list.append(s)
            type_a += 1
    if s['type'] == 'B':
        if type_b < 5:
            sl_list.append(s)
            type_b += 1
    if s['type'] == 'C':
        if s['C_TYPE'] == 'C_1':
            if type_c_1 < 2:
                sl_list.append(s)
                type_c_1 += 1
        if s['C_TYPE'] == 'C_2':
            if type_c_2 < 2:
                sl_list.append(s)
                type_c_2 += 1

在處理大量數據時,我是否會面臨一些明顯的性能問題? 我會很感激每一個提示,以提高效率!

編輯。 樣本輸入:

odl = [OrderedDict([('id', '1'), ('date', '2022-01-08'), ('time', '15:59:00'), ('type', 'A')]), OrderedDict([('id', '8'), ('date', '2022-01-08'), ('time', '14:59:00'), ('type', 'A')]), OrderedDict([('id', '2'), ('date', '2022-01-09'), ('time', '11:59:00'), ('type', 'A')]),  OrderedDict([('id', '3'), ('date', '2022-01-08'), ('time', '12:59:00'),  ('type', 'B')]), OrderedDict([('id', '9'), ('date', '2022-01-09'), ('time', '17:59:00'),  ('type', 'B')]), OrderedDict([('id', '4'), ('date', '2022-01-09'), ('time', '18:59:00'), ('type', 'B')]),
       OrderedDict([('id', '5'), ('date', '2022-01-08'), ('time', '09:59:00'), ('type', 'C'), ('C_TYPE', 'C_1')]), OrderedDict([('id', '6'), ('date', '2022-01-09'), ('time', '10:59:00'), ('type', 'C'), ('C_TYPE', 'C_2')]), OrderedDict([('id', '7'), ('date', '2022-01-07'), ('time', '16:59:00'),  ('type', 'A')])]

示例 output:

[OrderedDict([('id', '7'), ('date', '2022-01-07'), ('time', '16:59:00'), ('type', 'A')]), ...]

就像 MisterMiyagi 評論的那樣,我試圖獲得C_1C_2每種類型的前 5 個 A、5 B 和前 2 個 C。 代碼工作正常,我得到了想要的 output 但我想讓它盡可能高效,因為我不確定稍后會提供多少數據。

您可以嘗試根據您的情況調整此邏輯。

對不起我的英語,我自然會說法語。

sorted_list = []
sl_list = []
type_list =  {"A":0, "B":0, "C1":0, "C2":0}

for s in sorted_list:
    for i in type_list.keys():
        if s['type'] == i:
            if i in ['A', 'B'] and type_list[i] < 5:
                sl_list.append(s)
                type_list[i] += 1
            elif i in ['C1', 'C2'] and type_list[i] < 2:
                sl_list.append(s)
                type_list[i] += 1

簡單案例的示例:

sorted_list = [
{'type':"A"},
{'type':"B"},
{'type':"C1"},
{'type':"A"},
{'type':"C2"},
{'type':"B"},
{'type':"C1"},
{'type':"B"},
{'type':"B"},
{'type':"C1"},
{'type':"A"},
{'type':"C2"},
{'type':"C2"},
{'type':"C1"},
{'type':"B"},
{'type':"B"},
{'type':"B"},

]

sl_list = []
type_list =  {"A":0, "B":0, "C1":0, "C2":0}

for s in sorted_list:
    for i in type_list.keys():
        if s['type'] == i:
            if i in ['A', 'B'] and type_list[i] < 5:
                sl_list.append(s)
                type_list[i] += 1
            elif i in ['C1', 'C2'] and type_list[i] < 2:
                sl_list.append(s)
                type_list[i] += 1

print(f"sl_list: {sl_list}")

return: sl_list: [{'type': 'A'}, {'type': 'B'}, {'type': 'C1'}, {'type': 'A'}, {'type': 'C2'}, {'type': 'B'}, {'type': 'C1'}, {'type': 'B'}, {'type': 'B'}, {'type': 'A'}, {'type': 'C2'}, {'type': 'B'}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM