python3标准库书评_python3 标准库一些总结(1)

1,统计个数(字符串,列表等)或者初始化字典,输出一个包含键和计数的字典或提供一个元素序列,还可以使用关键字参数讲字符串名映射到计数。

模块:collections

构造函数: Counter

importcollections

text1= "asbgewgrg2121aaassbsbgeeeegwwrr"c=collections.Counter(text1)print(c)print(collections.Counter({'a':3,'b':2}))print(collections.Counter(a=6, b=5))##########输出如下#######

Counter({'g': 5, 'e': 5, 'a': 4, 's': 4, 'b': 3, 'w': 3, 'r': 3, '2': 2, '1': 2})

Counter({'a': 3, 'b': 2})

Counter({'a': 6, 'b': 5})

View Code

2,多行合并

sample_text = """Ridiculously fast.

Django was designed to help developers take applications from concept to completion as quickly as possible.

Reassuringly secure.

Django takes security seriously and helps developers avoid many common security mistakes.

Exceedingly scalable.

Some of the busiest sites on the Web leverage Django’s ability to quickly and flexibly scale."""

importtextwrapprint(textwrap.fill(sample_text,width=50))#dedented_text =textwrap.dedent(sample_text)print('Dedented:')print(dedented_text)#dedented2_text =textwrap.dedent(sample_text).strip()for width in [45, 60]:print('{} Columns:\n'.format(width))print(textwrap.fill(dedented2_text, width=width))print()#dedented3_text =textwrap.dedent(sample_text)

wrapped= textwrap.fill(dedented3_text,width=50)

wrapped+= '\n\nSecond garagraph after a blank line.'final= textwrap.indent(wrapped,'>')print('Quoted block:\n')print(final)

View Code

3,re

importre

pattern= "this"text= "Does this text match the pattern?"match=re.search(pattern, text)print(type(match))

s=match.start()

e=match.end()print('Found "{}"\nin "{}"\nfrom {} to {} ("{}")'.format(match.re.pattern, match.string, s, e, text[s:e]))#print("#" * 20+str(2) + "#" * 20)#2

importre

regexes=[

re.compile(p)for p in ['this', 'that']

]

text= 'Does this text match the pattern?'

print('text: {!r}\n'.format(text))for regex inregexes:print('Seeking "{}" ->'.format(regex.pattern), end=' ')ifregex.search(text):print('match!')else:print("no match")print("#" * 20+str(3) + "#" * 20)#3

importre

text= 'abbaaabbbbaaaaa'pattern= 'ab'

for match inre.findall(pattern,text):print('Found {!r}'.format(match))print("#" * 20+str(3.2) + "#" * 20)for match inre.finditer(pattern,text):

s=match.start()

e=match.end()print('Found {!r} at {:d}:{:d}'.format(text[s:e], s, e))print("#" * 20 + str(4) + "#" * 20)################输出#####################

Found"this"

in "Does this text match the pattern?"

from 5 to 9 ("this")####################2####################

text: 'Does this text match the pattern?'Seeking"this" ->match!

Seeking"that" ->no match####################3####################

Found 'ab'Found'ab'

####################3.2####################

Found 'ab' at 0:2Found'ab' at 5:7

View Code

4,查找单个字符串在某个序列中的计数,但不支持单个单词在string中查找,可以在列表或元祖中查找计数;

模块:collections

构造函数: Counter

importcollections#1

d = collections.Counter("abcdaafbbdd")for letter in 'abcde':print('{} : {}'.format(letter, d[letter]))#2

m = collections.Counter(["this","a","b"]) #()元组 ,序列

for letter in {'this'}: #[] 列表

print('{} : {}'.format(letter, m[letter]))#3 不支持字符串单词

text2 = "hello ,this is a tree ,this ,that..."m2=collections.Counter(text2)for letter in {'this'}:print('{} : {}'.format(letter, m2[letter]))###################输出#################

a : 3b :3c :1d :3e : 0

this :1this : 0

View Code

5,elements() 方法返回一个迭代器,返回所有元素,不能保证元素顺序不变,小于或等于0的元素不包含在内。

importcollections

c= collections.Counter('extremely')print(type(c))

c['z']=0print(c)print(type(c.elements()))print(list(c.elements()))################输出################

Counter({'e': 3, 'x': 1, 't': 1, 'r': 1, 'm': 1, 'l': 1, 'y': 1, 'z': 0})['e', 'e', 'e', 'x', 't', 'r', 'm', 'l', 'y']

View Code

6,统计系统字典内所有单词中出现字母,以一个频度分布,然后打印(3条最常见的),如果不向most_common()提供参数,则会生成所有元素构成的一个列表。

#cat /tmp/words

a

aaa

abc

ace

b

bb

bca

ce

coow

ccc

ddw

wer########cat collections_counter.py

importcollections

c=collections.Counter()

with open('/tmp/words','rt') as f:for line inf:

c.update(line.rstrip().lower())print('MOst common:')for letter, count in c.most_common(3):print('{}:{:>7}'.format(letter,count))########cat collections_counter.py

importcollections

c=collections.Counter()

with open('/tmp/words','rt') as f:for line inf:

c.update(line.rstrip().lower())print('MOst common:')for letter, count inc.most_common():print('{}:{:>7}'.format(letter,count))#################输出############

MOst common:

c:8a:7b:5

####

MOst common:

c:8a:7b:5e:3w:3o:2d:2r:1

View Code

##########################################

blog : https://pymotw.com/3/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值