Python learns---The following tables in Python

Source: Internet
Author: User

Lists [list] use brackets [] to handle the data structure of a set of ordered items, the type of the list is a mutable data type, and the type is a list

The list is mutable/thread insecure

# type (a) = list uses the type to determine the element offline

# slices are left closed right open [m,n]

# The direction is determined by the positive or negative of the third parameter, unchanged or the subscript of the original list

#-1 represents the last element

#:: Represent to the last

#列表时可变的

#可以利用id查看列表在内存中的存储位置

Create a list + query

List of checks: Slices, index, count

# list = [' Hello ', ' world ', ' 2017 '] there will be alarms because list is the keyword name = [' Hello ', ' world ', ' hhh ', ' FTL ']# print (ID (name)) # slice is left Close to the right open form, around just take the direction, unchanged or the list subscript # Print all elements #print ("Print all elements:", name) print ("Print all elements:", name[0:name.__len__ ())) print ("Print all elements ( Default to Last: ", Name[0:]) # Print the first print (" Print First: ", Name[0]) # Remove the back 2 print (" Take Out the Back 2: ", Name[3:5]) # takes all the elements behind the world and defaults to the last print ( "Take out all elements behind the world:", Name[1:]) #-1 for the last element print ("1 for the last element:", name[-1]) # Remove the last element print ("Remove last element:", name[0:-1]) # Although the maximum range is exceeded, the default is to take the last element print ("exceeds the maximum range, but defaults to the last element:", Name[1:10]) # interval one takes one element (the default interval is 1, that is, every element is taken) print ("Interval one takes one element ( The default interval is 1, that is, each element is taken): ", Name[0::2]) # Right-to-left value, take out all elements print (" right-to-left value name[-1::-1], take out all elements: ", name[-1::-1]) # Note: If you use 0 subscript, One Less value print ("Right-to-left Name[-1:0:-1", take out all elements: ", name[-1:0:-1]) # Right-to-left value (one for each interval) print (" Right-to-left value, interval one: ", Name[-1::-2]) # Print only Hhh,worldprint ("Print only hhh,world:[3::-2]", Name[3::-2]) print ("Print only hhh,world:[-2::-2]", name[-2::-2]) # default is +1 direction print ("The default is +1 direction:", Name[-2::])

Count method counts the number of occurrences of an element in a list

name = [' A ', ' B ', ' C ', ' A ']print (Name.count (' a '))--          2

Index: Returns the position of the element based on the element content, if there are 2 identical elements, whichever is the first

name = [' Z ', ' a ', ' B ', ' C ', ' A ']print (' a ' the first position is: ', Name.index (' a ') ')        #-->1# Force the position of the second A to print (Name[name.index (' a ') ) +1::].index (' a ') + name.index (' a ') +1)

Nested list Exercises

