您的位置:首页 > 编程语言 > Python开发

python 列表 元组 字典

2020-02-17 12:14 706 查看

python 列表 元组 字典

一、列表 - listlistlist

[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’) #移除已知元素

>>> 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]
[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]

二、元组 - tupletupletuple

元组是小括号,列表是中括号,字典是大括号

>>> t = ('wzl',19,220)
>>> type(t)
<clas
1bb8c
s 'tuple'>
>>> t[0]
'wzl'

元组和列表的转换

>>> r = list(t)
>>> r
['wzl', 19, 220]

元组也可以切片

>>> t[1:3]
(19, 220)

三、字典 - dictionarydictionarydictionary

[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
{}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
secx=1_cosx 发布了2 篇原创文章 · 获赞 0 · 访问量 83 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: