Basic data types and built-in method 2

First, a list of

  1, the basic method

    use:

      Stored value for one or more different types of

    Definition method :

      , Each value is separated by a comma in parentheses stored value

      s1 = [ 'a', 'b', 1, 'cd']

    Built-in method:

      ① index values

# Index value 
>>> L1 = [1,2,3,4,5 ]
 >>> L1 [2 ]
 . 3
 # index sections 
>>> L1 [. 1:. 4 ]
[2, 3, 4]

      ②append (): append, can only add to the list the last one, a one-time only add value

>>> l1 = [1,2,3,4,5]
>>> l1.append(1000)
>>> l1
[1, 2, 3, 4, 5, 1000]
>>> l1.append([9,0])
>>> l1
[1, 2, 3, 4, 5, 1000, [9, 0]]

      ③insert (): the value is inserted by the inserting position specified by the index

>>> l1 = [1,2,3,4,5]
>>> l1.insert(3,99)
>>> l1
[1, 2, 3, 99, 4, 5]

      ④extend (): adding a plurality of values

>>> l1 = [1,2,3,4,5]
>>> l1.extend([6,7,8])
>>> l1
[1, 2, 3, 4, 5, 6, 7, 8]

      ⑤remove (): Specifies to delete, if the same back, left to right, then remove the first

>>> l1 = [1,2,3,1,4,5]
>>> l1.remove(1)
>>> l1
[2, 3, 1, 4, 5]

      ⑥pop (): Delete the default start from the end, delete the specified index, pop is the return value, return the removed elements

>>> l1 = [1,2,3,1,4,5]
>>> l1.pop()
5
>>> l1
[1, 2, 3, 1, 4]
>>> l1.pop(1)
2
>>> l1
[1, 3, 1, 4]

      ⑦del: Clear

>>> l1 = [1,2,3,1,4,5]
>>> del l1[2]
>>> l1
[1, 2, 1, 4, 5]

      ⑧count (): Specifies the number of elements in the hit list

>>> l1 = [1,2,3,1,4,5]
>>> l1.count(1)
2

      ⑨index (): Gets the index of the element values, you can also find within the specified range

>>> l1 = [1,2,3,1,4,5]
>>> l1.index(3)
2
L1.index >>> (1,1,5)   # investigation where index 1 [2,3,1,4,5] is 
3

      ⑩sort (): sorting, sorting the list on the original (default from small to large, when the input parameter reverse = True, descending order) is modified, the original list

>>> l1 = [4,5,1,7,4,8,3,9]
>>> l1.sort()
>>> l1
[1, 3, 4, 4, 5, 7, 8, 9]
>>> l1.sort(reverse = True)
>>> l1
[9, 8, 7, 5, 4, 4, 3, 1]

# Sorted (): Sort time to generate a new list, the original list unchanged 
>>> L1 = [4,5,1,7,8,3,9 ]
 >>> sorted (L1)
[1, 3, 4, 5, 7, 8, 9]
>>> l1
[4, 5, 1, 7, 8, 3, 9]

      ⑾clear (): Empty data

>>> l1 = [4,5,1,7,8,3,9]
>>> l1.clear()
>>> l1
[]

      Queue: FIFO

l1 = []
l1.append(1)
l1.append(2)
l1.append(3)
l1.append(4)
print(l1)
for i in range(len(l1)):
    print(l1.pop(0))
    print(l1)

      Stack: last-out

l1 = []
l1.append(1)
l1.append(2)
l1.append(3)
l1.append(4)
print(l1)
for i in range(len(l1)):
    print(l1.pop())
    print(l1)

2, Type Summary:

    Ordered or disordered (indexed are ordered):

      Ordered list type

    Variable or invariable:

      Id value change is constant variable type, the value is changed id becomes immutable type

      List type is a variable type

    Save a value or multi-value:

      List can be stored a plurality of values

Second, the tuple

  1, the basic method:

    use:

      Storing a plurality of different types of values ​​(can not store a variable-type)

    Definition method :

      With parentheses storing data, and the data between the data separated by a comma, (values ​​can not be changed)

      t1 = ('a','b','c')

      # Container type defined time, if there is only one value, after the value of a comma *****
      # if not in a tuple, is the string

    Built-in method:

      ① index values

>>> t1 = ('a','b','c')
>>> t1[0]
'a'
>>> t1[0:2]
('a', 'b')

      ② membership operator in, not in

      ③len()

      ④count()

      ⑤index()

2, Type Summary:

    Ordered or disordered (indexed are ordered):

      Tuple type ordered

    Variable or invariable:

      Id value change is constant variable type, the value is changed id becomes immutable type

      Tuple type is immutable

    Save a value or multi-value:

      A plurality of values ​​can be stored tuple

Third, Dictionary

1, the basic method:

    use:

      Storing a plurality of different types of values

    Definition method :

      By braces to store data by key: value pairs to define the data, each separated by a comma key intermediate

      d1 = {'name':'abc','age':18}

      d2 = dict({'name':'egon'})

      zip :

