📜  Python - 字符串中的所有子字符串频率

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

Python - 字符串中的所有子字符串频率

给定一个字符串,提取所有唯一的子字符串及其频率。

方法 #1:使用循环 + 列表推导

上述功能的组合可以用来解决这个问题。在此,我们首先使用列表推导提取所有子字符串,然后使用循环来增加频率。

Python3
# Python3 code to demonstrate working of 
# All substrings Frequency in String
# Using loop + list comprehension
  
# initializing string
test_str = "abababa"
  
# printing original string
print("The original string is : " + str(test_str))
  
# list comprehension to extract substrings
temp = [test_str[idx: j] for idx in range(len(test_str))
       for j in range(idx + 1, len(test_str) + 1)]
  
# loop to extract final result of frequencies
res = {}
for idx in temp:
     if idx not in res.keys():
             res[idx] = 1
     else:
             res[idx] += 1
               
# printing result 
print("Extracted frequency dictionary : " + str(res))


Python3
# Python3 code to demonstrate working of 
# All substrings Frequency in String
# Using list comprehension
  
# initializing string
test_str = "abababa"
  
# printing original string
print("The original string is : " + str(test_str))
  
# list comprehension to extract substrings and frequency
res = dict()
for ele in [test_str[idx: j] for idx in range(len(test_str)) for j in range(idx + 1, len(test_str) + 1)]:
    res[ele] = 1 if ele not in res.keys() else res[ele] + 1             
      
# printing result 
print("Extracted frequency dictionary : " + str(res))


输出
The original string is : abababa
Extracted frequency dictionary : {'a': 4, 'ab': 3, 'aba': 3, 'abab': 2, 'ababa': 2, 'ababab': 1, 'abababa': 1, 'b': 3, 'ba': 3, 'bab': 2, 'baba': 2, 'babab': 1, 'bababa': 1}

方法#2:使用列表推导

这是可以执行此任务的另一种方式。在此,我们执行两个任务,即在单个嵌套列表理解中提取子字符串和计算频率。

Python3

# Python3 code to demonstrate working of 
# All substrings Frequency in String
# Using list comprehension
  
# initializing string
test_str = "abababa"
  
# printing original string
print("The original string is : " + str(test_str))
  
# list comprehension to extract substrings and frequency
res = dict()
for ele in [test_str[idx: j] for idx in range(len(test_str)) for j in range(idx + 1, len(test_str) + 1)]:
    res[ele] = 1 if ele not in res.keys() else res[ele] + 1             
      
# printing result 
print("Extracted frequency dictionary : " + str(res)) 
输出
The original string is : abababa
Extracted frequency dictionary : {'a': 4, 'ab': 3, 'aba': 3, 'abab': 2, 'ababa': 2, 'ababab': 1, 'abababa': 1, 'b': 3, 'ba': 3, 'bab': 2, 'baba': 2, 'babab': 1, 'bababa': 1}