Data Types and Methods

First, a list of

By index values

name=["张三","李四","果冻"]
name2=[1,3,2,11,4,8]

# 1.取值和取索引
# print(name[0])  #取值张三,从0开始,如果取值范围超过列表范围会报错

#索引,表示知道数据的内容,查找数据所在的位置
# print(name.index("果冻"))  #如果数据不在列表中,会报错

# 2.修改
# name[1] = "王二"      #超出范围会报错
# print(name)

# 3.增加
# name.append("辜友银") #append 在最后增加元素
# print(name)
# name.insert(1,"奥丁")  #insert 在列表1前面插入数据

# name1=["雷神","宇宙女","绿巨人","奥丁"]
# name.extend(name1)       # exted 添加元素组

# 4.删除
# name.remove("果冻")     # 删除前面第一次出现的数据“果冻”,remove删除指定数据
# #
# name.pop()              # pop,默认情况删除最后一个数据
# name.pop(0)             #指定删除对应列数据
# name.clear()            #清空列表

# 5.del删除,delete简称
# del name[3]              #本质上用来将一个变量从内存中删除
                            #在平时开发中,要用列表删除数据,不要用del删除,
                            #因为del从内存删除后,后续代码将不能在用这个数据


# 6.len,列表中总共包含元素的个数   len函数length 长度 的缩写
# len=len(name)
# print("列表中包含 %d 个元素"%len)

# 7.count 函数方法,统计某元素在列表中出现的次数
# count = name.count("张三")        # count次数
# print("张三在name列表中出现了 %d 次"%count)

# 8.排序 sort()升序,sort(reverse=Ture)降序,reverse()反转,逆序排列
# name2.sort()            # 升序
# print(name2)
# name2.sort(reverse=True)  #降序排列
# name2.reverse()              #逆序
# print(name2)

# 9.查找,in,not in
# a="奥丁" in(name)  # 奥丁是在name中    判断字典时,in只判断字典的key,而不判断值
b="奥丁" not in(name) # 奥丁不是在name中
# print(a)
print(b)
#
# print(name2)
# print(name)

Second, Dictionary

Key values ​​and disorderly

xiaoming = {"name":"小明",
            "sge":18,
            "gender":True,
            "hegiht":1.75,
            "weight":75.5}

1.取值
print(xiaoming["name"])  # 数据不存在报错
2.增加/修改
xiaoming["sex"] ="男"   # 如果key不存在,就是增加键值对
xiaoming["name"] = "小王"  # 如果key存在,就是修改键值对

3.删除
xiaoming.pop("name")
xiaoming.clear()    # 清空
name.remove("果冻")     # 删除前面第一次出现的数据“果冻”,remove删除指定数据

name.pop()              # pop,默认情况删除最后一个数据
name.pop(0)             #指定删除对应列数据
name.clear()            #清空列表

4.del删除,delete简称
del name[3]

5.统计键值对的数量  len
print(len(xiaoming))

6.合并键值对  update
xiaowang = {"hegihtwang":1.65}
print(xiaoming.update(xiaowang))

7.清空 clear
xiaoming.clear()

print(xiaoming)

Triad

#作用:存多个值,对比列表来说,元组不可变(是可以当做字典的key的),主要是用来读

#定义:与列表类型比,只不过[]换成()
age=(11,22,33,44,55)本质age=tuple((11,22,33,44,55))

Fourth, sliced

num = "0123456789"
# 列表元组也可以切片,字典不可以切片
# num[开始位置:结束位置:步长],开始位置=0可以省略,结束位置=末尾可以省略,步长=1可以省略,

# 1.切取2-5的字符串
num1=num[2:6:1]
print(num1)

# 2.切取2-末尾的字符串
num2=num[2:]
print(num2)

# 3.截取从开始-5位置的字符串
num3=num[:6]
print(num3)

# 4.截取完整的字符串
num4=num[:]
print(num)

# 5.从开始位置,每隔一个字符截取字符串(使用步长)
num5=num[::2]
print(num5)

# 6.从索引1开始,每隔一个截取一个
num6=num[1::2]
print(num6)

# 7.截取从2至(末尾-1)的字符串
num[-1]     #-1表示倒数第一的一个数
num7=num[2:-1]
print(num7)

# 8.截取字符串末尾的两个数
num8=num[-2:]
print(num8)

# 9.字符串的逆序(面试)从右向左截取
num9=num[::-1]  # 表示从最后一个数开始,每截取一个数向左移动一位
print(num9)

