A list of common method

List common methods:

  • append ------ represents a new element is added in the list

>>> a
[1, 2, 3, 4]

>>> a.append(0)
>>> a
[1, 2, 3, 4, 0]
>>>

  • insert ------ represents a new element is added at a specific location

>>> a
[1, 2, 3, 4, 0]
>>>
>>> a.insert(0,5)
>>> a
[5, 1, 2, 3, 4, 0]
>>>

  • clear ------ list empty

>>> a
[5, 1, 2, 3, 4, 0]=
>>> a.clear()
>>> a
[]

  • remove ------ itself the elements of the list by removing the elements

>>> a

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

  • pop ------ Removes the last element

>>> a
[1, 2, 3, 4]
>>>
>>> a.pop()
4
>>> a.pop()
3
>>> a
[1, 2]

  • index ------ find the location of elements in the list (if the element does not exist, an exception is thrown)

>>> a=[1,2,3,4,5]
>>> a
[1, 2, 3, 4, 5]
>>> a.index(3)
2
>>>

  • reverse ------ flip List

>>> a
[1, 2, 3, 4, 5]
>>> a.reverse()
>>> a
[5, 4, 3, 2, 1]
>>>

  • sort ------ sort the list, according to the ASCII sort table

>>> a
[5, 4, 3, 2, 1]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]
>>>

  • ----- Copy Copy, the copy target (shallow copy)

>>> a.copy()
[1, 2, 3, 4, 5]
>>> a
[1, 2, 3, 4, 5]
>>>

  • extend ------ merge list

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

Set (unordered and not repeat)

Set of common methods:

  • add ------ add elements

>>> a={1,2,3,4,5}

>>> a

{1, 2, 3, 4, 5}

>>> a.add(66)

>>> a

{1, 2, 3, 4, 5, 66}

>>>

  • clear ------ Clear List

>>> a

{1, 2, 3, 4, 5, 66}

>>> a.clear()

>>> a

set()

>>>

  • copy ------ copy, copy (shallow copy)

>>>

>>> a={11,22,33,44}

>>> a

{33, 11, 44, 22}

>>> a.copy()

{33, 11, 44, 22}

>>> a

{33, 11, 44, 22}

  • pop ------ immediately remove elements

>>> a

{33, 11, 44, 22}

>>> a.pop()

33

>>> a.pop()

11

>>> a

{44, 22}

>>>

  • remove ------ Removes the specified element

>>> a

{44, 22}

>>> a.remove(44)

>>> a

{22}

>>>

  • intersection ------ intersection

>>> a={11,22,33,44}

>>> a

{33, 11, 44, 22}

>>> b={22,33,44,55}

>>> b

{33, 44, 22, 55}

>>> a.intersection(b)

{33, 44, 22}

>>>

  • difference-------差集

>>> a

{33, 11, 44, 22}

>>> b

{33, 44, 22, 55}

>>> a.difference(b)

{11}

>>>

  • union ------ union

>>> a

{33, 11, 44, 22}

>>> b

{33, 44, 22, 55}

>>> a.union(b)

{33, 11, 44, 22, 55}

>>>

 

Tuple: a series of fixed value (immutable data type)

Note: In the python , if only one element, and the element is a number, tuple is required to represent a comma instead of the digital variable

Tuple common methods :

  • count ------ number of elements that appear in the statistics

>>> a=[1,1,1,1,1,2,3,4]

>>> a.count(1)

5

>>>

  • index ------ find the location of elements in the list (Note: if the element does not exist, an exception is thrown, if there are multiple elements, the first is returned)

>>> a

[1, 1, 1, 1, 1, 2, 3, 4]

>>> a.index(3)

  • Dictionary: dict {K1: V1, K2: V2}

  Dictionary is the key to exist, each key corresponds to a unique value. Key must be a string

None indicates no

Get key value corresponding to the use of the following two ways:

  • d [ "key"] ------------------ If the key does not exist, an exception is thrown
  • d.get ( "key") ------------- If this key does not exist, returns None

>>> a = { 'tiancai': 'diyibu', 'queshi': 'zhiniaoku'}

>>> a

{ 'Tiancai': 'diyibu', 'queshi': 'zhiniaoku'}

>>> a["tiancai"]

'Diyibu'

>>> a["queshi"]

'Zhiniaoku'

>>>

Dictionary common methods:

  • clear ------ Clear List

>>>

>>> a

{ 'Tiancai': 'diyibu', 'queshi': 'zhiniaoku'}

>>> a.clear()

>>> a

{}

>>>

  • copy ------ copy, copy list (shallow copy)

>>> a

{ 'Tiancai': 'diyibu', 'queshi': 'zhiniaoku'}

>>> a.copy()

{ 'Tiancai': 'diyibu', 'queshi': 'zhiniaoku'}

>>>

  • POP ( "Key" ) ------ by ke delete the corresponding key-value pairs

>>> a

{ 'Tiancai': 'diyibu', 'queshi': 'zhiniaoku'}

>>> a.pop("tiancai")

'Diyibu'

>>> a

{ 'Queshi': 'zhiniaoku'}

>>>

  • popitem ------ immediately delete a key-value pair

>>> a = { 'tiancai': 'diyibu', 'queshi': 'zhiniaoku'}

>>> a.popitem()

( 'Queshi', 'zhiniaoku')

>>> a

{ 'Tiancai': 'diyibu'}

>>>

  • get ------ by key acquisition value

>>> a = { 'tiancai': 'diyobu', 'queshi': 'zhiniaoku'}

>>> a

{ 'Tiancai': 'diyobu', 'queshi': 'zhiniaoku'}

>>> a.keys()

dict_keys ([ 'tiancai', 'queshi'])

>>>

  • values()

>>> a

{ 'Tiancai': 'diyobu', 'queshi': 'zhiniaoku'}

>>> a.values()

dict_values(['diyobu', 'zhiniaoku'])

>>>

  • Items()

>>> a

{ 'Tiancai': 'diyobu', 'queshi': 'zhiniaoku'}

>>> a.items()

dict_items ([( 'tiancai', 'diyobu'), ( 'queshi', 'zhiniaoku')])

>>>

Guess you like

Origin www.cnblogs.com/yxh6666/p/11520816.html