如何使我的Python 3代码更紧凑

时间:2017-10-06 12:47:46

标签: python python-3.x

我显然对python很新。所以我为我的作业编写的代码给了我正确的输出,然而,它只会让你的眼睛受伤。所以我想知道是否有办法让代码更紧凑?

这是我的代码:

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))

并且分配是单独计算每个标点符号并打印该标记加上表格中的结果。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

您可以对字符串进行哈希处理,并将其归功于tobias_k以获得理解:

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进行对话,如果作业只是简单地计算所有标点符号,那么我建议稍微更改以上内容:

import string
dictionary = {c: 0 for c in string.punctuation}

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

最终更新

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

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

答案 1 :(得分:0)

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

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)))

或者同一个想法在一行中,只是为了好玩:

(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 ".!?:\",;/\\()'")))()