📜  Python程序使字符频率相等

📅  最后修改于: 2022-05-13 01:54:49.671000             🧑  作者: Mango

Python程序使字符频率相等

给定一个字符串,确保它具有相等的字符频率,如果不是,则通过添加所需的字符来相等。

方法 #1:使用 count() + max() + 循环

在这里,我们得到最大出现字符的频率,然后附加每个字符以匹配最大字符频率。

Python3
# Python3 code to demonstrate working of 
# Equal character frequencies
# Using max() + count() + loop
  
# initializing string
test_str = 'geeksforgeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# getting maximum frequency character 
max_freq = max([test_str.count(ele) for ele in test_str])
  
# equating frequencies 
res = test_str
for chr in test_str:
      
    # if frequencies don't match max_freq
    if res.count(chr) != max_freq:
        res += chr * (max_freq - test_str.count(chr))
      
# printing result 
print("Equal character frequency String : " + str(res))


Python3
# Python3 code to demonstrate working of 
# Equal character frequencies
# Using Counter() + loop 
from collections import Counter
  
# initializing string
test_str = 'geeksforgeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# getting maximum frequency character 
# using Counter()
freq_dict = Counter(test_str) 
max_freq = test_str.count(max(freq_dict, key = freq_dict.get)) 
  
# equating frequencies 
res = test_str
for chr in test_str:
      
    # if frequencies don't match max_freq
    if res.count(chr) != max_freq:
        res += chr * (max_freq - test_str.count(chr))
      
# printing result 
print("Equal character frequency String : " + str(res))


输出:

The original string is : geeksforgeeks
Equal character frequency String : geeksforgeeksggkkssfffooorrr

方法 #2:使用 Counter() + 循环

与上面类似,不同之处在于 Counter() 用于获取最大元素计数。

蟒蛇3

# Python3 code to demonstrate working of 
# Equal character frequencies
# Using Counter() + loop 
from collections import Counter
  
# initializing string
test_str = 'geeksforgeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# getting maximum frequency character 
# using Counter()
freq_dict = Counter(test_str) 
max_freq = test_str.count(max(freq_dict, key = freq_dict.get)) 
  
# equating frequencies 
res = test_str
for chr in test_str:
      
    # if frequencies don't match max_freq
    if res.count(chr) != max_freq:
        res += chr * (max_freq - test_str.count(chr))
      
# printing result 
print("Equal character frequency String : " + str(res)) 

输出:

The original string is : geeksforgeeks
Equal character frequency String : geeksforgeeksggkkssfffooorrr