检查我的ddict中是否有任何字母的值大于1,如果是,则打印一些文本,如果不是,则转换为lis

2024-05-20 10:26:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用python重新创建enigma代码,目前我正在获取交换机设置,为此我要确保输入中没有重复的字母(格式为“AB CD EF GH”等)。目前我正在使用默认dict检查输入字符串中每个字母的数量。如果有任何重复,我要能够打印错误,如果没有,则将值存储在列表中(像[AB,CD,EF,GH])我找了很多都没找到,希望你能帮忙。代码如下

global SwitchboardSettings
            global d
            NSwitchboardSettings = input("Enter the switchboard settings leaving a space between each, there should be 13 pairs: ")

            d = collections.defaultdict(int)
            for c in NSwitchboardSettings:  #Collections module, FIX
                d[c] += 1

Tags: 字符串代码数量ab格式字母cdenigma
1条回答
网友
1楼 · 发布于 2024-05-20 10:26:00

你的问题有点宽泛,不太精确,所以我希望我在这个代码示例中是对的

from string import ascii_letters

input_ok = 'AB CD EF gh ij'
input_nok = 'AB CD EF gh ij Ag'

# Searches for repeated characters
for ch in ascii_letters:
    count = input_nok.count(ch)
    if count > 1:
        print('{} is repeated {} times'.format(ch, count))
        # Do something else

# Convert pair of letters into a list
lst_ok = input_ok.split(' ')
lst_nok = input_nok.split(' ')

print(lst_ok)
print(lst_nok)

相关问题 更多 >