python找出字符串中字母出现的次数,有重复时的情况

作者&投稿:訾促 (若有异议请与网页底部的电邮联系)
python怎么判断字符串中出现次数最多的字母~

class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ lenS = len(s); maxLen = 0; left = right = 0; charDict = {}; while right < lenS: if s[right] in charDict and left <= charDict[s[right]]: #出现重复的字符且这个字符在窗口中 left = charDict[s[right]] + 1; #左窗口位置调整到这个重复字符的右端 else: #没有重复字符出现 maxLen = max(maxLen,right - left + 1); #更新最长长度 charDict[s[right]] = right; #更新字符位置 right += 1; #右窗口后移 return maxLen;

代码如下:

【备注】:
1. 用str.split(',')只能分隔逗号一种;如果涉及到多重分隔的话就需要使用re.split(',|:')。
2. 原字符串以逗号分隔的,后面有一个或多个字符串,所以re.split(', | ')。
3. 执行re.split(r', | ', S)操作之后,列表中会产生大量的'',就需要将filter过滤掉。
4. 使用L.count(x) == 1 或者 L.count(x) > 1来保留重复项或,非重复项。
5. set(L)则是保留列表中的唯一项,再用list()将其转换为列表。
6. 使用', '.join(L),将列表拼接成我们想要的字符串。

代码如下:

dic=dict()

d={}

s=set()

s='helloworld'

(1)d=dict()

    for x in s:

      if x not in d.keys():

          d[x]=1

       else:

         d[x]=d[x]+1

    print(d)

(2)d2=dict()

    for x in s:

      d2[x]=d2.get(x,0)+1

    print(d2)

(3)d3=dict()

   for x in s:

      d3[x]=s.count(x)

   print(d3)

扩展资料

字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。

创建字符串很简单,只要为变量分配一个值即可。例如:

var1 = 'Hello World!'

var2 = "Python Runoob"

Python 访问字符串中的值

Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用。Python 访问子字符串,可以使用方括号来截取字符串,如下实例:

实例(Python 2.0+)

#!/usr/bin/python 

var1 = 'Hello World!'

var2 = "Python Runoob" 

print "var1[0]: ", var1[0]

print "var2[1:5]: ", var2[1:5]

以上实例执行结果:

var1[0]:  H

var2[1:5]:  ytho




用python怎么实现,找出一个字符串中的重复字符子串和字符串数量?
代码如下:【备注】:1. 用str.split(',')只能分隔逗号一种;如果涉及到多重分隔的话就需要使用re.split(',|:')。2. 原字符串以逗号分隔的,后面有一个或多个字符串,所以re.split(', | ')。3. 执行re.split(r', | ', S)操作之后,列表中会产生大量的'',就需要将filter过滤掉。4. ...

python中如何取出字符串中最后一个特定字符后的字符串
在a中查找最后一个b后面的字符 a="ABCDEFGHIJABCDEFGHIJABCDEFGHIJ"b="H"print(a[a.rfind(b):])或:str = "addhakshdaskhdsak---12345.0"str.find("12345.0") 找到"12345.0"所在位置的index str[str.find("12345.0") - 1]即是需要的字符 ...

python 怎么在列表中查找含有某个字符?
In [3]: data=['qwerfyy','dsfha','fdyythg','efggg']In [4]: [i for i in data if 'yy' in i]Out[4]: ['qwerfyy', 'fdyythg']In [5]: import re In [6]: [i for i in data if re.search('yy',i)]Out[6]: ['qwerfyy', 'fdyythg']

Python新手求助,如何查找一个包含指定字符的字符串
l = ['132','135','141','121','241377','1111322']for i in l: if i[:2] == '13': print i

如何用python找出字符串中出现最多的字符
for index in range(len(inputstr)):if inputstr [index] in dict:Dict[inputstr[index]] +=1 Else:Dict[inputstr[index]] =1 然后对这个字典用key排序就出来了。sorted(dict.items(),key = lambda x:x[1],reverse = True)第一个就是了。

python字符串操作
六、查找与替换子串 调用find方法可以判断是否包含某个子串,比如以下代码会输出" python contains th" 和 " python doesn't contain he":调用replace方法可以对字符串进行替换,比如要把"hello world"中的”hello“替换为”world“,以下代码会输出:”world world“七、分隔字符串 如果我们要把一句话...

请教,这个怎么用python找出2个字符串的不同,按差异顺序输出?
def find(a, b):list1 = []lena = len(a)lenb = len(b)n = 0for i in range(lena):for j in range(n, lenb):if a[i] == b[j]:n += 1breakif a[i] != b[j]:list1.append(b[j])n = j + 1continueprint(list1)seq1 = 'ABCrist_H'seq2 = 'ABC_arist_b_02...

