python 列表 元组 字典 集合
一、列表 - l i s t list list
[0] 列表与元组
1、列表与元组可以相互转换,但列表可修改,元组不可修改
2、时间和空间
列表:空间来存储,但是耗时少,增添删改的时间短
元组:不占空间,但是很耗时间
[1] 列表的增加
fruit.insert(0,‘pineapple’) #添加在左边
>>> fruit.insert(0,'pineapple')
>>> fruit
['pineapple', 'apple', 'banana', 'orange', 'strawberry', 'grape']
fruit.append(‘pineapple’) #添加在末尾
>>> fruit.append('blueberry')
>>> fruit
['pineapple', 'apple', 'banana', 'orange', 'strawberry', 'grape', 'blueberry']
>>>
list_bb = []
for i in range(1,10):
list_bb.append(i)
print(list_bb)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
字符串的组合:
result = []
a = 'ABCDEF'
b = '123456789'
c = min(len(a),len(b))
for i in range(c):
result.append(a[i]+b[i])
print(result)
['A1', 'B2', 'C3', 'D4', 'E5', 'F6']
>>> fruit = ['apple','banana','orange','strawberry','grape']
>>> for i in fruit:
print(i.title(),end=' ')
Apple Banana Orange Strawberry Grape
[2] 列表的删除
fruit.remove(‘apple’) #移除已知元素
aa=[5,3,1,9,7]
a = max(aa)
aa.remove(a)
b = max(aa)
print(b)
7
>>> fruit = ['apple','banana','orange','strawberry','grape']
>>> fruit.remove('apple')
>>> fruit
['banana', 'orange', 'strawberry', 'grape']
fruit.pop() #删除最后一个元素,并返回最后一个元素的值(默认-1)
>>> fruit = ['apple','banana','orange','strawberry','grape']
>>> fruit.pop()
'grape'
>>> fruit
['apple', 'banana', 'orange', 'strawberry']
fruit.pop(0) 则为删除第一个元素,并返回第一个元素
[3] 列表的修改
[4] 列表的排列
list.sort()
>>> a = [1,3,4,7,6,5]
>>> list.sort(a)
>>> a
[1, 3, 4, 5, 6, 7]
list.sort(reverse=True) #从大到小排列,默认是false
>>> a.sort(reverse=True)
>>> a
[7, 6, 5, 4, 3, 1]
只想打印排序后的列表,不想改变原列表
>>> a = [1,3,4,7,6,5]
>>> b = [i for i in a]
>>> b.sort()
>>> b
[1, 3, 4, 5, 6, 7]
>>> a
[1, 3, 4, 7, 6, 5]
>>> list1 = ['orange','apple','banana','blueberry']
>>> list1.sort()
>>> list1
['apple', 'banana', 'blueberry', 'orange']
[5] 列表的其他
1)整合列表
>>> b = [1,2,3,4,5,6]
>>> ''.join(str(i) for i in b)
'123456'
2)整合两个列表:b+c
>>> c = [7,8,2]
>>> b+c
[1, 2, 3, 4, 5, 6, 7, 8, 2]
>>> d = [i*2 for i in b]
3)两倍
>>> b = [1,2,3,4,5,6]
>>> d = [i*2 for i in b]
>>> d
[2, 4, 6, 8, 10, 12]
·
·
二、元组 - t u p l e tuple tuple
元组是小括号,列表是中括号,字典是大括号
>>> t = ('wzl',19,220)
>>> type(t)
<class 'tuple'>
>>> t[0]
'wzl'
元组和列表的转换
>>> r = list(t)
>>> r
['wzl', 19, 220]
元组也可以切片
>>> t[1:3]
(19, 220)
·
·
三、字典 - d i c t i o n a r y dictionary dictionary
[0] 字典
字典
>>> price={'cabbage':3,'tomato':6,'carrot':1,'bean':8}
>>> price
{'cabbage': 3, 'tomato': 6, 'carrot': 1, 'bean': 8}
字典的操作
>>> len(price)
4
>>> for element in price:
print(element)
cabbage
tomato
carrot
bean
>>> price['tomato']
6
>>> for element in price:
print('{}的价格是{}'.format(element,price[element]))
cabbage的价格是3
tomato的价格是6
carrot的价格是1
bean的价格是8
>>>
key:value,
>>> price={'cabbage':3,'tomato':6,'carrot':1,'bean':8}
>>> price.keys()
dict_keys(['cabbage', 'tomato', 'carrot', 'bean'])
>>> price.values()
dict_values([3, 6, 1, 8])
>>> price.items()
dict_items([('cabbage', 3), ('tomato', 6), ('carrot', 1), ('bean', 8)])
>>> for ele in price.items():
print(ele[0],ele[1])
cabbage 3
tomato 6
carrot 1
bean 8
[1] 字典的修改
price[‘key’]=value
>>> price['tomato']=2
>>> price
{'cabbage': 3, 'tomato': 2, 'carrot': 1, 'bean': 8}
price.update(tomato=3) #字符名称不用加单引号
>>> price.update(tomato=3)
>>> price
{'cabbage': 3, 'tomato': 3, 'carrot': 1, 'bean': 8}
[2] 字典的增加
price.update(apple=5) #update后面是未知的
>>> price.update(apple=5)
>>> price
{'cabbage': 3, 'tomato': 3, 'carrot': 1, 'bean': 8, 'apple': 5}
[3] 字典的删除
price.pop(‘apple’)
>>> price.pop('apple')
5
>>> price
{'cabbage': 3, 'tomato': 3, 'carrot': 1, 'bean': 8}
price.popitem()
>>> price.popitem()
('bean', 8)
>>> price
{'cabbage': 3, 'tomato': 3, 'carrot': 1}
[4] 字典的查询
price.get(‘tomato’)
price={'cabbage':3,'tomato':3,'carrot':1,'bean':8}
>>> price.get('tomato')
3
price.pop(‘apple’)
>>> price.pop('apple')
5
>>> price
{'cabbage': 3, 'tomato': 3, 'carrot': 1, 'bean': 8}
[5] 字典的清空
price.clear()
>>> price.clear()
>>> price
{}
[6] 字典的操作们
# 0212
tele={'张三':10010110,'李四':19018892,'王五':18920032,'白一':176230992,'关二':18020210}
tele.pop('白一')
for element in tele:
print('{}的电话号码是{}'.format(element,tele[element]))
# the another way
for key,value in tele.items():
print('{}的电话号码是{}'.format(key,value))
print(tele.items())
print(*zip(tele.keys(),tele.values())) # the second way of printing
·
·
四、集合 - s e t set set
[0] 字典
确定性
给定一个集合,任给一个元素,该元素或者属于或者不属于该集合,二者必居其一,不允许有模棱两可的情况出现。
互异性
一个集合中,任何两个元素都认为是不相同的,即每个元素只能出现一次。有时需要对同一元素出现多次的情形进行刻画,可以使用多重集,其中的元素允许出现多次。
无序性
一个集合中,每个元素的地位都是相同的,元素之间是无序的。集合上可以定义序关系,定义了序关系后,元素之间就可以按照序关系排序。但就集合本身的特性而言,元素之间没有必然的序。
>>> set1 = {1,2,3,4,5,6}
>>> set1
{1, 2, 3, 4, 5, 6}
>>> type(set1)
<class 'set'>
[1] 字典的增添
xxx = set(range(m,n))
>>> set2 = set(range(1,10))
>>> set2
{1, 2, 3, 4, 5, 6, 7, 8, 9}
xxx.add()
>>> set2.add(10)
>>> set2
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
xxx.update([m,n,o,p,q])
>>> set2.update([11,12])
>>> set2
{2, 3, 4, 5, 6, 7, 8, 9, 11, 12}
>>> set2.update([3,4,34])
>>> set2
{2, 3, 4, 5, 6, 7, 8, 9, 34, 11, 12}
[2] 字典的删除
xxx.remove()
>>> set2.remove(10)
>>> set2
{1, 2, 3, 4, 5, 6, 7, 8, 9}
setx.discard()
>>> set2.add(1000)
>>> set2
{2, 3, 4, 5, 6, 7, 8, 9, 34, 11, 12, 1000}
>>> set2.discard(1000)
>>> set2
{2, 3, 4, 5, 6, 7, 8, 9, 34, 11, 12}
- discard 与 remove 的区别:
若删除一个并不存在的数字,discard不会报错,remove会报错。
>>> set2
{2, 3, 4, 5, 6, 7, 8, 9, 34, 11, 12}
>>> set2.remove(99)
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
set2.remove(99)
KeyError: 99
>>> set2.discard(99)
>>> set2
{2, 3, 4, 5, 6, 7, 8, 9, 34, 11, 12}
setx.pop() - 删除第一个元素
>>> set2.pop()
1
>>> set2
{2, 3, 4, 5, 6, 7, 8, 9}
listx.pop() - 删除最后一个元素
>>> list2.pop()
9
>>> list2
[1, 2, 3, 4, 5, 6, 7, 8]
[3] 字典的查找
>>> set2.update([11,12])
>>> set2
{2, 3, 4, 5, 6, 7, 8, 9, 11, 12}
>>> set2.update([3,4,34])
>>> set2
{2, 3, 4, 5, 6, 7, 8, 9, 34, 11, 12}
集合是无序的,所以增加是随机位置增加,列表与元组是有序的
[4] 字典的其他
>>> for element in set2:
print(element**2)
4
9
16
25
36
49
64
81
1156
121
144