• python_统计字符串个数


    #取出字符串中出现2次的字符串,使用count方法统计
    def
    two_zifuchuan(str): s=set() for i in str: if str.count(i)==2: s.add(i) return s #取出字符串中出现2次的字符串,使用字典统计 def two_occur(str): s={} for i in str: if i in s.keys(): s[i]+=1 else: s[i]=1 return [i for i in s if s[i]==2] str="dddredddddewws22dff43" print(two_zifuchuan(str)) print(two_occur(str))

    #统计数组中每个值的个数并打印且不能用count和字典,且时间换空间
    li=[1,2,3,4,5,5,5,1,3,2,1] #数组
    x=0
    last=sorted(li)[0] #排序后第一个值
    for i,j in enumerate(sorted(li)): #遍历排序数组
    if j==last: #假如当前遍历数组值和上一个值一样
    x+=1 #个数加1
    else:
    print("%s的次数是:%s" % (last,x)) #当前遍历数组和上一个值不同,输出值及个数
    x=1 #个数归1
    last=j #当前值遍历给last

    print("%s的次数是:%s" % (last, x))#输出数组最后一个值的个数

  • 相关阅读:
    coder的脚印
    Mysql
    MSDos
    Windows Develop
    Eclipse 使用总结
    DBA常用SQL
    SSH总结
    Unity3D协程
    yield的作用
    UGUI优化
  • 原文地址:https://www.cnblogs.com/xiaokuangnvhai/p/11465194.html
Copyright © 2020-2023  润新知