python中找出本文中包含“hello”字符的行,并统计出对应的行号_百度知 ...
i=1 for line in open('a.txt'):if line.find('hello')>0:print 'find hello in %s row:'%i i=i+1 --- a.txt asfdasfashelloasdasd asfdasfashelloasdasd asfdasfashelloasdasd

python 如何连续查找字符串
python的字符串可以看做是数组的 所以比如mystr = "what is your name"newstr = mystr if newstr.find("a") >= 0:newstr = newstr[newstr.find("a"):] #这是后newstr就是第一个a开始之后的字符串 如果不需要包含第一个找到的a,那么可以这样:while newstr.find("a") >= 0:ne...

python如何获取一串字符中的几位字符
python获取一串字符中的几位字符的方法:使用“字符串名[开始位:结束位]”语句对字符串进行切片,通过更改开始位以及结束位的数值,取任意数量的字符 示例如下:取字符串从第一位到第二位的值 执行结果:更多Python知识,请关注:Python自学网!!

信丰县14773959633: python 编写程序 对用户输入的英文字符串中出现的英文字母进行提取 急求解??? -
蒙梦奥德: 题主你好, 代码及测试截图如下:a.输出为小写字母: ----- b.输出为大写字母: ----- c.其它例子: 写在最后: 上面给的代码差别就在于upper()函数及lower()函数, 而输入没啥限制, 光字母,或汉字字母混着等都没问题. 但代码写的不是太好,只是将基本的逻辑实现了希望可以帮到题主, 欢迎追问

信丰县14773959633: 在Python中,如何从键盘输入字符串,找出里面的字母,不区分大小写,重复的只输出? -
蒙梦奥德: s = input().lower()result = [[e, s.count(e)] for e in set(list(s))]print(result)

信丰县14773959633: 怎么用python统计字符串中每个字符出现的次数 -
蒙梦奥德: python统计字符串中指定字符出现的次数,例如想统计字符串中空格的数量 s = "Count, the number of spaces." print s.count(" ") x = "I like to program in Python" print x.count("i")

信丰县14773959633: 请问下用Python怎样找出2个字符串中相同的字母,然后按顺序列出来.比如'abcd''bcfg' - 》'bc' -
蒙梦奥德: 飘过 def strIntersection(str1, str2): tmp = [ch for ch in str1 if ch in str2] res = [] for i in tmp: if i not in res: res.append(i) return "".join(res)

信丰县14773959633: python怎么判断字符串中出现次数最多的字母 -
蒙梦奥德: class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ lenS = len(s); maxLen = 0; left = right = 0; charDict = {}; while right < lenS: if s[right] in charDict and left <= charDict[s[right]]: #出现重复的字符且这个字...

信丰县14773959633: 如何在Python字符串列表中查找出指定字符所在字符串 -
蒙梦奥德: re.findall('\\w*{}\\w*'.format(c),','.join(l)) Python 3.5.2 (default, Dec 7 2016, 23:38:49)[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import re>>> l=['sfdsd','ddff']...

信丰县14773959633: python怎么查找字符串里最大的字母 -
蒙梦奥德: string = 'abcdefg' print max(list(string))

信丰县14773959633: 查找指定字母在某字符串中出现的次数,字符串和字母为参数,以函数的形式编写本程序 -
蒙梦奥德: //Cint find(char str[],char ch){ int ret = 0; for(int i = 0;i if(str[i]==ch) ret++; return ret;}//C++int find(string str,char ch){ int ret = 0; for(int i = 0;i if(str[i]==ch) ret++; return ret;}//javaint find(String str,char ch){ int ret = 0; for(int i = 0;i if(str.charAt(i)==ch) ret++; ...

信丰县14773959633: python:从一个字符串中找出该字符最后一次出现的下标.我写的哪里错了? -
蒙梦奥德: def Index(str1, c): icount = -1 for char in str1: if char == c: icount = char return icount 这样才对,一直更新找到字符的下标并存储到icount中,直到最后一个字符,如果找不到就返回-1

信丰县14773959633: 大牛们好,我问题是用python读取一个文件,文件里有很多行字符串.需要把所有连续的字母提取出来 -
蒙梦奥德: t='''Now is the time time^%$# is time-is%?"time''' import re a=re.findall("\w+",t); d={k:a.count(k) for k in dict(zip(a,[0]*len(a))).keys()} print(list(d.keys())) print(d)============= ['is', 'Now', 'the', 'time'] {'is': 3, 'Now': 1, 'the': 1, 'time': 4}

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网