python实现在一段字符串中,找出重复的字段(分享python算法)

人生总有一段时光是你一直在努力,但是默默无为没有任何回报和结果,我是王大毛~

练习python中自带函数的使用,快速掌握python数据类型的各个内置方法:

今天分享一段简单的实现在一段字符串中,找出重复的字段,如有更好的方法,欢迎评论区留言。

用到的内置方法有:

str.split:分割字符串

  set.(list): 找出list中重复的元素,得到的结果以没有key,只有value的字典类型;

  count(list.obj):count() 方法用于统计某个元素在列表中出现的次数。

str_1 = '''It was designed by the prestigious architect Zaha Hadid and has a wave-like roof that is 160 metres long.
Today's special events are designed to arouse interest in the Olympics around the world and to encourage British fans too.
Many failed to get Olympic tickets in the recent sales process.
According to a new survey for the BBC, 53% of Londoners think the process was "not fair".
But the same survey found support is growing for London 2012. Of the 1,000 people surveyed, 73% said they backed the Games - up from 69% in 2006‘’‘
new_list=list(str_1.split(' '))#以空格键分割str_1中每个单词且强制转换成list

new_1 = set(new_list) #去除list中重复的字符串
for i in new_1: #循环新得到的去重的list,得到i
    #print(i)
    #print(new_list.count(i))
    print(str(i) + '在文章中出现了' + str(new_list.count(i)) + '次') #i在str_1中出现的次数
    if new_list.count(i) > 5: #大于5次出现的字符串
        #print(i)
        #print(new_list.count(i))
        print(str(i) + '在文章中出现了' + str(new_list.count(i)) + '次')

 

输出结果:

support在文章中出现了1次
for在文章中出现了2次
of在文章中出现了1次
69%在文章中出现了1次
wave-like在文章中出现了1次
tickets在文章中出现了1次
"not在文章中出现了1次
special在文章中出现了1次
Many在文章中出现了1次
growing在文章中出现了1次
get在文章中出现了1次
But在文章中出现了1次
Today's在文章中出现了1次
Londoners在文章中出现了1次
has在文章中出现了1次
1,000在文章中出现了1次
prestigious在文章中出现了1次
the在文章中出现了9次
2006.在文章中出现了1次
a在文章中出现了2次
are在文章中出现了1次
roof在文章中出现了1次
they在文章中出现了1次
by在文章中出现了1次
Olympics在文章中出现了1次
failed在文章中出现了1次
According在文章中出现了1次
2012.在文章中出现了1次
was在文章中出现了2次
from在文章中出现了1次
metres在文章中出现了1次
Games在文章中出现了1次
is在文章中出现了2次
160在文章中出现了1次
and在文章中出现了2次
in在文章中出现了3次
Hadid在文章中出现了1次
events在文章中出现了1次
think在文章中出现了1次
BBC,在文章中出现了1次
Olympic在文章中出现了1次
people在文章中出现了1次
survey在文章中出现了2次
fans在文章中出现了1次
sales在文章中出现了1次
same在文章中出现了1次
found在文章中出现了1次
to在文章中出现了4次
process在文章中出现了1次
designed在文章中出现了2次
up在文章中出现了1次
that在文章中出现了1次
encourage在文章中出现了1次
said在文章中出现了1次
Zaha在文章中出现了1次
surveyed,在文章中出现了1次
73%在文章中出现了1次
around在文章中出现了1次
British在文章中出现了1次
-在文章中出现了1次
new在文章中出现了1次
It在文章中出现了1次
Of在文章中出现了1次
interest在文章中出现了1次
London在文章中出现了1次
53%在文章中出现了1次
backed在文章中出现了1次
arouse在文章中出现了1次
world在文章中出现了1次
architect在文章中出现了1次
recent在文章中出现了1次

Process finished with exit code 0

 

the在文章中出现了9次

Process finished with exit code 0


 

 

 

 

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值