Python Basics (ii)

Source: Internet
Author: User

Set Set : Set is a collection of unordered, non-repeating elements. Nested lists, dictionaries (objects that can be used for loops or iterations). -------------------------------------------------------------------------## # # #差集:a={11,22}b={22,33}c=a.difference (b)#A is present, and B does not exist.D=b.difference (a)#b exists, A does not existPrint(c)--{11}Print(d)--{33}#### Add Add Method: The element to be passed in as an entire addition to the collection, for example:>>> A = set (' Boy')>>> A.add ('python')>>>Aset (['y','python','b','o'])## # # #集合update方法: is the element to be passed into the split, as an individual into the collection, for example:>>> A = set (' Boy')>>> A.update ('python')>>>Aset (['b','h','o','N','P','T','y'] # Delete element # Randomly remove an element, no error B={11,22,33}ret=B.pop ()Print(b)Print(ret)#Delete an element, no errord={1,2,3,4,5}d.discard (2)Print(d)#receives an object that can be used for loops (iterations)List = [1,2,3,4,5]set1={1,2}set.update (list)Print(Set1) Example: Find the value of two dictionary changes old_dict={"#1": 4,"#2": 8,"#4": 4,}new_dict={"#1": 8,"#2": 8,"#3": 4,#plus comma will not be error, there may be errors}#take a value and remove the key from the dictionaryOld_key=old_dict.keys () new_key=New_dict.keys ()#convert key to collectionOld_set=set (Old_key) new_set=Set (New_key)#New in the existing, the old does not exist, print out the need to delete theRemove=old_set.difference (New_set)Print(remove)#need to be updated,Update=new_set.difference (Old_set)Print(update)#you need to add theAdd=old_set.difference (New_set)
lists: List is an ordered set of elements that can be added and removed at any time. Use a variable to store more values Eg:name=['sz','YZ','XZ']num=[1,2,3,4,5,6,7,8,9,1,23,4,5,54,5,5]#you can find the element by subscript, the subscript starts with 0>>>NUM[-1]#-1 represents the last element in the list>>>5>>>NUM[0]#first element>>>1>>>NUM[-3]#Take out the third-lowest element of the list>>>54##如果取值的下标超过列表的总长度就会报错****Subscript is starting from 0################The value returned is true or falsePrint(3inchnum)#there are several 5 in the View listPrint(Num.count (5))#view an element in the following table in the list, after the first value is found, does not continue to find, directly returns the following table of the first valuePrint(Num.index (5))#the Len () built-in function lets you see the number of elements in a listlen (num)------------## # #切片user=['Zhangsan','Lisi','Liuwu','Zhaoliu','Wangqi', 1,2,3,4]#print out the elements from 2 to 8Print(User[2:8])Print(User[0:3])#takes 0 to 3rd element, does not contain 4th, 0 can not writePrint(User[:-3])#Prints the 3rd element from 0 to the bottomPrint(user[-3:])#take the last three elementsPrint(User[::2])#Step-Over value----------------------------------------------------------------## #增删改查added: User=['Zhangsan','Lisi','Liuwu','Zhaoliu', 1,2,1,'Wangqi', 1,2,3,4]user.append ("Test")#append an element at the end of the listDelete:#Delete the element as 1, if there are more than 1, then delete the first from left to right. User.remove (user[1])#Delete an in-memory data, the deleted head does not delete the taildelUser[3:4] Change: user[1]='Coffee'             #change the second element to coffeeUser1 = ['a','b','C','D']user2= ['e','F','g','h']#Inserting DataUser.insert (1,user1)#Insert a data, subscript 1User.insert (3,user2)#Insert a data, subscript 3#StatisticsUser.count ('1')#count the number of 1 elementsUser.sort ()#sort the listUser.reverse ()#Invert the list#Traverse List forIinchUser:Print(i)#instance loops change the value of an element in a listnum = [1,2,3,4,5,3,[3,3,3],6,7,7,8,9,9,9,9,9,5,6,6,6,7]#this element in the Num.count statistics list has several#Here's a few, just a couple of loops. forIinchRange (Num.count (9)):#The following table for each element is counted hereReal = Num.index (9)#to change the value of an element by subscriptNum[real] ='hhhhhhhhhhh'#PrintPrint(num)############################Example: Merging a new list into the NUM list num= [1,2,3,4,5,3]num1= ['Zeng','Xian']num.extend (NUM1)Print(num)
Meta-group
Another ordered list is called a tuple: a tuple. The tuple and list are very similar, but the tuple cannot be modified once it is initialized.
Once a tuple is initialized, it cannot be changed
For example
Name= (' Test ', ' study ')
You can also use the subscript to query
NAME[0]
NAME[1]
If you define a tuple with only 1 elements

Here there will be ambiguity, you define not a tuple, is 1 this number, () as a mathematical parentheses, so you define an element, you will add a comma after.
T1 = (1,)
---------------
Dictionary: unordered, using key-value pairs to store, with extremely fast lookup speed.
Dict Features:
The speed of finding and inserting is very fast and will not slow with the increase of key;
It takes a lot of memory, and it wastes a lot of memory.
List
The time to find and insert increases as the element increases;
Small footprint and little wasted memory.



Python Basics (ii)

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.