首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使Python 3代码更紧凑

如何使Python 3代码更紧凑
EN

Stack Overflow用户
提问于 2017-10-06 20:47:46
回答 2查看 95关注 0票数 0

我显然是python的新手。所以我为我的赋值写的代码给了我正确的输出,然而,它只会伤害你的眼睛。所以我想知道是否有一种方法可以使代码更紧凑?

下面是我的代码:

代码语言:javascript
复制
import sys

period_count = 0
excl_count = 0
quest_count = 0
colon_count = 0
dquote_count = 0
comma_count = 0
scolon_count = 0
slash_count = 0
bslash_count = 0
lparen_count = 0
rparen_count = 0
quote_count = 0

for line in sys.stdin:
    for char in line:
        if char == ".":
            period_count = period_count + 1
        if char == "!":
            excl_count = excl_count +1
        if char == "?":
            quest_count = quest_count +1
        if char == ":":
            colon_count = colon_count +1
        if char == "\"":
            dquote_count = dquote_count +1
        if char == ",":
            comma_count = comma_count +1
        if char == ";":
            scolon_count = scolon_count +1
        if char == "/":
            slash_count = slash_count +1
        if char == "\\":
            bslash_count = bslash_count +1
        if char == "(":
            lparen_count = lparen_count +1
        if char == "(":
            rparen_count = rparen_count +1  
        if char == "'":
            quote_count = quote_count +1    

print("{0} {1:>3} {0:>4} {2:>5} {0:>4}".format("|", "mark", "number"))
print("{0} {1} {0} {2} {0}".format("|", "_______", "_________"))                

print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", ".", period_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", "!", excl_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", "?", quest_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", ":", colon_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", "\"", dquote_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", ",", comma_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", ";", scolon_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", "/", slash_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", "\\", bslash_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", "(", lparen_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", ")", rparen_count))
print("{0} {1:>3} {0:>5} {2:>5} {0:>5}".format("|", "'", quote_count))

任务是分别计算每个标点符号的数量,并将该标点符号和结果打印在表格中。

任何帮助都将不胜感激!

EN

回答 2

Stack Overflow用户

发布于 2017-10-06 20:53:05

您可以对字符串进行散列,并将其归功于tobias_k的理解:

代码语言:javascript
复制
dictionary = {c: 0 for c in ".!?:\",;/\\()'"}

for line in sys.stdin:
   for char in line:
      if char in dictionary:
          dictionary[char] += 1

for char, counter in dictionary.items():
    print(...) # formatted string goes here

编辑

每次在评论中与Chris_Rands交谈时,如果任务是简单地计算所有标点符号,那么我建议稍微修改一下上面的内容:

代码语言:javascript
复制
import string
dictionary = {c: 0 for c in string.punctuation}

Rest如下所示。在我的机器上,string.punctuation'!"#$%&'()*+,-./:;<=>?@[\]^_{|}~' (根据文档,该集合是基于区域设置的)。

最终更新

感谢Muposat提醒我使用defaultdict:当0int构造函数的默认值时,不需要初始化键和计数器:

代码语言:javascript
复制
import sys
import string

dictionary = defaultdict(int)
for line in sys.stdin:
    for char in line:
        if char in string.punctuation:
            dictionary[char] += 1


for char, counter in dictionary.items():
    print(...) # formatted string goes here
票数 3
EN

Stack Overflow用户

发布于 2017-10-06 21:13:38

这个较短的版本将使用内置类collections.Counter计算所有字符频率,并打印所需的频率(包括原始表格格式):

代码语言:javascript
复制
import sys, collections
freqs = collections.Counter(char for line in sys.stdin.readlines() for char in line)
print("|  mark | count |")
print("|_______|_______|")
for char in ".!?:\",;/\\()'":
  print("| {:>5} | {:>5} |".format(char, freqs.get(char, 0)))

或者同样的想法,只是为了好玩:

代码语言:javascript
复制
(lambda freqs=__import__('collections').Counter(char for line in __import__('sys').stdin.readlines() for char in line):print("|  mark | count |\n|_______|_______|\n"+"".join("| {:>5} | {:>5} |\n".format(char, freqs.get(char, 0)) for char in ".!?:\",;/\\()'")))()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46606133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档