Python list and tuple and list operation [4]

1, What is data structure
In computer science, data structure is a way of storing and organizing data in computers

  1. Core data structure classification in Python
    Sequence type: string, list, tuple
    Mapping types: dictionaries
    Set: set()
    In Python, lists, tuples, dictionaries, and collections are called containers.
    All elements in a sequence type have corresponding positions, which are called offsets or indexes
    Two, list
    1 the elements in the list are mutable. The elements of a list can be any type of Python data and objects
    Elements with the same value in the list are allowed to appear more than once
>>> a
[3, 5, 1, 9, 2, 3, 7, 'qw', 1]
>>> type(a)
<class 'list'
In [2]: a = [1, 1, 2, 34, 'rwer', 'rwer']

In [3]: a
Out[3]: [1, 1, 2, 34, 'rwer', 'rwer']

2 create list

In [8]: nn = []
In [9]: n1 = [1, 2, 'wefw']

In [10]: n1
Out[10]: [1, 2, 'wefw']

In [11]: n = 'hello'

In [12]: list(n)  list() Convert from other types
Out[12]: ['h', 'e', 'l', 'l', 'o']

In [13]list('hello')   
Out[14]: ['h', 'e', 'l', 'l', 'o']
 
In [17]: '123.rwqr.343'.split('.')  split() Space as separator by default
Out[17]: ['123', 'rwqr', '343']
//nested list 
//The list can contain any type of element in python(object),You can also include one or more lists

li = [['one', 'two', 'three'], [1, 2, 3]]

3. Basic operation of list
3.1 value
No nested list values

ln [20]: n = ['we', 'eww', 1, 2]
In [21]: n[1]
Out[21]: 'eww'n
Nested list values
l2 = [['one', 'two', 'three'], [1, 2, 3]]
l2[0][1]  # two
Values in the list can be changed
d2 = [1, 2, 3]
>>> d2[1]
2
>>> d2[1] = 5
>>> d2
[1, 5, 3]

3.2 slicing is the same as string slicing

li_f = [ 'insert', 'append','extend', 'remove', 'pop', 'sort', 'sorted']

# Get all elements
li_f[:]

# Reversal
li_f[::-1]

3.3 necessary methods

#Define a list first
li = [5, 4, 3, 2, 1, 0]
len() 
Method is a built-in function that counts the length of a sequence type data structure.
n = len(li)
print(n) 6
in 
//Determines whether an element exists in the list.
In [3]: li = ['lenovo', 1314, '521']

In [4]: '521' in li
Out[4]: True

In [5]: 1314 in li
Out[5]: True

In [6]: if 'lenovo' in li:
   ...:     print('ok')
   ...:
ok

In [7]:
append()
To the end of the list, add an element and receive only one parameter.
In [7]: li.append(521) cannot be assigned to b b = li.append(521) error

In [8]: li
Out[8]: ['lenovo', 1314, '521', 521]

insert()
Insert an element to the specified location of the original list and receive two parameters,
The first is the index number and the second is the element to insert.

In [9]: li.insert(0, 521)

In [10]: li
Out[10]: [521, 'lenovo', 1314, '521', 521]

extend()
Each element of a sequence type can be appended to the original list, and the received parameter is the data of a sequence type (string, list).

In [11]: l2 = ['qf','yangge']

In [12]: li.extend(l2)

In [13]: li
Out[13]: [521, 'lenovo', 1314, '521', 521, 'qf', 'yangge']

remove()
Remove a specified element from the list without a return value, and if there are multiple elements with the same value, only the top element will be removed each time

In [14]: li.remove(521)

In [15]: li
Out[15]: ['lenovo', 1314, '521', 521, 'qf', 'yangge']

pop()

Remove an element from the original list and return it.
Receive zero or one parameter, the parameter is offset, int type.

# Delete the last element in the list
In [16]: name = li.pop()

In [17]: name
Out[17]: 'yangge'

In [18]: li
Out[18]: ['lenovo', 1314, '521', 521, 'qf']

 # Delete the element corresponding to the second index number in the list, and return the element, and receive it with the variable name 'n'.  
In [19]: n = li.pop(-2)

In [20]: n
Out[20]: 521

In [21]: li
Out[21]: ['lenovo', 1314, '521', 'qf']

clear() clears the list

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

count() counts the number of times an element appears in the list sort() sorts the elements in the list
As like as two peas, reverse() reverses the position of the element and copies copy exactly the same.
index() returns the index number of an element in an element


Three, tuple

Tuples, like lists, are sequences.
The only difference is that tuples are relatively immutable. Elements in the list can be modified elements in the tuple cannot be modified
t1 = () create tuple of empty element

>>> t=()
>>> type(t)  type
<class 'tuple'>
>>> 

tuple()
Data of other sequence types can be converted to tuples.

In [173]: s1 = 'car'

In [174]: li = [1,2,3]

In [175]: tuple(s1)
Out[175]: ('c', 'a', 'r')

In [176]: tuple(li)
Out[176]: (1, 2, 3)

In [177]: dl = [1,2,3,['a','b']]

In [178]: tuple(dl)
Out[178]: (1, 2, 3, ['a', 'b'])
  1. Reason to use tuples
    Small memory footprint
    Values in tuples will not be modified unexpectedly
    Key that can be used as a dictionary
    The parameters of a function are passed as tuples
    Named tuples can sometimes replace class objects
Published 6 original articles, won praise 5, visited 174
Private letter follow

Tags: Python

Posted on Thu, 05 Mar 2020 02:41:48 -0800 by mobile target