简体   繁体   English

我需要使我的主要功能正常工作

[英]I need to get my main function working

I am trying to use my create_list to generate 10,000 numbers between 1-6. 我正在尝试使用我的create_list在1-6之间生成10,000个数字。 The count_list is suppose to count the amount of 1,2,3,4,5,6's that were generated and the main function is suppose to attach the two and then prints the results of each. count_list假定要对所生成的1,2,3,4,5,6的数量进行计数,并且主要功能是假定将两者附加在一起,然后打印每个结果。 BTW I just started using def so go easy on me ;) 顺便说一句,我刚刚开始使用def,所以对我轻松一点;)

def count_list(my_list, c):
     amount_one=0
     amount_two=0
     amount_three=0
     amount_four=0
     amount_five=0
     amount_six=0
     or number in my_list:
         if number==1:
             amount_one+=1
         elif number==2:
             amount_two+=1
         elif number==3:
             amount_three+=1
         elif number==4:
             amount_four+=1
         elif number==5:
             amount_five+=1
         elif number==6:
             amount_six+=1
     return amount_one
     return amount_two
     return amount_three
     return amount_four
     return amount_five
     return amount_six
def create_list(n):
     import random
     my_list=[]
     for i in range(10000):
         n=random.randint(1,6)
         my_list.append(n)
     return my_list
def main():
     hello=count_list(create_list)
     print "1-",count_list()[0]
     print "2-",count_list()[1]
     print "3-",count_list()[2]
     print "4-",count_list()[3]
     print "5-",count_list()[4]
     print "6-",count_list()[5]

 main()

You can use the argument n to determine the number of numbers to generate in your list 您可以使用参数n来确定要在列表中生成的数字数量

def create_list(n):
    import random
    my_list = []
    for i in range(n):
        my_list.append(random.randint(1,6))
    return my_list

To count the items in your list, a straight forward way to do it would be like so 要计算您列表中的项目,简单的方法就是这样

def count_list(l):
    c = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0}  # initialize dictionary
    for key in c:
        c[key] = l.count(key)
    return c

Now we can call the above functions in main 现在我们可以在main调用以上函数

def main():
    nums = create_list(1000)
    counts = count_list(nums)
    for i in range(1,7):
        print(i, counts[i])

Testing 测试

>>> main()
1 185
2 166
3 162
4 168
5 143
6 176

For fun, here are some slightly more advanced methods to do the same thing in a more efficient way 为了好玩,这里有一些更高级的方法,可以更有效地完成相同的操作

def create_list(n):
    import random
    return [random.randint(1,6) for _ in range(n)]  # using a list comprehension

def count_list(l):
    from collections import Counter  # using the Counter class
    return Counter(l)

def main():
    nums = create_list(1000)
    counts = count_list(nums)
    for i in range(1,7):
        print(i, counts[i])

use collections.Counter to do the counting, just pass it your list of numbers returned from create_list : 使用collections.Counter进行计数,只需将其从create_list返回的数字列表传递给它create_list

from collections import Counter
import random


def create_list():
    my_list = []
    for i in range(10000):
        n = random.randint(1, 6)
        my_list.append(n)
    return my_list


print(Counter(create_list()))

Counter({1: 1702, 4: 1695, 6: 1692, 3: 1688, 5: 1651, 2: 1572})

If you want the 3 most common numbers etc.. use .most_common(3) : 如果要3个最常用的数字等。请使用.most_common(3)

print(Counter(create_list()).most_common(3))

You can use a dictionary to map the count with corresponding number: 您可以使用字典将计数映射到相应的数字:

def count_list(my_list):
    from collections import defaultdict
    dict = defaultdict(int)

    for x in my_list:
        dict[x] += 1

    return dict


def create_list(a,b):
     import random
     my_list=[]
     for i in range(10000):
         n=random.randint(a,b)
         my_list.append(n)
     return my_list

def main():
     lst = create_list(1,6)
     dict = count_list(lst)
     print(dict)


if __name__=="__main__":
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pygame.mouse.get_pressed() 在我的按钮功能中无法正常工作 - pygame.mouse.get_pressed() not working as I need in my button function 我需要给包含主要 function 的 my.py 文件提供一个集合名称吗? - Is there a set name I need to give my .py file containing the main function? 我如何获得__str __()函数的返回值以在我的main中打印? - How do I get the return value of my __str__() function to print in my main? 如何将 get_me_random_list() function 中生成的随机列表传递到我的 main() function 中? - How do I pass the random list generated in the get_me_random_list() function in my main() function? 需要了解为什么我的 function 是这样工作的 - Need to understand why my function is working in this way 我可以做些什么来让我的 split() function 正常工作吗? - Is there anything I can do to get my split() function working? 如何让我的主要 function 在异常检查输入变量时工作? - How do I get my main function to work while exceptions check the inputted variable? 我该怎么办才能摆脱“ int”对象,这不是我的主函数def模型中的下标错误: - What can I do to get rid of the 'int' object is not subscriptable error in my main function def model: 我需要多个输入来运行主 function 电话簿 python - i need multiple input to run main function phonebook python 如果我在python中使用多重处理,但我的函数未返回任何内容,我是否需要调用get()? - Do I need to call get() if I use multiprocessing in python but my function does not return anything?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM