简体   繁体   English

返回零(计数)作为 Python 词典中的值

[英]return zero(count) as value in Python Dictionaries

I have a code which works pretty well if not returning keys with zero counts as values.如果不返回计数为零的键作为值,我有一个代码可以很好地工作。 I am still new to Python, so this may be a matter of a simple tweek, but don't know how So please how can I return 0 as a value of a key,if there are 0 counts.我还是 Python 新手,所以这可能是一个简单的 tweek 问题,但不知道如何如果有 0 个计数,我如何将 0 作为键的值返回。

def count_types(string):
    dictionary = {}

   for char in string:
        if 'Z' >= char >= 'A':
            dictionary.setdefault("upper", 0)
            dictionary["upper"] += 1
        elif 'z' >= char >= 'a':
            dictionary.setdefault("lower", 0)
            dictionary["lower"] += 1
        elif char == ' ':
            dictionary.setdefault("space", 0)
            dictionary["space"] += 1
        elif '9' >= char >= '0':
            dictionary.setdefault("numeral", 0)
            dictionary["numeral"] += 1
        else:
            dictionary.setdefault("punctuation", 0)
            dictionary["punctuation"] += 1
  return dictionary

print(count_types("aabbccc")) 

Output is:输出是:

{'lower': 7}

Desired output is:期望的输出是:

{'lower': 7, 'upper': 0, 'punctuation': 0, 'space': 0, 'numeral': 0}

Try like this.像这样尝试。 Initialize the dictionary earlier提前初始化字典

def count_types(string):
    dictionary = {'lower':0, 'upper':0, 'space':0, 'numeral':0, 'punctuation':0}
    
    for char in string:
        if 'Z' >= char >= 'A':
            dictionary.setdefault("upper", 0)
            dictionary["upper"] += 1
        elif 'z' >= char >= 'a':
            dictionary.setdefault("lower", 0)
            dictionary["lower"] += 1
        elif char == ' ':
            dictionary.setdefault("space", 0)
            dictionary["space"] += 1
        elif '9' >= char >= '0':
            dictionary.setdefault("numeral", 0)
            dictionary["numeral"] += 1
        else:
            dictionary.setdefault("punctuation", 0)
            dictionary["punctuation"] += 1
    return dictionary

print(count_types("aabbccc")) 

output输出

{'lower': 7, 'upper': 0, 'space': 0, 'numeral': 0, 'punctuation': 0}
> 

The bug in your code is that if there are no numerical chars in the string for example, the code in the if statement never executes so the key will never get inserted to the dict.代码中的错误是,例如,如果字符串中没有数字字符,则 if 语句中的代码永远不会执行,因此键永远不会插入到字典中。

What I suggest is preparing the dict in adavance with 0 as a default value, and then you can remove all of the defaultdict usage and make your code much shorter:我建议使用 0 作为默认值预先准备 dict,然后您可以删除所有 defaultdict 用法并使您的代码更短:

def count_chars(string):
    dictionary = {'lower': 0, 'upper': 0, 'space': 0, 'numeral': 0, 'punctuation': 0}
    
    for char in string:
        if 'Z' >= char >= 'A':
            dictionary["upper"] += 1
        elif 'z' >= char >= 'a':
            dictionary["lower"] += 1
        elif char == ' ':
            dictionary["space"] += 1
        elif '9' >= char >= '0':
            dictionary["numeral"] += 1
        else:
            dictionary["punctuation"] += 1
    return dictionary

print(count_chars("testtest")) 

I see other answers But you can use import string and use string.ascii_uppercase or string.ascii_lowercase or ... in if condition like below:我看到其他答案但是您可以使用import string并使用string.ascii_uppercasestring.ascii_lowercase或 ... 在如下if condition中:

import string
 
def count_types(chars):
    dictionary = {'lower':0, 'upper':0, 'space':0, 'numeral':0, 'punctuation':0}
    for char in chars:
        if char in string.ascii_uppercase   : dictionary["upper"] += 1
        elif char in string.ascii_lowercase : dictionary["lower"] += 1
        elif char == ' '                    : dictionary["space"] += 1
        elif char in string.digits          : dictionary["numeral"] += 1
        else                                : dictionary["punctuation"] += 1
    return dictionary
print(count_types("a bBC123"))

Output:输出:

{'lower': 2, 'upper': 2, 'space': 1, 'numeral': 3, 'punctuation': 0}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM