2019 6.30学习笔记

对一个列表里的字典元素进行value值提取并组成新的一个列表
 data=[
   {"city":"北京","min":2},
   {"city":"天津","min":5} ,     
   {"city":"石家庄","min":9}
 ]
 cites=list(map(lambda x:x['city'],data))
 #在Python2里,直接使用map就可以打印结果,但是在Python3里,map返回的结果是迭代器(iterator),因此需要先转换为列表list,再print输出
 #上面的一行代码等价于
 # cites=[]
 # for city_temp in data:
 # city=city_temp["city"]
 # cites.append(city)

 print(cites)
运行结果:  
['北京', '天津', '石家庄']

正则表达式
单字符匹配规则
import re
# 1.匹配某个字符串
# text="hello"
# ret=re.match("he",text)
# print(ret.group())

# 2.点:匹配任意的字符
# text="joker"
# ret=re.match(".",text)
# print(ret.group())

# 3.\d:匹配任意的数字(0-9)     \D:匹配任意的非数字
# text="1997joker"
# ret=re.match("\d",text)
# print(ret.group())

# 4.\s:匹配空白字符(\n,\t,\r,空格)
# text="\r"
# ret=re.match("\s",text)
# print(ret.group())

# 5.\w:匹配a-z,A-Z,数字和下划线    \W跟\w正好相反
# text="+"
# ret=re.match("\w",text)
# print(ret.group())

#6.[]组合的方式,只要满足中括号里的字符,就可以匹配
#text="0732-88888888asdf"
#ret=re.match("[\d\-]+",text)
#print(ret.group())

多字符匹配原则

import re
# 1. *:可以匹配0或者任意多个字符
# text="2123abcd12"
# ret=re.match('\d*',text)
# print(ret.group())

# 2. +:可以匹配一个或者多个字符
# text="2123abAFWd1+2"
# ret=re.match('\w+',text)
# print(ret.group())

# 3. ?:可以匹配一个或者0个字符(要么没有要么就只有一个)
# text="2123abAFWd12"
# ret=re.match('\w?',text)
# print(ret.group())

# 4. {m}:可以匹配m个字符   {m-n}匹配m到n个字符
# text="2123abAFWd1+2"
# ret=re.match('\w{8}',text)
# print(ret.group())

 









posted on 2019-06-30 17:29  Joker乔柯  阅读(62)  评论(0编辑  收藏  举报

导航