字典计数不显示

时间:2016-06-24 04:52:25

标签: python python-2.7 dictionary tuples

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

说明:

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

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

此字典按两个顺序打印:按州代码排序,并按顺序排序 国家统计。

当前代码:

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 :(得分:1)

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

您已计算count现在需要在statesTallyDict

中设置该州的值

答案 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

答案 2 :(得分:0)

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

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