我的名字中有两个字母,它们的数目相等,如何打印两个字母的数目

时间:2018-12-18 15:09:30

标签: python python-3.x

我需要显示字母,如果它的名称中具有最大数量,则该字母为计数。但是,我在名称中有两个字母(n:2,u:2),它们的计数相等,如何打印两个字母及其计数,因为它们具有最大计数和相等计数。我只能写一封信。

    name = 'Annuu'
    name = name.lower()
    names = set(name)
    highest = 0
    p = ''

    for i in names:
        if name.count(i) > highest:
             highest = name.count(i)
             p = i
    print(f"{p} {highest}")

6 个答案:

答案 0 :(得分:0)

您可以使用Counter对象找到计数。 然后找到最大数量以过滤字母。

from collections import Counter

name = "annuu"

count_dict = Counter(name)

max_count = max(count_dict.values())

for letter, count in count_dict.items():
    if count == max_count:
        print(letter, count)

答案 1 :(得分:0)

将值存储在dict中并找到max_frequency

name = 'Annuu'
name = name.lower()

d={}
for i in name:
    d[i]=d.get(i,0)+1

max_freq = max((d.values()))

for k,v in sorted(d.items(),key=lambda (x,y):(y,x), reverse=True):
    if v == max_freq:
        print(k,v)
    else:
        break

答案 2 :(得分:0)

这不使用任何导入:

window

答案 3 :(得分:0)

fsi

的示例
collections.Counter

答案 4 :(得分:0)

以下代码可以工作并产生输出: 最大字符数及其各自的计数如下: n 2 u 2

name = 'Annuu'
name = name.lower()
names = set(name)
name_count_dict = {} # Use dictionary because of easy mapping between character and respective max
for current_char in names:
    # First save the counts in a dictionary
    name_count_dict[current_char] = name.count(current_char)

# Use the max function to find the max (only one max at this point but will find the remaining in the lines below)
max_char = max(name_count_dict, key=name_count_dict.get)
max_value = name_count_dict[max_char]

# Find all other characters which match the max-value, i.e. all maximums
array_of_all_maxes = [k for k, v in name_count_dict.items() if v == max(name_count_dict.values())]

print("The maximum characters and their respective count is as follows:")
for max_chars in array_of_all_maxes:
    print(f"{max_chars} {max_value}")

答案 5 :(得分:0)

我认为这是解决该问题的简单方法,而无需使用任何收藏之类的外部软件包。

  

在这里,我编写了2个测试用例,并重复了相同的代码行。您不必那样做。您拥有超过2、3等的内容。因此最好编写任何其他函数以通过向其传递不同的值来测试代码。

def get_count_and_highest (name): 
    name = name.lower()
    names = set(name)
    highest = 0
    d = {}

    for ch in names:
        count = name.count(ch)
        if count >= highest:
            highest = count

        if highest in d: 
            d[highest].append(ch) 
        else: 
            d[highest] = [ch]

    return highest, d

#Test case 1
highest, d = get_count_and_highest("Annuu") 
l = d.get(highest,  []) # in case if dictionary d is empty then l will be an empty list
output = {ch: highest for ch in l}

print(highest) # 2
print(d)       # {1: ['a'], 2: ['n', 'u']}
print(l)       # ['n', 'u']
print (output) # {'u': 2, 'n': 2}

# Test case 2
highest, d = get_count_and_highest("Babylon python new one") 
l = d.get(highest,  []) # in case if dictionary d is empty then l will be an empty list
output = {ch: highest for ch in l}

print(highest) # 4
print(d)       # {3: ['o', 'p', 'l', 'b', 't', ' ', 'h', 'y', 'w'], 4: ['n', 'e', 'a']}
print(l)       # ['n', 'e', 'a']
print (output) # {'n': 4, 'e': 4, 'a': 4}