Python(四)语法(list有序可变列表,tuple有序不可变列表,dict字典【MAP】,set)

list

Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。

>>> list=[0,1,2,3,4,5]
>>> list
[0, 1, 2, 3, 4, 5]

获取list长度

>>> len(list)
5

用索引来访问list中每一个位置的元素,记得索引是从0开始的

>>> list[0]
1

索引为负数时从后往前取值

>>> list[-1]
5
>>> list[-2]
4

添加元素list是一个可变的有序表,所以,可以往list中追加元素到末尾

>>> list.append(6)
>>> list
[1, 2, 3, 4, 5, 6]

把元素插入到指定的位置,比如索引号为0的位置插入数字0

>>> list.insert(0,0)
>>> list
[0, 1, 2, 3, 4, 5, 6]

删除list末尾的元素,用pop()方法

>>> list.pop()
6
>>> list
[0, 1, 2, 3, 4, 5]

删除指定位置的元素,用pop(i)方法,其中i是索引位置

>>> list.pop(5)
5
>>> list
[0, 1, 2, 3, 4]

替换指定位置的元素,可以直接赋值给对应的索引位置

>>> list
[0, 1, 2, 3, 4]
>>> list[0]=1
>>> list
[1, 1, 2, 3, 4]

list中的元素不一定要是同一类型

>>> list
[1, 1, 2, 3, 4]
>>> list[0]="abc"
>>> list
['abc', 1, 2, 3, 4]

list元素也可以是另一个list,取值的时候可以list[i][i]类似于二维数组

>>> a=['a','b','c']
>>> a
['a', 'b', 'c']
>>> list[0]=a
>>> list
[['a', 'b', 'c'], 1, 2, 3, 4]
>>> list[0]
['a', 'b', 'c']
>>> list[0][0]
'a'

tuple

另一种有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改

不可变的tuple有什么意义?因为tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple

如下tuple不能变了,它也没有append(),insert()这样的方法。其他获取元素的方法和list是一样的

>>> list=(1,2,3,4)
>>> list
(1, 2, 3, 4)
>>> list[1]
2
>>> list[-1]
4
>>> list[1]=0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> list.append(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'

空的tuple

>>> list=()
>>> list
()

一个元素的tuple要注意一定要加逗号","。这是因为括号()既可以表示tuple,又可以表示数学公式中的小括号,这就产生了歧义,因此,Python规定,这种情况下,按小括号进行计算

>>> list=(1)
>>> list
1
>>> list=(1,)
>>> list
(1,)
>>>

当然tuple内部的对象是可以改变的 如下

>>> list=(1,2,[3,4,5])
>>> list
(1, 2, [3, 4, 5])
>>> list[2][0]=1
>>> list
(1, 2, [1, 4, 5])
>>>

表面上看,tuple的元素确实变了,但其实变的不是tuple的元素,而是list的元素。tuple一开始指向的list并没有改成别的list,所以,tuple所谓的“不变”是说,tuple的每个元素,指向永远不变。即指向'a',就不能改成指向'b',指向一个list,就不能改成指向其他对象,但指向的这个list本身是可变的!

 

dict---->MAP

Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储具有极快的查找速度

>>> map={'key1':1,'key2':2,'key3':3}
>>> map
{'key1': 1, 'key2': 2, 'key3': 3}

添加元素

>>> map['key4']=4
>>> map
{'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4}

取值

map[key]:如果key不存在会报错

map.get(key)  :如果key不存在返回none 注意:返回None的时候Python的交互环境不显示结果,但在在编译器中会显示none

>>> map
{'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4}
>>> map['key5']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'key5'
>>> 'key5' in map
False
>>> 'key4' in map
True
>>> map.get('key4')
4
>>> map.get('key5')
>>>

删除 pop(key)

>>> map.pop('key4')
4
>>> map
{'key1': 1, 'key2': 2, 'key3': 3}

和list比较,dict有以下几个特点:

  1. 查找和插入的速度极快,不会随着key的增加而变慢;
  2. 需要占用大量的内存,内存浪费多。

list:

  1. 查找和插入的时间随着元素的增加而增加;
  2. 占用空间小,浪费内存很少。

所以,dict是用空间来换取时间的一种方法

set

set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。

要创建一个set,需要提供一个list作为输入集合

>>> s=set([3,2,3,1,3,2])
>>> s
{1, 2, 3}

注意:传入的参数[3,2,3,1,3,2]是一个list,而显示的{1, 2, 3}只是告诉你这个set内部有1,2,3这3个元素,显示的顺序也不表示set是有序的。重复元素在set中自动被过滤

通过add(key)方法可以添加元素到set中,可以重复添加,但不会有效果

>>> s
{1, 2, 3}
>>> s.add(3)
>>> s
{1, 2, 3}
>>> s.add(4)
>>> s
{1, 2, 3, 4}
>>>

通过remove(key)方法可以删除元素,删除没有的元素会报错

{1, 2, 3, 4}
>>> s.remove(1)
>>> s
{2, 3, 4}
>>> s.remove(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1

set可以看成数学意义上的无序和无重复元素的集合,因此,两个set可以做数学意义上的交集、并集等操作

>>> a=set([1,2,3])
>>> b=set([3,4,5])
>>> a&b
{3}
>>> a|b
{1, 2, 3, 4, 5}

猜你喜欢

转载自blog.csdn.net/baiyan3212/article/details/84030712