V. tuples and lists conversion

# 列表可以修改(安全系数低),元组别人不可以修改(安全系数高)
# zu=(),    biao=[],    括号不同

# list(元组),元组转化列表
# tuple(列表),列表转化元组

# 1.修改前:list列表类型
biao=[1,2,3,4]
print(type(biao))
    # 修改后:tuple元组类型
biao1=tuple(biao)
print(type(biao1))

# 2.修改前:tuple元组类型
zu=(1,2,3,4)
print(type(zu))

# 3.修改后:list列表类型
zu1=list(zu)
print(type(zu1))

Six, string alignment

1. centering center

poem = ["你是猪",
        "辜友银",
        "你是个笨猪",
        "你是个大笨猪",
        "你是个超级大笨猪"]
        
 for poem_str in poem:
    print("|%s|" % poem_str.center(20," "))   # 居中对齐,其余地方用空格补全
    # 结果:
|        你是猪         |
|        辜友银         |
|       你是个笨猪        |
|       你是个大笨猪       |
|      你是个超级大笨猪      |
    

2. Left ljust

for poem_str in poem:
    print("|%s|" % poem_str.ljust(20, "0"))   # 左对齐,其余地方0补全
# 结果
|你是猪00000000000000000|
|辜友银00000000000000000|
|你是个笨猪000000000000000|
|你是个大笨猪00000000000000|
|你是个超级大笨猪000000000000|

3. Align Right rjust

for poem_str in poem:
    print("|%s|" % poem_str.rjust(20,"#"))  # 右对齐,其余地方#补全
# 结果:
|#################你是猪|
|#################辜友银|
|###############你是个笨猪|
|##############你是个大笨猪|
|############你是个超级大笨猪|
    

Seven small way

1.findall (text search return):

1. Find the string of content that matches the regular
2. returns a list, the list is being matched content is expressed

res = re.findall('a','asd jeff ball')
res1 = re.findall('[a-z]+','asd jeff ball')
print(res1)

2.search (look from top to bottom):

1. returns an object, you must call the group to see the results
according to a regular look, as long as found in the results will not look back
3. When the search results does not exist (return None), and then call the group directly error, because there is no group none method

res2 = re.search('a','asd jeff ball')
if res2:
print(res2.group())

3.match (matching beginning):

1.match start of the string
2. not have the same return None
3. The same group call error

res3 = re.match('a','asd jeff ball')
if res3:
print(res3.group())

4.split (Cutting):

ret = re.split ( '[ab] ', 'abcd') # press the 'a' obtained by dividing 'and' bcd ', of the' and 'BCD' by 'b' are divided
print (ret) # [ '', '', ' cd']

str = "abcd,ssdhu,123";
print str.split( );       # 以空格为分隔符,包含 \n
print str.split(' ,', 1 ); # 以逗号为分隔符,分隔成两个

5.sub (replacement)

ret = re.sub ( '999', 'H', 'jeff age max 999 8884', 2) # digital replaced 'H', the parameter 1 replaces only a

print (right) # evaHegon4yuan4

6.subn (Alternatively, returns a tuple)

ret = re.subn ( '\ d' , 'H', 'eva3egon4yuan4') # digital replaced 'H', returns a tuple (a result of the replacement, the replacement how many times)
Print (RET)

7.compile (Regular encapsulated as objects)

obj = re.compile ( '\ d { 3}') # regular expression compiled into a regular expression object, the rule is to be matched 3 digits
ret = obj.search ( 'abc123eeee') # regular expression object call search, the parameters to be matched string
print (ret.group ()) # results: 123

8.finditer (iteration)

ret = re.finditer ( '\ d' , 'ds3sy4784a') #finditer returns an iterator stored matching results
Print (RET) # <Object callable_iterator AT 0x10195f940>
Print (Next (RET) .group ()) See section # one result of
print (next (ret) .group ( )) # results View second
print ([i.group () for i in ret]) # view about the remaining results

9. Packet

res = re.search('^[1-9]\d{14}(\d{2}[0-9x])?$',110105199812067023
print(res)

print (res.group (1)) # get the regular expression parentheses broad grouping together the contents of
print (res.group (2)) # search and match support the acquisition operation of the packet content has nothing to do with the regular mechanism is python

10. Since Alias:

re.search('^[1-9]?P (\d{14})(\d{2}[0-9x])?$',110105199812067023)
print(res.group(passwd))

Guess you like

Origin www.cnblogs.com/guyouyin123/p/11373691.html