根据条件增加列表中的值

时间:2017-12-09 09:43:16

标签: python pandas numpy

我正在准备供个人使用的代码,该代码将根据历史彩票号码给出每组数字中的百分比部分。我将彩票可用数量分为三组:

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数字创建listdict,并使用计数值递增它们。因此,例如,如果有彩票抽奖,其中主要是第一组,我有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``

1 个答案:

答案 0 :(得分:0)

解决:

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