Python-base List Collection Dictionary file processing

Source: Internet
Author: User

Directory

    1. list, tuple Operations
    2. Dictionary operations
    3. Collection operations
    4. File operations

1. list, tuple Operations

 Lists are one of the most commonly used data types and can be easily stored, modified, and manipulated by the List.

Definition List

names = ["zhang san", "john doe", "pock"]

The data in the list is taken by Subscript. subscript starts at 0

names = ["zhang san", "john doe", "pock"]print (names[0]) Zhang San print (names[1]) John Doe print (names[2]) Pock note: You can also rewind print (names[-3]) Zhang San print ( Names[-2]) John Doe Print (names[-1]) pock

Slices: take multiple elements (Gu Tou regardless of tail.)

names = ["zhang san", "john doe", "pock", "madman", "fool"]print (names[0:2]) [' Zhang San ', ' John Doe ']print (names[:3]) [' Zhang San ', ' John Doe ', ' pock ']print (names[1:]) [' John Doe ', ' pock ', ' lunatic ', ' fool ']print (names[0::2]) [' Zhang San ', ' pock ', ' fool ']print (names[::2]) [' Zhang San ', ' pock ', ' fool ']

Append (added in Last One)

names = ["zhang san", "john doe", "pock", "madman", "fool"]names.append ("SB") print (names) <br>[' Zhang San ', ' John Doe ', ' pock ', ' lunatic ', ' fool ', ' SB ']

Insert (insert Anywhere)

