List, Ganso, dictionary, collection, usage summary

1. List:

  1> increase: append (): a single element is added, or a list 

       extend (): Extended

       insert (index, insert data): insert, insert a specific element in the specified location, the original elements shall be postponed

  2> Delete: pop (): pop-up, remove the element at the specified target list is specified, the default value if the specified index to remove the last element

      remove (): Removes the specified elements, from left to right is detected, to remove only the first matching element to

      clear (): Empty list

  3> Review: reverse (): Reverse

      sort (): sort by default the program, to descending, ascending to be, in the reverse, or reverse = True

  4> Query: len (): Gets the length

      max (): Get the maximum

      min (): Get the minimum

      index (): Get a list of the next table element first appears is specified in the list by

      count (): a method to count the number of elements appear in the list of the list

2. Ganso:

Ganso once they are defined, you can not make any changes to its elements

len (): Get the number of elements

max (): Get the maximum value of the element

min (): Get the minimum value of the element

List Ganso and mutual transformation:

3. dictionary:

key features: 1> Unique can not be repeated

     2> must be immutable, it can not be lists

Attention to the problem through the key check value:

  One way: dictionary name [key] Gets value, if the key does not exist, will direct error

  Second way: the dictionary name .get (key) if the key does not exist, None is returned

Traversal:

Method 1: for key in dict1:

      print(key, dict1[key])

Second way: for key in dict1.keys ():

      print(key,dict[key])

Three ways: for value in dict1.values ​​():

      print(value)

Four ways: for index, key in enumerate (dict1): #enumerate into the dictionary (ID, key) in the form

    print(index,key, dict1[key])

Five ways: for key, value in dict1.items ():

      print(key,value)

4. Collection:

Features: Do not allow storage repeating elements and disorderly.

Creating an empty set: set ()

Increase: s1.add () # can not add variable data types

Update: s1.update () # methods analogous to extend in list must be iterative, break added

   Add the dictionary when only identify key

Delete: s1.remove ()

Traversal:

  A manner: for i in s1:

  Second way: for i, num in enumerate (s1):

        print (I Do)

Intersection and union: r1 = set1 & set2 r2 = set1 | set2

 

Guess you like

Origin www.cnblogs.com/yanhonghong/p/11617011.html