Boy = [[' FTL ', ' Male '], [' HHH ', ', ' female '], [' Pig ', 0, ' Unknow ']]print ("info of the HHH:", boy[1]) print ("Age of HHH:" , Boy[1][1])

Special Printing for Loops

Te = [[' Shell ', ' max], [' Python ', ' max], [' Mysql ', ' Max ', [' Oracle ', 400]]print ("Overall print 1:", TE) print ("Overall print 2 (with loop):") for I in TE: Print (    i) print (' Te size: ', Len (TE)) print ("[K,v] Use:") for I, V in te:    print (i, v)

Use the list class to create lists: you can see that the parentheses, the brackets, or the curly braces are small, and the list is constructed.

name = List ([1, 2, 3, 4, 5, 6]) print (name) name2 = List ((7, 8, 9, ten, one, each)) print (name2) Name3 = List ({13, 14, 15, 16, 17, ) Print (Name3)

Use List Builder to generate lists, or you can place functions to manipulate them

# List Builder

A = [x*2 for x in range]  # find x and manipulate x print (a)               # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

# Operate inside a function

def plus (x):    return x * * 3# b = [Plus (x) for x in [1, 3, 5]]   is a sequence just fine b = [Plus (x) for x in range (]print)                # [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

Special assignment of the list: note the number of elements and the quantity in the list are the same

c = [' Hello ', ' world ']x,y = Cprint (x, y)      # Hello World

Increase in list: Append,insert,extend

Append is inserted at the end of the list by default, and the index value is modified

# list = [' Hello ', ' world ', ']  will have alarms because list is the keyword name = [' Hello ', ' world ', ' hhh ', ' FTL ']print ('------------ --------------------------------------------------------------------') print ("Append before modification:", name[0:]) # Append default is insert to List tail name.append (' Python ') print ("Append modified:", Name[0:]) print ('-------------------------------------- ------------------------------------------') print ("Insert modified before:", Name[0:]) # modifies the index value Name.insert (0, "Java") Print (" Insert modified: ", Name[0:]) print ('--------------------------------------------------------------------------------')

The Extend method can append multiple values from another sequence at the end of the list at once.

The Extend method modifies the list that is expanded, and the original connection operation (+) returns a completely new list without affecting the original list.

name = [' A ', ' B ', ' c ']name2 = [' E ', ' f ', ' G ']name2.extend (name) print ("Name:", name) print ("Name2.extend (name):", name2) NA Me3 = [' x ', ' y ', ' Z ']print ("name + Name3:", name + Name3)

Changes to the list:

Modify multiple elements at the same time, you need to use a list to assign values again

# list = [' Hello ', ' world ', ']  will have alarms because list is the keyword name = [' Hello ', ' world ', ' hhh ', ' FTL ']# modified 2017 to 2018print (" Before modification: ", name[0:]) name[2] = ' 2018 ' Print (" Modified: ", Name[0:]) print ('---------------------------------------------------- ----------------------------') # Modify Hello World for Java pythonprint ("Pre-Modified:", Name[0:]) name[0:2]=[' Java ', ' Python ']print (" Modified: ", Name[0:])

Special case: If the number of replacements and replacements is not on, the element of that position will be deleted

name = [' A ', ' B ', ' C ', ' d ', ' e '] #name [1:4]  ==> b c dprint (name) name[1:4] = [' B ', ' C ']print (name)

Removal of the list: remove Pop del

Remove removes the element, not the subscript

Pop removes the subscript, and it returns the deleted element, and nothing is written. Delete the last value by default

Del is a function that can either delete an element or delete a list object

Clear clears, but retains the original list properties

Remove:

# list = [' Hello ', ' world ', ']  will have an alarm because list is the keyword name = [' Hello ', ' world ', ' hhh ', ' FTL ']# delete 2017print ("Modified before:" , name[0:]) # Remove is an element, not subscript # Name.remove (1)--  ValueError:list.remove (x): X not in Listname.remove (' 2017 ') # Name.remove (name[2])  -the same effect, which is equivalent to removing the print ("Modified:", Name[0:]) After removing the element with the list

Pop: Delete subscript, and you can get the deleted content

# list = [' Hello ', ' world ', ']  will have an alarm because list is the keyword name = [' Hello ', ' world ', ' hhh ', ' FTL ']# delete 2017print ("Modified before:" , name[0:]) Year=name.pop (2) Print ("Modified:", Name[0:]) Print ("Year:", year) #特殊: Delete the last element by default Name.pop () print (name)

Del: is a function that can either delete an element or delete a list object

# list = [' Hello ', ' world ', ']  will have an alarm because list is the keyword name = [' Hello ', ' world ', ' hhh ', ' FTL ']# delete 2017print ("Modified before:" , Name[0:]) del name[2]print ("Modified:", Name[0:]) # Delete the name Object del nameprint ("after deleting the name object:", Name[0:])

Other

The reverse method stores the elements in the list in reverse

name = [' A ', ' B ', ' C ']name.reverse () print (name)

The sort method is used to sort the list at the original location, by default ascending

name = [' Z ', ' a ', ' B ', ' C ', ' A ']name.sort () print (name) name.sort (reverse=true) print (name)

Python learns---The following tables in Python

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.