关于Python学习之 列表与字典
列表
列表是Python中最具灵活性的有序集合对象类型。
# 列表迭代和解析
>>> res = [c*4 for c in 'Spam']
>>> res
['SSSS', 'pppp', 'aaaa', 'mmmm'
>>> res = []
>>> for c in 'Spam':
... res.append(c*4)
...
>>> res
['SSSS', 'pppp', 'aaaa', 'mmmm']
>>> list(map(abs,[-1,-2,0,1,2]))
[1, 2, 0, 1, 2]
# 一般操作
>>> L = [5,3,6,2,8]
>>> sorted(L)
[2, 3, 5, 6, 8]
>>> L
[5, 3, 6, 2, 8]
>>> L.sort()
>>> L
[2, 3, 5, 6, 8]
>>> L.insert(0,1)
>>> L
[1, 2, 3, 5, 6, 8]
>>> L.reverse()
>>> L
[8, 6, 5, 3, 2, 1]
>>>
>>> del L[0]
>>> L
[6, 5, 3, 2, 1]
>>> L.pop()
1
>>> L
[6, 5, 3, 2]
>>> L.remove(6)
>>> L
[5, 3, 2]
'''
原处修改列表:因为Python只处理对象引用,所以需要将原处修改一个对象与生成的一个新对象区分开来。
因为在原处修改一个对象时,可能同时会影响一个以上指向它的引用。
'''
# 其他
>>> L = ['already','got','one']
>>> L
['already', 'got', 'one']
>>> L[1:]=[]
>>> L
['already']
>>> L[0]=[]
>>> L
[[]]
字典
如果把列表看成是有序的对象集合,字典可以当成是无序的集合。主要区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
>>> D = {'food':{'ham':1,'egg':2}}
>>> D.get('food')
{'ham': 1, 'egg': 2}
>>> D2 = {'a':1,'b':2}
>>> D.update(D2)
>>> D
{'food': {'ham': 1, 'egg': 2}, 'a': 1, 'b': 2}
>>> D.pop('b')
2
>>> len(D)
2
>>> D
{'food': {'ham': 1, 'egg': 2}, 'a': 1}
>>> del D['a']
>>> D
{'food': {'ham': 1, 'egg': 2}}
>>> D = {x:x*2 for x in range(10)}
>>> D
{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
>>> D = {'spam':2,'ham':1,'eggs':3}
>>> D['spam']
2
>>> D
{'spam': 2, 'ham': 1, 'eggs': 3}
>>> len(D)
3
>>> 'ham' in D
True
>>> list(D.keys())
['spam', 'ham', 'eggs']
>>> list(D.values())
[2, 1, 3]
>>> D
{'spam': 2, 'ham': 1, 'eggs': 3}
>>> D['ham']=['grill','bake','fry']
>>> D无锡看男科医院哪家好 https://yyk.familydoctor.com.cn/20612/
{'spam': 2, 'ham': ['grill', 'bake', 'fry'], 'eggs': 3}
>>> del D['eggs']
>>> D
{'spam': 2, 'ham': ['grill', 'bake', 'fry']}
>>> D['brunch'] = 'Bacon'
>>> D
{'spam': 2, 'ham': ['grill', 'bake', 'fry'], 'brunch': 'Bacon'}
>>> list(D.items())
[('spam', 2), ('ham', ['grill', 'bake', 'fry']), ('brunch', 'Bacon')]
>>> D2 = {'toast':4,'muffin':5}
>>> D.update(D2)
>>> D
{'spam': 2, 'ham': ['grill', 'bake', 'fry'], 'brunch': 'Bacon', 'toast': 4, 'muffin': 5}
>>> table = {'Python':'Guido van Rossum',
... 'Perl':'Larry Wall',
... 'Tcl':'John Ousterhout'}
>>>
>>> language = 'Python'
>>> creator = table[language]
>>> creator
'Guido van Rossum'
>>> for lang in table:
... print(lang,'\t',table[lang])
...
Python Guido van Rossum
Perl Larry Wall
Tcl John Ousterhout
# 三种方法来避免missing-key错误
... if (2,3,6) in Matrix:
... print(Matrix[(2,3,6)])
... else:
... print(0)
...
0
>>>
>>>
>>> try:
... print(Matrix[(2,3,6)])
... except KeyError:
... print(0)
...
0
>>>
>>>
>>> Matrix.get((2,3,4),0)
98
>>> Matrix.get((2,3,6),0)
0
关于Python学习之 列表与字典的更多相关文章
- python学习之列表和字典
列表 基本操作>>>len([1,3,4])3 >>>[1,2,3]+[4,5,6] +号两边必须是相同类型[1,2,3,4,5,6] >>> ...
- Python学习三|列表、字典、元组、集合的特点以及类的一些定义
此表借鉴于他人 定义 使用方法 列表 可以包含不同类型的对象,可以增减元素,可以跟其他的列表结合或者把一个列表拆分,用[]来定义的 eg:aList=[123,'abc',4.56,['inner', ...
- 【python学习笔记】4.字典:当索引不好用时
[python学习笔记]4.字典:当索引不好用时 字典是python中唯一内建的map类型 创建: key可以为任何不可改变的类型,包括内置类型,或者元组,字符串 通过大括号: phonebook={ ...
- Python学习02 列表 List
Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...
- python字符串、列表和字典的说明
python字符串.列表和字典的说明 字符串.列表.字典 字符串的作用存储一段数据信息.例如 info = '我爱北京天安门' ,在调取的时候可以直接调取,灵活方便,print(info) 就可以把刚 ...
- Python学习笔记----列表、元组和字典的基础操作
文章目录 一.列表的基本操作 1.1 修改列表中的某个数据 1.2 获取某个元素的返回值(返回的是索引) 1.3 在列表中插入新的元素 1.4 删除列表中的元素 1.5 +和* 二.内置的函数和方法 ...
- Python 1.2 列表和字典基础
一. List创建.索引.遍历和内置增删函数 1.列表是Python的内置可变对象,由Array实现,支持任意类型的添加.组合和嵌套. L = [] # list declare L = [1, 1. ...
- [转载]Python 元组、列表、字典、文件
python的元组.列表.字典数据类型是很python(there python is a adjective)的数据结构.这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的益 ...
- Python学习(11)字典
目录 Python 字典 访问字典中的值 修改字典 删除字典元素 字典键的特性 字典内置函数&方法 Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. ...
随机推荐
- string, byte[], Base64String相互转化
直接使用.NET中的的库类函数 方法: ///<summary> ///Base64加密 ///</summary> ///<paramname="Messag ...
- google cloud storage products
https://cloud.google.com/products/storage/ BigTable Cloud Bigtable 是 Google 面向大数据领域的 NoSQL 数据库服务.它也是 ...
- 【错误解决】git报错:you are not allowed to push code to protected branches on this project
场景回忆: 本地修改需要退回到之前的版本,打算强制push本地版本覆盖远程版本,但是在git push --force后出现了以下的错误: Fix GitLab error: "you ar ...
- 生成pcf文件
import os import datetime import hashlib def checksum(filename): with open(filename, mode='rb') as f ...
- 在使用Hanlp配置自定义词典时遇到的问题
要使用hanlp加载自定义词典可以通过修改配置文件hanlp.properties来实现.要注意的点是: 1. root根路径的配置: hanlp.properties中配置如下: #本配置文件中的路 ...
- python的帮助信息的写法
# coding = utf-8from optparse import OptionParserfrom optparse import OptionGroup usage = 'Usage: %p ...
- PHP对二维数组进行排序
/** * 获取最近的店铺 * @param $lng * @param $lat * @return array */ protected function getClosestShop($lng, ...
- Java实现RSA加密&AES加密&DES加密
RSA package com.demo; import org.springframework.util.StringUtils; import javax.crypto.Cipher; impor ...
- RocketMQ 4.5.1 单机环境搭建以及生产消费测试
为了学习和方便测试,总是要启动一个单机版的.下载 http://rocketmq.apache.org/dowloading/releases/ 1. 要先配置环境变量 ROCKETMQ_HOME E ...
- Python实现树
树 (tree) 是一种非常高效的非线性存储结构.树,可以很形象的理解,有根,有叶子,对应在数据结构中就是根节点.叶子节点,同一层的叶子叫兄弟节点,邻近不同层的叫父子节点,非常好理解. 注:定义来自百 ...