l1 = ['name',"age"]
l2 = [ ' Egon ' , 18 ]
z1 = zip(l1,l2)
print(dict(z1))

 

      # Key: it must be a immutable
      # value: may be any type

    Built-in method:

      ① Set key: value mapping relation values ​​(can be kept desirable)

>>> d1 = {'name':'abc','age':18}

#
>>> d1['name']
'abc'

#
>>> d1['name'] = 'a'
>>> d1
{'name': 'a', 'age': 18}

#
>>> d1['gender'] = 'male'
>>> d1
{'name': 'a', 'age': 18, 'gender': 'male'}

      ② membership operator in, not in: the default judgment key value

      ③get: Gets the value of the specified key, if there is no default return None, can be modified via the second argument returns the contents

>>> d1 = {'name':'abc','age':18}
>>> d1.get('name')
'abc'
>>> d1.get('a')
None
>>> d1.get('a','*')
'*'

      ④keys、values、items

>>> d1 = {'name':'abc','age':18}
>>> d1.keys()
dict_keys ([ ' name ' , ' Age ' ])     # Returns all key values 
>>> d1.values ()
dict_values ([ ' ABC ' , 18 is])         # Returns the value of all values 
>>> d1.items ()
dict_items ([( ' name ' , ' ABC ' ), ( ' Age ' , 18 is)])     # returns all key-value pairs set to return a list of tuples

# 取出所有
for key,value in d1.items():
    print(key,value)        #  key,value = ("name",'age')

     ⑤pop: delete the specified key, it returns a value, returns the corresponding value

      popitem: random delete the key right, returns a tuple deleted

# pop
>>> d1 = {'name':'abc','age':18}
>>> d1.pop('name')
'abc'
>>> d1
{'age': 18}

# popitem
>>> d1 = {'name':'abc','age':18}
>>> d1.popitem()
('age', 18)
>>> d1
{'name': 'abc'}

      ⑥update: Replace the old dictionary with the new dictionary, the new key value does not exist, there is a key value is modified

>>> d1 = {'name':'abc','age':18}
>>> d1.update({'a':'2'})
>>> d1
{'name': 'abc', 'age': 18, 'a': '2'}
>>> d1.update({'name':'a'})
>>> d1
{'name': 'a', 'age': 18, 'a': '2'}

      ⑦fromkeys: generating a dictionary, the first parameter (list), it will first argument each element of the key value, the second value for the parameter

>>> dict.fromkeys(['k1','k2'],['v1','v2'])
{'k1': ['v1', 'v2'], 'k2': ['v1', 'v2']}

      ⑧setdefault: key does not exist, the new key-value pair, by the return value; Key is present, returns the corresponding value

>>> d1 = {'name':'abc','age':18}
>>> d1.setdefault('name',1)
'abc'
>>> d1
{'name': 'abc', 'age': 18}
>>> d1.setdefault('a',1)
1
>>> d1
{'name': 'abc', 'age': 18, 'a': 1}

2, Type Summary:

    Ordered or disordered (indexed are ordered):

      Dictionary type of disorder

    Variable or invariable:

      Id value change is constant variable type, the value is changed id becomes immutable type

      Dictionary type is the variable type

    Save a value or multi-value:

      A plurality of values ​​can be stored Dictionary

Fourth, the collection

1, the basic method:

    use:

      De-emphasis, relational operators

    Definition method :

      Storing data by braces, each of the elements separated by commas

      The definition of the empty set, you must use the set () is defined

    Built-in method:     

      Collection: |
      Intersection: &
      set difference: -
      symmetric difference: ^

  Set of two identical elements impossible

pythons = {'egon', 'kevin', 'echo', 'owen', 'jason'}
linuxs = {'egon', 'echo', 'tank', 'oscar'}

pythons = {'egon', 'kevin', 'echo', 'owen', 'jason'}
linuxs = { ' Egon ' , ' echo ' , ' Tank ' , ' Oscar ' }
 Print ( " Registration python participants: " , Pythons The)
 Print ( " Registration linux participants: " , linuxs)
 Print ( " both registration python and registration Linux students: " , Pythons the & linuxs)
 Print ( " all enrolled students: " , Pythons the | linuxs)
 Print ( "Registration only python program, participants: ", Pythons The - linuxs)
 Print ( " not at the same time participants of the two courses: " , Pythons The ^ linuxs)

2, Type Summary:

    Ordered or disordered (indexed are ordered):

      Unordered collection type

    Variable or invariable:

      Id value change is constant variable type, the value is changed id becomes immutable type

      Collection type is a variable type

    Save a value or multi-value:

      Collection can store a plurality of values

summary:

  A deposit: integer, floating point, string
  stored plurality of values: lists, tuples, dictionaries, set


  Variable or non-variable:
  Variable:; lists, dictionaries
  immutable: integer, floating point, string, tuple, set

  Ordered or disordered:
  Ordered: strings, lists, tuples
  disorder: dictionaries, collections

  Space:
  Dictionary
  list
  of tuples
  collection of
  string
  numeric type

Guess you like

Origin www.cnblogs.com/hexianshen/p/11807392.html