python数据结构

列表可以修改,字符串和元组不能

一、列表

1.1列表常用方法

1.1.1

list.append()元素插入到列表结尾 主要加入一个元素

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

[0, 1, 2, 3, 4, 12]

1.1.2

list.extend(L)拓展列表 主要加入多个元素

a.extend([6, 7, 8])
a

[0, 1, 2, 3, 4, 12, 6, 7, 8]

1.1.3

list.insert(i, x)在指定位置插入一个元素。第一个参数是准备插入到其前面的那个元素的索引

a.insert(0,-1)
a

[-1, 0, 1, 2, 3, 4, 12, 6, 7, 8]

1.1.4

list.remove(x)删除列表中值为 x 的第一个元素。如果没有这样的元素,就会返回一个错误。

a.append(-1)
a.remove(-1)
a

[0, 1, 2, 3, 4, 12, 6, 7, 8, -1]

1.1.5

list.pop([i])从列表的指定位置移除元素,并将其返回。如果没有指定索引,list.pop()返回最后一个元素。元素随即从列表中被移除。

b = a.pop(5)
print(b)
a

12

[0, 1, 2, 3, 4, 6, 7, 8, -1]

1.1.6

list.index(x)返回列表中第一个值为 x 的元素的索引。如果没有匹配的元素就会返回一个错误。

a.index(-1)

8

1.1.7

list.count(x)返回 x 在列表中出现的次数。

a.append(-1)
a.append(-1)
a.count(-1)

3

1.1.8

list.sort()对列表排序

a.sort()
a

[-1, -1, -1, 0, 1, 2, 3, 4, 6, 7, 8]

1.1.9

list.reverse()倒排列表中的元素。

a.reverse()
a

[8, 7, 6, 4, 3, 2, 1, 0, -1, -1, -1]

1.2将列表作为堆栈

#这两行代码用来输出多个输出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all' # 默认为'last',即输出最后一个结果

stack = [3, 4, 5]
stack.append(6)
stack.append(7)
stack
stack.pop()
stack
stack.pop()
stack.pop()
stack

[3, 4, 5, 6, 7]

7

[3, 4, 5, 6]

6

5

[3, 4]

1.3将列表作为队列

from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append("Terry")           # Terry arrives
queue.append("Graham")          # Graham arrives
queue.popleft()                 # The first to arrive now leaves
queue.popleft()                 # The second to arrive now leaves
queue                           # Remaining queue in order of arrival

'Eric'

'John'

deque(['Michael', 'Terry', 'Graham'])

二、集合

集合无序没有重复元素

可以用"{}"创建集合,但空集合只能用set(),不能用"{}",因为单纯一个"{}"创建的是字典

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)

{'banana', 'orange', 'apple', 'pear'}

'orange' in basket 

True

a = set('abracadabra')
b = set('alacazam')
a,b

({'a', 'b', 'c', 'd', 'r'}, {'a', 'c', 'l', 'm', 'z'})

c = {}
d = set()
type(c)
type(d)

dict

set

三、字典

tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
tel
tel['jack']
del tel['sape']
tel['irv'] = 4127
tel
list(tel.keys())
sorted(tel.keys())
'guido' in tel
'jack' not in tel

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

4098

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

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

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

True

False

# 用dict()将元组构建字典
dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])

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

3.1字典的遍历

在字典中遍历时,关键字和对应的值可以使用 items() 方法同时解读出来

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

gallahad the pure

robin the brave

同时遍历两个或更多的序列,可以使用 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值