Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何插入“|&引用;在-&引用;像这样每隔5分钟?_Python_Loops_Printing - Fatal编程技术网

Python 如何插入“|&引用;在-&引用;像这样每隔5分钟?

Python 如何插入“|&引用;在-&引用;像这样每隔5分钟?,python,loops,printing,Python,Loops,Printing,我想做像这张照片,但我不知道插入“|” 这是我的密码 """test""" def counts(): """process""" text = input() text_abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" for i in text_abc: count_text = text.count(i) if count_text > 0:

我想做像这张照片,但我不知道插入“|”

这是我的密码

"""test"""
def counts():
    """process"""
    text = input()
    text_abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for i in text_abc:
        count_text = text.count(i)
        if count_text > 0:
            ans = "-"* count_text
            print("%s : %s"%(i, ans))
counts()
它是这样输出的

my input:"aaaaaaaabbbbbcccdd"
my output:
a : --------
b : -----
c : ---
d : --
我该怎么办?提前感谢您提供的任何帮助。
ps.对不起,我的英语不好

最简单的方法就是通过
str.replace()
将其放入
ans
,默认情况下,它将替换从前面开始的任意数量的事件:

ans = ("-" * count_text).replace('-----', '-----|')

您也可以这样做:

ans = "-" * count_text
ans = '|'.join(ans[i:i+5] for i in range(0, len(ans), 5))
完整代码:

def counts():
    """process"""
    text = input()
    text_abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for i in text_abc:
        count_text = text.count(i)
        if count_text > 0:
            ans = "-" * count_text
            ans = '|'.join(ans[i:i+5] for i in range(0, len(ans), 5))
            print("%s : %s"%(i, ans))

counts()
输出:

例1

例2


我使用了collections软件包中的计数器模块,格式也在3.6中,我不确定collections是否适用于低于3.6的版本,请交叉检查

from collections import Counter

"""test"""
def counts():
    """process"""
    text = Counter(input()) # counter returns a list with key as the element and value as it's frequency

    for i in text.keys(): # iterating in keys
        count_text = text[i]  # getting the frequency of that alphabet
        pattern = ['|-' if (_ % 5 == 0 and _ != 0) else '-' for _ in range(count_text)] # creating a list in which we put '|-' if the position if mod of 5 else we just put '-'.
        patternString = ''.join(pattern) # then we convert it to string
        print(f'{i}: {patternString} ')

counts()

这是最好的XD
"aaaaaaaabbbbbcccdd"
a : -----|---
b : -----
c : ---
d : --
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbcccccccccccccccccccccddyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
a : -----|-----|-----|-----|-----|-----|-----|-
b : ----
c : -----|-----|-----|-----|-
d : --
y : -----|-----|-----|-----|-----|-----|----
from collections import Counter

"""test"""
def counts():
    """process"""
    text = Counter(input()) # counter returns a list with key as the element and value as it's frequency

    for i in text.keys(): # iterating in keys
        count_text = text[i]  # getting the frequency of that alphabet
        pattern = ['|-' if (_ % 5 == 0 and _ != 0) else '-' for _ in range(count_text)] # creating a list in which we put '|-' if the position if mod of 5 else we just put '-'.
        patternString = ''.join(pattern) # then we convert it to string
        print(f'{i}: {patternString} ')

counts()