Python lists and tuples and a list of the operation [4]

First, what is the data structure
in computer science, a data structure (English: data structure) is stored in the computer, organize data

  1. The core data structure classification in Python
    sequence types: strings, lists, tuples
    map type: dictionary
    set: SET ()
    in Python lists, tuples, dictionaries, collections are called containers.
    Position in the sequence has a corresponding type of elements, the location is called an offset or index
    II lists
    the elements 1 within the list is variable. Elements of the list may be any type of data and objects Python of
    elements in the list have the same value allows multiple occurrences
>>> 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 a 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() 从其他类型转换
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() 默认以空格为分隔符
Out[17]: ['123', 'rwqr', '343']
嵌套的列表
列表中可包含 python 中任何类型的元素(对象),当然也可以包括一个或多个列表

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

3. Basic operation list
3.1 value
not nested lists values

ln [20]: n = ['we', 'eww', 1, 2]
In [21]: n[1]
Out[21]: 'eww'n
嵌套的列表取值
l2 = [['one', 'two', 'three'], [1, 2, 3]]
l2[0][1]  # two
列表内的值是可以更改的
d2 = [1, 2, 3]
>>> d2[1]
2
>>> d2[1] = 5
>>> d2
[1, 5, 3]

3.2 slices, like slices of a string

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

# 获取全部元素
li_f[:]

# 反转
li_f[::-1]

3.3 method will be

# 先定义一个列表
li = [5, 4, 3, 2, 1, 0]
len() 
方法是一个内置函数,可以统计序列类型的数据结构的长度。
n = len(li)
print(n) 6
in 
判断元素是否存在于列表中。
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()
向列表的最后位置,添加一个元素,只接收一个参数。
In [7]: li.append(521)  不能这样赋值给b b = li.append(521) 错误

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

insert ()
designated insertion position to the origin of a list element, receiving two parameters,
the first index number is, the second element is to be inserted.

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

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

extend ()
can be a type of each element of the sequence is added to the original list, the received parameter sequence is a type of data (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 list element, no return value, and if a plurality of elements of the same value exists, every element in that row will only remove the frontmost

In [14]: li.remove(521)

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

pop()

To delete an element from the original list and return to the elements.
Receiving zero or one argument, which is the offset, int type.

# 删除列表中的最后一个元素
In [16]: name = li.pop()

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

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

 # 删除列表中第二个索引号对应的元素,并且返回这个元素,用变量名`n` 接收。  
In [19]: n = li.pop(-2)

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

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

clear () Clear List

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

count () sort statistics list the number of times an element that appears () to sort the list of elements in
reverse () reverses the position of the elements in an identical copy to copy a list of
index () returns the index number of the element in an element

Here Insert Picture Description
Triad

Tuples and lists, but also a sequence.
The only difference is that the tuple is relatively immutable. Within the list of elements may be modified within tuple element can not be changed
t1 = () # Create an empty element tuple

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

tuple ()
may be a sequence of tuples to other types of data conversion.

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 for using tuples
    occupies a small memory space
    values within the tuples can not be accidentally modified
    keys as a dictionary
    function of the parameters are transmitted in the form of tuples of
    objects may sometimes be replaced by a tuple named class
Released six original articles · won praise 5 · Views 266

Guess you like

Origin blog.csdn.net/wx912820/article/details/104659295