加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python3 标准库一些总结

发布时间:2020-12-20 12:43:36 所属栏目:Python 来源:网络整理
导读:? 1,统计个数(字符串,列表等)或者初始化字典,输出一个包含键和计数的字典或提供一个元素序列,还可以使用关键字参数讲字符串名映射到计数。 模块:collections? 构造函数: Counter? import collectionstext1 = " asbgewgrg2121aaassbsbgeeeegwwrr " c

?

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

模块:collections?

构造函数: Counter?

import collections
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.
"""

import textwrap
print(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 += nnSecond garagraph after a blank line.
final = textwrap.indent(wrapped,>)
print(Quoted block:n)
print(final)
View Code

3,re

import re

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
import re
regexes = [
    re.compile(p) for p in [this,that]
]
text = Does this text match the pattern?
print(text: {!r}n.format(text))

for regex in regexes:
    print(Seeking "{}" ->.format(regex.pattern),end= )
    if regex.search(text):
        print(match!)
    else:
        print("no match")

print("#" * 20+str(3) + "#" * 20)
#3
import re
text = abbaaabbbbaaaaa
pattern = ab
for match in re.findall(pattern,text):
    print(Found {!r}.format(match))
print("#" * 20+str(3.2) + "#" * 20)
for match in re.finditer(pattern,text):
    s = match.start()
    e = match.end()
    print(Found {!r} at {:d}:{:d}.format(text[s:e],e))
print("#" * 20 + str(4) + "#" * 20)
################输出#####################
<class re.Match>
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:2
Found ab at 5:7
View Code

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读