names = ["zhang san", "john doe", "pock", "madman", "fool"]names.insert (2, "old Cold Legs") names.insert (5, "bigfoot") print (names) [' Zhang San ', ' John Doe ', ' old cold legs ', ' pock ', ' Madman ', ' Bigfoot ', ' fool ', ' SB '

Modify

names = ["zhang san", "john doe", "pock", "madman", "fool"]names[2] = ("leper") print (names) [' Zhang San ', ' John Doe ', ' leper ', ' lunatic ', ' fool ']

Delete

#指定删除下标对应的names = ["zhang san", "john doe", "pock", "madman", "fool"]del  names[4]print (names) [' Zhang San ', ' John Doe ', ' pock ', ' Madman '] # Use Remove to delete only the first occurrence of names = ["zhang san", "john doe", "john doe", "pock", "madman", "fool"]names.remove ("john Doe") print (names) [' Zhang San ', ' John Doe ', ' pock ', ' madman ' , ' fool '] #使用pop删除时删除列表中的最后一个值names = ["zhang san", "john doe", "john doe", "pock", "madman", "fool"]names.pop () print (names) [' Zhang San ', ' John Doe ', ' John Doe ', ' Pock ', ' Madman ']

Copy (the Copy Here is a shallow copy. means only the first layer is Copied)

names = ["zhang san", "john doe", "john doe", "pock", "madman", "fool"]names_copy = names.copy () print (names_copy) [' Zhang San ', ' John Doe ', ' John Doe ', ' pock ', ' lunatic ', ' fool ']

Statistics

names = ["zhang san", "john doe", "john doe", "pock", "madman", "fool"]print ("john Doe occurrences:", names.count ("john Doe")) John Doe number of Occurrences: 2

Sort

names = ["zhang san", "john doe", "john doe", "pock", "madman", "fool", "3", "2"]names.sort () print (names) [' 2 ', ' 3 ', ' fool ', ' Zhang San ', ' John Doe ', ' John Doe ', ' pock ', ' lunatic ']
#3.0 the different data types cannot be sorted together

Get subscript

names = ["zhang san", "john doe", "john doe", "pock", "madman", "fool", "1", "3", "2"]print ("john doe subscript is:", names.index ("john doe")) John Doe subscript is: 1 <br># Returns only the first subscript found

2. Dictionary operation

A dictionary of Key-value data types.

K How to call when you define the Call.

Key definition Rules: 1. immutable: numbers, strings, tuples (mutable: lists, dictionaries). K unique (if the K definition repeats then fetch only the last Occurrence)
V definition rule: any type
Dictionaries: defining symbols (), exactly the same as the list, except that elements within tuples are not mutable
Grammar:
DIC = {name: "yuhl", age=18,qq=2878****} attribute: unordered     key must be Unique.

Increase

DIC = {"name": "yuhl", "age": "+", "QQ": "2878****"}dic["phone"] = "the" print (dic) #增加 {' phone ': ' ' ', ' age ': ' "qq": ' 2878**** ', ' name ': ' Yuhl '} dic00 = {"name": {"yuhl": {"age", "qq"}, "qq": "111"}, "age": 18}dic00.setdefault (" Gender "," Zhang ") #增加, random print (dic00)

Modify

DIC = {"name": "yuhl", "age": "+", "QQ": "2878****"}dic ["name"] = "yuhonglin" print (dic)      note: The K value cannot be modified. Can only modify value{' qq ': ' 2878**** ', ' age ': ' + ', ' name ': ' yuhonglin '} dic00 = {"name": {"yuhl": {"age", "qq"}, "qq": "111"}, "age" : 18}dic00.update (name= "yuhonglin", gender= "name") #修改和新增print (dic00) {' age ': ' gender ': ' name ', ' name ': ' Yuhonglin ' }

Delete

#删除dic = {"name": "yuhl", "age": "+", "qq": "2878****"}del dic["name"]print (dic) {' QQ ': ' 2878**** ', ' age ': ' + '} #标准删除dic = {"name": "yuhl", "age": "+", "qq": "2878****"}dic.pop ("name") print (dic) {' age ': ' + ', ' qq ': ' 2878**** '} #随机删除dic = {"name ":" Yuhl "," Age ":" + "," QQ ":" 2878**** "}dic.popitem () print (dic) try several times to have effect

Find

DIC = {"name": "yuhl", "age": "+", "QQ": "2878****"}print ("print The result is:", dic.get ("name"))       #获取 The print result is: Yuhl      dic = {"name ":" Yuhl "," Age ":" all "," QQ ":" 2878**** "}print (" Print the result is: ", dic[" name "])           #与之前的一样 the result is: yuhldic = {" name ":" Yuhl "," Age ": "2878****", "QQ": "}print" ("print result is:", "stu1102" in Dic) the result is: False                            #在字典中有返回True否则返回False  dic = {"name": "yuhl "," Age ":" 2878**** "," QQ ":"}print "(" print result is: ", dic[" stu1105 "])         #如果一个key不存在会报错. Get does not return directly to none

Other methods:

DIC00 = {"name": "yuhl", "age": 18}dic00.clear ()         #清空print (dic00) The information promised is: {} dic00 = {"name": {"yuhl": {"age", "QQ"}, " QQ ":" 111 "}," Age ": 18}dic000002= Dic00.fromkeys ([" a "," b "],1)        #快速生成字典print (dic000002) automatically generated dictionary is: {' b ': 1, ' a ': 1}   dic00 = {"name": {"yuhl": {"age", "qq"}, "qq": "111"}, ' age ': 18}for k,v in Dic00.items ():    print (k,v)                  #列表打印                     print (k[0],v)              #列表打印 dic000 = {"name": "yuhl", "age":, "qqanddianhua": [110,133]}dic00new = dic000.copy () #拷贝. Shallow copy print (dic00new, "\ n", dic000) dic00 = {"name": {"yuhl": {"age", "qq"}, "qq": "111"}, "age": +} dic00["qqanddianhua" ]= "11222", "111111" #拷贝print ("\ n", dic00) {' age ':, ' Qqanddianhua ': (' 11222 ', ' 111111 '), ' name ': {' QQ ': ' 111 ', ' Yuhl ': {' Age ', ' QQ '}}
3. Collection

Functions: deduplication and relational operations (sets: intersection, difference set, and set) other methods:

#交集python_set = {"alex", "lhf", "zhangsan", "lisi", "wangmazi"}linux_set = {"alex", "lhf", "yuhl", "dsd"}print (python_set &linux_set) #求两个集合里面出现相同的数print (python_set.intersection (linux_set)) print (python_set&linux_set) print (py Thon_set.intersection (linux_set)): {' lhf ', ' Alex '}: {' lhf ', ' Alex '}: {' lhf ', ' Alex '}: {' lhf ', ' alex '}python_set = {' al Ex "," LHF "," Zhangsan "," lisi "," Wangmazi "}linux_set = {" Alex "," lhf "," Yuhl "," DSD "}<br> #并集print (python_set| Linux_set) #去重复print (python_set.union (linux_set)) #去重复: {' lhf ', ' Yuhl ', ' DSD ', ' Alex ', ' Wangmazi ', ' Lisi ', ' Zhangsan ' }: {' lhf ', ' Yuhl ', ' DSD ', ' Alex ', ' Wangmazi ', ' Lisi ', ' Zhangsan '} # #差集python_set = {"alex", "lhf", "zhangsan", "lisi", "w Angmazi "}linux_set = {" Alex "," lhf "," Yuhl "," DSD "}print (python_set-linux_set) #去掉重复的print (python_set.difference ( Linux_set)) print (linux_set-python_set) #去掉重复的print (linux_set.difference (python_set)) {' Lisi ', ' Zhangsan ', ' Wangmazi '} {' DSD ', ' Yuhl '} #并集python_set = {"alex", "lhf", "zhangsan", "lisi","wangmazi"}linux_set = {"alex", "lhf", "yuhl", "dsd"}print (python_set|linux_set) #去重复 (prints Two duplicate Data. merged together) print (python _set.union (linux_set)) #去重复 (print Two duplicate Data. merged together) #d对称差集python_set = {"alex", "lhf", "zhangsan", "lisi", "wangmazi"}linux _set = {"alex", "lhf", "yuhl", "dsd"}print (python_set^linux_set) #把共有的地方去掉就是对称差集print (python_set.symmetric_    Difference (linux_set)): {' Lisi ', ' DSD ', ' Zhangsan ', ' Wangmazi ', ' Yuhl '}: {' Lisi ', ' DSD ', ' Zhangsan ', ' Wangmazi ', ' Yuhl '} S1 = {1,2,3,4,5}S2 = {2,3} #子集print (s1 <= s2) #s2是s1的子集print (s1.issubset (s2)) #父集print (s2 <= s1) #s1是s2的父集print (s1 . issuperset (s2)) s3 = set (["he", "l", "1", "1", [+/-]) Print (s3)

Other methods:

#更新s1 = {1,2,3}s2 ={"yhl"}s1.update (' e ')          #插入s1. update ((1,2,3,4,5))   #去重进行写入s1. update ("hello") s1.update (s2) Print (s1)    #增加s1 = {1,2,3}s1.add ("Hello")     #整个加入print (s1)    #删除s3 = {1,2,3,4,5,6,7,8,9,10}s3.pop () print (s3 )  #随机删除s3. Remove #指定删除  If there is no error print (s3) S3.discard ("a") #不报错的删除 There will be     no error  . print (s3, " ------")      
4. File Processing

  

 

Python-base List Collection Dictionary file processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.