【问题标题】:python array count each elementspython数组计算每个元素
【发布时间】:2020-05-31 03:05:49
【问题描述】:

我想要一个小程序来计算用户输入的每个零件号。 到目前为止,我可以做到。

有没有办法可以将零件编号及其频率导出到 .csv 文件?

from collections import Counter
thislist = []
frequency = []
partnumber = ""
def mainmanu():
    print ("1. Create List")
    print ("2. Print list")
    print ("3. Exit")
    while True:
        try:
            selection = int (input("Enter Choice: ")
            if selection ==1:
                creatlist(thislist)
            elif selection ==2:
                counteach(thislist)
            elif selection ==3:
                break
    except ValueError:
        print ("invalid Choice. Enter 1-3")
def creatlist(thislist)
   # while True:
        partnumber = input  ("Enter Part number: ")
        if partnumber =="end":
            print(thislist)
            mainmanu()
            break
        thislist.append(partnumber)
def counteach(thislist)
    Counter(thislist)
    mainmanu()

mainmanu()

【问题讨论】:

    标签: python counter frequency


    【解决方案1】:

    欢迎来到 StackOverflow。

    您正在另一个由 mainmanu 函数调用的函数中调用 mainmanu 函数。相反,您应该做的是让 mainmanu 调用所有其他辅助函数。另一件事是,您不能在另一个函数中调用 break 并期望调用它的位置会中断。

    执行如下:

    mainmanu 被调用,它调用 creatlist,在它完成执行后它继续执行它离开的指令。

     from collections import Counter
            thislist = []
            frequency = []
            partnumber = ""
            def mainmanu():
                print ("1. Create List")
                print ("2. Print list")
                print ("3. Exit")
                while True:
                    try:
                        selection = int (input("Enter Choice: ")
                        if selection ==1:
                            if creatlist(thislist): # line x
                                break
                        elif selection ==2:
                            counteach(thislist)
                        elif selection ==3:
                            break
                except ValueError:
                    print ("invalid Choice. Enter 1-3")
    
    
            def creatlist(thislist)
               # while True:
                    partnumber = input  ("Enter Part number: ")
                    if partnumber =="end":
                        print(thislist)
                        return True #this value will make the the condition to be true see line x
                    thislist.append(partnumber)
                    return false
    
    
            def counteach(thislist)
                Counter(thislist)
            mainmanu()
    

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2017-07-15
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      相关资源
      最近更新 更多