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 {}
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- RobotFarmework+Python3环境搭建
- 如何用python进行邮件发送
- Python实现网页敏感词批量查找
- python实现多个网站站内链接批量导出
- 【python】斐波那契数列
- python编码问题
- Python ImportError: DLL load failed: %1 不是有效的 Win32 应用程序。
- 3步解决Python外部模块pycrypto安装错误问题
- python-return,break,continue,pass,exit( )用法
- 笨方法学python:from sys import argv 相关问题回答
- 20170915-python初识
- python学习笔记(执行方式)
- python学习笔记(变量类型)
- python学习笔记(变量计算和输入输出)
- python学习笔记(变量的命名)
- python学习笔记(if判断语句)
- python学习笔记(第一个案例石头剪刀布)
- python学习笔记(while循环)(赋值运算符)(第二个案例九九乘法表)(转义字符)
- python学习笔记(函数)
- python学习笔记(模块化)