Common methods and differences of Python basic-list,tuple,dict,set

Source: Internet
Author: User
Tags set set

1, List

How the list is defined

lis1=[1,2,3,4] #一维数组

lis2=[1,2,3,[4,5,6]] #二维数组

Multidimensional data in turn, with several layers being a few-dimensional array

The value of the list can be subscript, subscript is starting from 0, the list of additions and deletions to check;

Increase:

msg = ' Hello '
name = [' Andashu ', ' cc ', ' Niuniu ']
Name.append (msg) #从最后面开始插入
Name.insert (1,msg) #从指定位置插入, this 1 represents subscript
Print (name)
Change:
msg = ' Hello '
name = [' Andashu ', ' cc ', ' Niuniu ']
name[1] = ' Baby ' #修改指定位置的值
Check:
msg = ' Hello '
name = [' Andashu ', ' cc ', ' Niuniu ']
print (name[0]) #获取第一个元素
print (Name[-1]) #-1 represents the last element
Delete:
msg = ' Hello '
name = [' Andashu ', ' cc ', ' Niuniu ']
name.remove (' baby ') #删除指定的值
del name[0] #删除指定位置的值
Name.pop () #不传入下标的话, remove the last value, and pass in the subscript to delete the value at the specified position
name.clear () #清空列表

Common built-in methods:

 name = [' Andashu ', ' cc ', ' Niuniu ']  
