笔记-python-tutorial-5.data structure

笔记-python-tutorial-5.data structure

1.      data structure

1.1.    list operation

  1. list.append(x) #尾部添加一个元素
  2. list.extend(iterable)
  3. list.insert(I, x)
  4. list.remove(x)

remove the first item from the list whose value is x.if x is not exist return ValueError.

  1. list.pop([i])
  2. list.clear()
  3. list.index(x [, start, end]) #返回首个值为X的元素下标。
  4. list.count(x)
  5. list.sort(key=None, reverse=False)
  6. list.reverse()
  7. list.copy()

返回一个复制品而不是引用。

1.1.1.   using lists as stacks

stack = [3,4,5]

stack.append(6)

stack.pop()

1.1.2.   using lists as queues

列表如果使用先进先出就是队列了,当然这样没有效率,不符初衷。

from collections import deque

queue = deque([‘before’, ‘middle’, ‘after’])

queue.append(‘carry’)

queue.popleft()

1.1.3.   列表生成式

下面是使用函数常规列表生成:

squares = list(map(lambda x: x**2, range(10)))

等效于

squares = [x**2 for x in range(10)]

1.1.4.   复杂一点的嵌套式列表生成式

>>> matrix = [

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

...     [5, 6, 7, 8],

...     [9, 10, 11, 12],

... ]

>>> [[row[i] for row in matrix] for i in range(4)]

[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

1.2.    删除del

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

del a[0]

del a[2:4]

del a[:]

del a

注意最后两个语句的效果是不一样的;

1.3.    tuple

tuple是不可变的,但tuple中可以包含可变元素。

>>> t = ([45,67,3], [5,6,7,8],[54])

>>> t

([45, 67, 3], [5, 6, 7, 8], [54])

>>> t[1].append(879)

>>> t

([45, 67, 3], [5, 6, 7, 8, 879], [54])

>>> t[1] = [4]

Traceback (most recent call last):

  File "<pyshell#15>", line 1, in <module>

    t[1] = [4]

TypeError: 'tuple' object does not support item assignment

1.4.    sets

python中的sets是一组无序不重复数据集合

类似于列表生成式,也支持生成式。

1.5.    dictionaries

一些常规操作

>>> tel = {'jack': 4098, 'sape': 4139}

>>> tel['guido'] = 4127

>>> tel

{'sape': 4139, 'guido': 4127, 'jack': 4098}

>>> tel['jack']

4098

>>> del tel['sape']

>>> tel['irv'] = 4127

>>> tel

{'guido': 4127, 'irv': 4127, 'jack': 4098}

>>> list(tel.keys())

['irv', 'guido', 'jack']

>>> sorted(tel.keys())

['guido', 'irv', 'jack']

>>> 'guido' in tel

True

>>> 'jack' not in tel

False

生成式

>>> {x: x**2 for x in (2, 4, 6)}

{2: 4, 4: 16, 6: 36}

1.6.    元素遍历

对于字典有些不同,存在items()方法来同时访问键和值。

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}

>>> for k, v in knights.items():

...     print(k, v)

...

gallahad the pure

robin the brave

enumerate()序列访问,该方法会同时返回元素位置下标和值。

>>> for i, v in enumerate(['tic', 'tac', 'toe']):

...     print(i, v)

...

0 tic

1 tac

2 toe

1.7.    条件判断

在while和if语句中可以使用带操作的条件判断式。

in and not in用于判断一个值是否存在于序列中。is用于判断两个对象是否是同一对象,需要注意区别。

a<b==c是合规的比较式,等于a<b and b ==c

and or 是可以在比较式中使用的,注意写法,简写一时爽,回看爽过头。

1.8.    序列之间比较

相同类型的序列之间可以比较。

比较原则:

  1. 先比较前面的元素,如果不同则给出结果;
  2. 如果相同,则按序逐个比较;
  3. 如果所有元素相等,则序列相等;
  4. 如果某一序列是另一序列的初始子序列,则较短的序列小;
  5. 字符串比较使用unicode编号来比较。

(1, 2, 3)              < (1, 2, 4)

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

'ABC' < 'C' < 'Pascal' < 'Python'

(1, 2, 3, 4)           < (1, 2, 4)

(1, 2)                 < (1, 2, -1)

(1, 2, 3)             == (1.0, 2.0, 3.0)

(1, 2, ('aa', 'ab'))   < (1, 2, ('abc', 'a'), 4)

猜你喜欢

转载自www.cnblogs.com/wodeboke-y/p/9696560.html