首页 文章

字典计数不显示

提问于
浏览
0

CS学生在这里......也许是因为睡眠不足但我无法弄清楚为什么我的计数根本没有出现 . 格式是正确的,但我无法弄清楚为什么没有显示计数 . 任何帮助表示赞赏 .

Description:

从文本文件:customerData.txt将小企业的客户信息读入customerList . 每个客户记录都在文本文件中的一行上,并在customerList中生成一个条目,该条目恰好是12个字符串字段的列表 . 字段的顺序是名字,中间名字,姓氏,街道地址,城市,州,邮政编码,国家,电子邮件地址,电话号码,性别和生日 .

customerList用于生成包含每个州的客户数量的计数的字典 .

此字典按两个顺序打印:按州代码排序,按州计数排序 .

Current Code:

def main():
    """ Open's file, reads customer information into a list, closes the file"""
    custFile = open('customerData.txt','r')
    customerList = generateList(custFile)
    custFile.close()
    statesCountDictionary = generateStatesCountDictionary(customerList)
    printSortedByState(statesCountDictionary)
    printSortedByCount(statesCountDictionary)

def generateList(custFile):
    """ Reads customer data from file and returns a list of customers"""
    customers = []
    for line in custFile:
        custInfoList = line.strip().split(',')
        customers.append(custInfoList)
    return customers

def generateStatesCountDictionary(customerList):
    """ Tallies the number of customers from each state and
        returns a dictionary with the state as a key and the
        count as the associated value."""
    statesTallyDict = {}
    for customer in customerList:
        state = customer[5]
        if state in statesTallyDict:
            count = statesTallyDict.get(state) +1
        else:
            count = 1
    return statesTallyDict

def printSortedByState(statesCountDictionary):
    """ Prints the tally of the number of customers from each state
        sorted alphabetically by the state abbreviation."""
    tupleList = statesCountDictionary.items()
    tupleList.sort()
    print "\n\n%5s   %-14s" % ("State","Customer Count")
    print "-"*25
    for item in tupleList:
        state, count = item
        print "%5s   %8d" % (state.center(5), count)

def printSortedByCount(statesCountDictionary):
    """ Prints the tally of the number of customers from each state
        sorted from most to least."""
    stateCountTupleList = statesCountDictionary.items()
    countStateTupleList = []
    stateCountTupleList.sort()
    print "\n\n%5s   %-14s" % ("Customer Count","State")
    print "-"*25
    for item in stateCountTupleList:
        count, state = item
        print "%8d  %5s" %(count.center,state(5))


main()

3 回答

  • 0

    generateStatesCountDictionary 中,您实际上从未向字典中添加任何值 .

    你已经计算了 count 现在你需要在 statesTallyDict 中设置该状态的值

  • 1

    正如user3220892所说,你没有在 statesTallyDict dict中添加任何值:

    def generateStatesCountDictionary(customerList):
        statesTallyDict = {}
        for customer in customerList:
            state = customer[5]
            if state in statesTallyDict:
                count = statesTallyDict.get(state) +1
            else:
                count = 1
        return statesTallyDict
    

    要为字典中的值指定键,您必须“索引”不存在的键并为其赋值:

    my_dict[key] = value
    

    同理:

    if state in statesTallyDict:
        statesTallyDict[state] = statesTallyDict.get(state) +1
    else:
        statesTallyDict[state] = 1
    

    但是,在这种情况下,您可以使用.get的默认值:

    statesTallyDict[state] = statesTallyDict.get(state, 0) + 1
    
  • 0
    if state in statesTallyDict:
            count = statesTallyDict.get(state) +1
        else:
            count = 1
    

    实际上并没有更新字典本身的计数..

    if state in statesTallyDict:
            statesTallyDict[state] = statesTallyDict.get(state) + 1
        else:
            statesTallyDict[state] = 1
    

    或者,使用更多的pythonic

    if state in statesTallyDict:
            statesTallyDict[state] += 1
        else:
            statesTallyDict[state] = 1
    

相关问题