In [2]:
s = ' hello, world!'
print(s.strip())
print(s.lstrip(' hello, '))
print(s.rstrip('!'))
In [3]:
sStr1 = 'strcat'
sStr2 = 'append'
sStr1 += sStr2
print(sStr1)
In [7]:
# 找不到报错,用find代替
sStr1 = 'strchr'
sStr2 = 'r'
nPos = sStr1.index(sStr2)
print(nPos)
print(sStr1.index('o'))
In [9]:
#python 3不再有cmp,用operator
import operator
a = 'strchr'
b = 'strch'
print(operator.lt(a,b))
print(operator.le(a,b))
print(operator.eq(a,b))
print(operator.ne(a,b))
print(operator.gt(a,b))
print(operator.ge(a,b))
print(operator.lt(a,b))
print(cmp(sStr2,sStr1))
In [11]:
sStr1 = 'JCstrlwr'
sStr1 = sStr1.upper()
sStr2 = sStr1.lower()
print(sStr1)
print(sStr2)
In [14]:
#字符串为不可变类型
s1 = 'hello,world!'
s2 = s1[0:7:2] #2为步长
s3 = s1[1:3]
print(s2)
print(s3)
In [15]:
sStr1 = 'abcdefg'
sStr1 = sStr1[::-1] #通过反向步进切片
print(sStr1)
In [16]:
#查找字符和字符串用find,index找不到会报错
sStr1 = 'abcdefg'
sStr2 = 'cde'
print(sStr1.find(sStr2))
print(sStr1.find('f'))
In [18]:
sStr1 = 'ab,cde,fgh,ijk'
sStr2 = ','
sStr1 = sStr1[sStr1.find(sStr2) + 1:]
print(sStr1)
s = 'ab,cde,fgh,ijk'
print(s.split(','))
In [40]:
from collections import Counter
s = 'ABaacbdbccdeeffg123klmn'
s = [x for x in s.lower() if x.isalpha()]
print(s)
count = Counter(s)
print(count)
print(type(count))
print(count.keys())
print(count.values())
print(count.items())
m = max(count.values())
print(m)
result_list = [x for (x,y) in count.items() if y==m]
print(result_list)
print(sorted(result_list))
In [41]:
#version 1
import re
from collections import Counter
def get_max_value_v1(text):
text = text.lower()
result = re.findall('[a-zA-Z]', text) # 去掉列表中的符号符
print(result)
count = Counter(result) # Counter({'l': 3, 'o': 2, 'd': 1, 'h': 1, 'r': 1, 'e': 1, 'w': 1})
count_list = list(count.values())
max_value = max(count_list)
max_list = []
for k, v in count.items():
if v == max_value:
max_list.append(k)
max_list = sorted(max_list)
return max_list
print(get_max_value_v1('ABaacbdbccdeeffg123klmn'))
In [20]:
#version 2
from collections import Counter
def get_max_value(text):
count = Counter([x for x in text.lower() if x.isalpha()])
m = max(count.values())
return sorted([x for (x, y) in count.items() if y == m])[0]
In [42]:
#version 3
import string
def get_max_value(text):
text = text.lower()
return max(string.ascii_lowercase, key=text.count)
print(get_max_value('ABaacbdbccdeeffg123klmn'))
In [50]:
"""
max(iterable, key, default) 求迭代器的最大值,其中iterable 为迭代器,
max会for i in … 遍历一遍这个迭代器,然后将迭代器的每一个返回值当做参数传给key=func 中的func(一般用lambda表达式定义) ,
然后将func的执行结果传给key,然后以key为标准进行大小的判断。
"""
max(range(5),key = lambda x : x>3)
Out[50]:
In [51]:
max(range(6), key = lambda x : x>2)
# >>> 3
# 带入key函数中,各个元素返回布尔值,相当于[False, False, False, True, True, True]
# key函数要求返回值为True,有多个符合的值,则挑选第一个。
max([3,5,2,1,4,3,0], key = lambda x : x)
# >>> 5
# 带入key函数中,各个元素返回自身的值,最大的值为5,返回5.
max('ah', 'bf', key=lambda x: x[1])
# >>> 'ah'
# 带入key函数,各个字符串返回最后一个字符,其中'ah'的h要大于'bf'中的f,因此返回'ah'
max('ah', 'bf', key=lambda x: x[0])
# >>> 'bf'
# 带入key函数,各个字符串返回第一个字符,其中'bf'的b要大于'ah'中的a,因此返回'bf'
text = 'Hello World'
max('abcdefghijklmnopqrstuvwxyz', key=text.count)
# >>> 'l'
# 带入key函数,返回各个字符在'Hello World'中出现的次数,出现次数最多的字符为'l',因此输出'l'
Out[51]:
In [52]:
#T h e M i s s i s s i p p i R i v e r
#[1, 1, 2, 2, 1, 5, 4, 4, 5, 4, 4, 5, 2, 2, 5, 2, 1, 5, 1, 2, 1]
s = 'The Mississippi River'
s = s.lower()
map_result = list(map(s.count,s))
print(map_result)
print(max(map_result))
In [ ]: