[英]Increment value in the list based on condition
我正在准備供個人使用的代碼,該代碼將基於歷史彩票號碼給出每組號碼中的百分比。 我將一組彩票可用數字分為三組:
first_group = [1,2,3,4,5,6,17,18,19,20,21,33,34,35,36,37] second_group = [7,8,9,10,11,12,22,23,24,25,26,38,39,40,41,42] third_group = [13,14,15,16,27,28,29,30,31,32,43,44,45,46,47,48,49]
original numbers.txt
從一開始就包含所有彩票的數據庫
my_list = []
with open('original numbers.txt') as f:
lines=f.readlines()
for line in lines:
my_array = np.fromstring(line, dtype=int, sep=' ')
my_list.append(my_array)``
代碼遍歷文件的每一行,並根據文件所屬的組分配1,2,3值。
for line in my_list: for num in line: if num in first_group: new_set.append(1) elif num in second_group: new_set.append(2) elif num in third_group: new_set.append(3)
現在,我無法進一步介紹的部分。 我已經設定了條件,如果每次抽獎中的1,2,3之和大於4,它將增加dash
,這是每個組計算百分比值的分母。
count = Counter(new_set)
'''example
5 21 28 31 33 41
1 1 3 3 1 2'''
if count[1] == 4:
rash_one += 1
for x,y in line,new_set:
if y == 1:
elif count[2] == 4:
rash_two +=1
elif count[3] == 4:
rash_three +=1``
我要完成的工作是為1-49數字創建list
或dict
,並用計數值遞增它們。 因此,例如,如果在第一組中大多數都rash_one = 10
抽獎狀態,那么我的rash_one = 10
並且有2次抽獎3次,有17次抽獎4次。 因此它給我們提供了30%的nr 2和40%的nr 17。
我的代碼(到目前為止):
import numpy as np
from collections import Counter
my_list = []
with open('original numbers.txt') as f:
lines=f.readlines()
for line in lines:
my_array = np.fromstring(line, dtype=int, sep=' ')
my_list.append(my_array)
first_group = [1,2,3,4,5,6,17,18,19,20,21,33,34,35,36,37]
second_group = [7,8,9,10,11,12,22,23,24,25,26,38,39,40,41,42]
third_group = [13,14,15,16,27,28,29,30,31,32,43,44,45,46,47,48,49]
rash_one = 0
rash_two = 0
rash_three = 0
new_set = []
for line in my_list:
for num in line:
if num in first_group:
new_set.append(1)
elif num in second_group:
new_set.append(2)
elif num in third_group:
new_set.append(3)
count = Counter(new_set)
'''example
5 21 28 31 33 41
1 1 3 3 1 2'''
if count[1] == 4:
rash_one += 1
for x,y in line,new_set:
if y == 1:
'''stopped here'''
elif count[2] == 4:
rash_two +=1
elif count[3] == 4:
rash_three +=1``
解決了:
from collections import defaultdict
first_data_group = defaultdict(int)
second_data_group = defaultdict(int)
third_data_group = defaultdict(int)
if count[1] == 4:
print(new_set)
rash_one += 1
for x,y in zip(line,new_set):
if y == 1:
first_data_group[x] += 1
elif count[2] == 4:
rash_two +=1
for x,y in zip(line,new_set):
if y == 2:
second_data_group[x] += 1
elif count[3] == 4:
rash_three +=1
for x,y in zip(line,new_set):
if y == 3:
third_data_group[x] += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.