name2 = [1,2,3,4,[' hehe ', ' Haha ']]
print (Name.index (' Niuniu ') #取索引
print ( Name.count (' cc ')) #取cc出现的次数
Name.clear () #清空列表
print (Name.sort ()) #排序, Sort
Print (Name.extend (name2)) #扩展列表, that is, add the value of name2 to name
name.reverse () #反转数组 will change the value of the original array

The value of the list can also be sliced by:

Starting from the first few elements, to the end of the first few elements, to get the value between them, the format is name:[1:10], for example, to get the first element of name to the fifth element, you can use Name[0:6], the slice is not the value of the subsequent element, remember Gu Tou regardless of the tail , before the subscript if it is 0, you can omit not to write , so write, name[:6], slices can also write a parameter, called step, that is, how many elements, take once, the default can not write, that is, the slice operation can also use the string , as with the use of lists

names = [' Andashu ', ' cc ', ' Niuniu ', ' Amy ', ' Lily ']
Names[1:4] #取下标1至下标4之间值, including 1, not including 4
Names[1:-1] #取下标1至-1 value, excluding -1
Names[0:3] #取下标0至3的值, not including 3
Names[:3] #取下标0至3的值, not including 3, and the effect above, 0 can be omitted to write
names[2:] #取从第二个下标开始的后面所有元素
#下面是加上步长的
nums = [1,2,3,4,5,6,7,8,9,10]
Nums[::2] #这个代表取所有的元素, then take one every 2 elements
Nums[1:8:3] #代表取第二个元素开始, to the eighth end, 3 to take once
2, tuple tuple,
The value of the tuple is immutable, the tuple is defined with () parentheses, and tuples are typically used to store unchanging data in the program, such as JDBC connection data, only two built-in methods of the tuple index (), COUNT ()
3, Dictionary dict
A dictionary is a way of key-value, with {}, for example, to save everyone's information, then everyone's number is Key,value is everyone's information, so that a dictionary can save everyone's information.
the dictionary is defined using {}, curly braces, each value separated by ",", Key and value using ":" Delimited
Infos={' Marry ':[18,18612512981, ' Beijing ' ], ' Amy ' :[ 20,18612512991, ' Shandong ' ], ' Lily ' :< Span class= "Crayon-sy" >[25,18612532981< Span class= "Crayon-sy" >, ' Henan ' ]} infos[' Marry '] #取marry的信息 features of the dictionary:
The dictionary is unordered because it has no subscript and is indexed with key, so it is unordered
The key of the dictionary must be unique because it is indexed by key, so key cannot be duplicated and is inherently heavy .
dictionary additions and deletions to search:
Increase:
infos = {' Marry ': [18, 18612512981, ' Beijing '], ' Amy ': [20, 18612512991, ' Shandong '], ' lily ': [25, 18612532981, ' Henan ']}
infos[' andy ' = [22, 18712512981, ' Hebei '] # Add
Modify:
infos = {' Marry ': [18, 18612512981, ' Beijing '], ' Amy ': [20, 18612512991, ' Shandong '], ' lily ': [25, 18612532981, ' Henan ']}
infos[' Marry ' = [38, 18612512981, ' Beijing ']
Delete:
infos = {' Marry ': [18, 18612512981, ' Beijing '], ' Amy ': [20, 18612512991, ' Shandong '], ' lily ': [25, 18612532981, ' Henan ']}
Infos.pop (' Marry ') # standard method of deletion
del infos[' marry ' # Use the Del method to delete
Info.popitem () # randomly deletes a value
Enquiry:
infos = {' Marry ': [18, 18612512981, ' Beijing '], ' Amy ': [20, 18612512991, ' Shandong '], ' lily ': [25, 18612532981, ' Henan ']}
infos.get (' Maryy ') # Gets the information of marry, which returns none if key does not exist
infos[' Marry ' # Gets the marry information, this way if key does not exist, it will be an error
' Marry ' in Infos # to determine if marry is in this dictionary, returns TRUE or false
built-in methods for dictionaries:
dic = {' STU1 ': ' cc ', ' stu2 ': ' Andashu ', ' stu3 ': ' Niuniu '}
print (Dic.values ()) # prints all value
print (Dic.keys ()) # prints all the keys
Print (Dic.setdefault (' stu1 ', ' Fengluo ')) # If this key exists, then do not move it, do not exist, add a
Dic2 = {' stu1 ': ' Sriba ', ' stu10 ': ' Baidu '}
dic.update (DIC2) # Updates the dictionary value, if key exists, it is updated, does not exist to add
print (Dic.items ()) # Dictionary converted to a list
Circular Dictionary:
DiC = {' STU1 ': ' cc ', ' stu2 ': ' Andashu ', ' stu3 ': ' Niuniu '}
For K in DiC:
Print (K,dic[k]) #打印key和value的值, recommended this way, faster
4, Set set

Collection:

A collection is also a data type, a similar list of things, it is characterized by unordered, non-repeating, that is, the collection is no duplication of data

list = [2,3,1,2,3,4]
s_list = set (list) #这样就定义了一个集合
Set1 = set ([1,3,4,5,6]) #这种方式和上面的都是把list转换成一个集合
set2={' hehe ', ' hehe1 ', ' hehe3 '} #这种方式是直接定义一个集合

List1 = {1, 2, 3, 4, 5, 6, 9}
List2 = {2, 3, 4, 6, 1}
List3 = {1, 2, 3}
Print (List1.intersection (LIST2)) # Take the intersection, that is, take List1 and List2.
print (List1 & list2) # take intersection
Print (List1.union (LIST2)) # takes the union, which is to merge List1 and List2, and then remove the duplicate
Print (List1 | list2) # FETCH and set
Print (List1.difference (list2)) #取差集 exist in list, not in List2
print (LIST1-LIST2)
Print (List3.issubset (list1)) #判断list3是不是list1的子集
Print (List1.issuperset (LIST3)) #判断list1是不是list3的父集
Print (List1.isdisjoint (LIST3)) #判断list1和list3是否有交集
Print (List1.symmetric_difference (list2)) #对称差集, output a value that is not in the two list, that is, remove the same in two sets
Print (list1 ^ list2)
List1.add (888) #添加元素
list1.update ([777,666,666])
List1.remove (777) #删除元素, if the element does not exist will be an error
List1.pop () #删除一个随机的元素 and returns the deleted element
list1.discard (' dddd ') #如果删除的元素存在, delete, does not exist do not handle

Common methods and differences of Python basic-list,tuple,dict,set

Related Article

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.