您的位置:首页 > 理论基础 > 数据结构算法

Python数据结构详解(列表、字典、元组、集合)

2019-03-04 15:15 344 查看

Python的数据结构(列表、字典、元组、集合)

列表:list_=[‘apple’,‘mango’,‘carrot’,‘banana’]
字典:dir_={‘a’:123,‘b’:‘something’}
集合:set_={‘apple’,‘pear’,‘apple’}
元组:tuple_=(123,456,‘hello’)

一、列表

1.1基本知识

空列表:a=[]
函数方法

函数方法 作用|备注
list.append(object) 如未说明,请查阅python关于Data Structures的开发文档,下同
https://docs.python.org/3/tutorial/datastructures.html#
list.extend(iterable) 迭代器是一个对象,提供了一种访问一个容器对象中各个元素的方法
这个方法的作用等同于a[len(a):]=iterable
list.insert(index,object)
list.remove(x) Remove the first item from the list whose value is equal to x.
list.pop()
list.pop(index)
list.clear()
list.index(x,start,stop) Return zero-based index in the list (from start to stop) of the first item
whose value is equal to x
list.cout(x) Return the number of times x appeared in the list
list.sort()
list.reverse()
list.copy() Return a shallow copy of the list. Equivalent to a[:]
del list[start:stop]
del list

列表推导式

vec = [2,4,6]
[3*x for x in vec if x<6] >>>[6,12]
vec2 = [1,2,3]
[x*y for x in vec for y in vec2] >>>[2,4,6,4,8,12,6,12,18]

1.2列表的栈、队列实现

1.2.1列表的栈实现

//list实现栈
stack=[list]
stack.append(object)	//入栈
stack.pop() 			//出栈
//deque实现栈
from collections import deque
stack=deque(list)
stack.append(x)			//入栈
stack.pop()				//出栈

1.2.2列表的队列实现

from collections import deque
queue=deque(list)
queue.append(x)	//入栈
queue.popleft()	//出栈

二、字典(Mapping Types – dict)

2.1基本知识

空字典 dict_={}
字典通过key键来进行索引,key可以是任何不可变的类型,包括字符、数字、不可变的元组。
如果你使用已经存在的key来进行存储,那原有的(key,value)将会被遗忘。
函数方法:

函数方法 作用|备注
del dict_[key]
key (not) in dict_
iter(dict_) 返回字典key的迭代器
dict_items() Return a new view of the dictionary’s keys
dict_.pop(key) if key is in the dictionary, remove it and return its value
dict_.popitem() Remove and return a (key,value) pair , pairs are returned in LIFO order
dict_.setdefault(key) If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.
dict_.update([other]) Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.
dict_.values()
list(dict_.keys())
for key,value in dict_.items()
dict_[key] 返回key对应的value
dict_.get(key) 同上

三、元组

3.1基本知识

元组是由许多由逗号隔开的值构成:t=1,2,3,‘hello’
元组和列表的区别在于,元组是不可变的有序序列,列表是可变的有序序列。
函数方法

函数方法 作用|备注
t.count(value) 统计元组中value出现的次数
t.index(value) 返回元组中value的位置

四、集合

4.1基本知识

集合是一种不带重复元素的无序聚集,集合支持成员测试、成员除重;集合类还支持和、并、交等数学操作。
空集合 set_={}
函数方法

函数方法 作用|备注
set_.pop()
set_.remove(element)
set_.add(element)
set_.clear()

五、遍历技术

5.1对于字典

knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for k, v in knights.items():
print(k, v)

>>>gallahad the pure
>>>robin the brave

5.2对于序列

使用enumerate()函数,下标和对应的value可以同时被获取:

>>> for i, v in enumerate(['tic', 'tac', 'toe']):
...     print(i, v)
...
0 tic
1 tac
2 toe

5.3对于多序列

同时遍历多序列,可以通过zip()函数实现:

>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
...     print('What is your {0}?  It is {1}.'.format(q, a))
...
What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue.

5.4有序遍历

sorted()函数返回有序序列:

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
...     print(f)
...
apple
banana
orange
pear
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: