- 浏览: 2486455 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
python入门(五)序列和字典
参考书籍 python_note_pdq.pdf
7.1 序列
letters = (1,)
print(letters,":",type(letters))
letters = (1)
print(letters,":",type(letters))
letters = (1,2,3,4,5,6)
print(letters,":",type(letters))
#用索引找寻元素和片段
print(letters[0],":",letters[1:3])
#不能直接修改0号的元素,只能通过如下方法修改
letters = (16,) + letters[1:]
print(letters)
控制台返回信息:
(1,) : <class 'tuple'>
1 : <class 'int'>
(1, 2, 3, 4, 5, 6) : <class 'tuple'>
1 : (2, 3)
(16, 2, 3, 4, 5, 6)
7.2 序列赋值
#python交换几个变量值的YD方法
a = 1
b = 2
c = 3
a,b,c=c,b,a
print(a,b,c)
a,b,c=a*b,b*c,a+b+c
print(a,b,c)
控制台返回信息:
3 2 1
6 2 6
7.3 序列作为返回值
def swap1(x,y):
return y,x
a,b=(1,2)
print(a,type(a),b,type(b))
print(swap1(a,b))
def swap2(x, y):
x, y = y, x
a = 1
b = 2
swap2(a, b)
print(a, b)
打印的结果:
1 <class 'int'> 2 <class 'int'>
(2, 1)
1 2
在Python中如果变量具有相同的值,就可以认为这些变量是同一个值的别名。也就是这些变量指向同一个
值。这样做的好处也许是节省了内存空间。但是如果某一个变量改变了它自己的值,那么解释器就开辟出新内存空间存储这个变量的值。这个变量就指向新的值。
a = 1
b = 1
print(id(a),id(b))
a = 13
print(id(a),id(b))
打印信息:
505389048 505389048
505389240 505389048
7.4 随机函数
import random
for i in range(3):
print(random.random())
打印结果如下:
0.868696815445
0.280806794298
0.349025563434
7.5 随机数列表
import random
def randomList(n):
s = [0]*n
for i in range(n):
s[i] = random.random();
return s
print(randomList(2))
打印输出信息:
[0.5529235083808711, 0.9325466671041306]
7.6 计数
import random
def randomList(n):
s = [0]*n
for i in range(n):
s[i] = random.random();
return s
def inBucket(list,low,high):
count = 0
for num in list:
if(low < num < high):
count = count + 1
return count
print(inBucket(randomList(2000),0.0,0.5))
计算了出线在0---0.5之间的几率,控制台打印信息1003
7.7 分割范围
import random
#产生随意数列
def randomList(n):
s = [0]*n
for i in range(n):
s[i] = random.random();
return s
#检查在某范围的随机数的个数
def inBucket(list,low,high):
count = 0
for num in list:
if(low < num < high):
count = count + 1
return count
#产生大量范围看分布
def rangeBucket(numBuckets,list):
buckets = [0] * numBuckets
bucketWidth = 1.0 / numBuckets
for i in range(numBuckets):
low = i * bucketWidth
high = low + bucketWidth
buckets[i] = inBucket(list, low, high)
return buckets
print(rangeBucket(20,randomList(2000)))
后台打印结果如下:
[105, 94, 100, 89, 98, 97, 112, 93, 109, 106, 96, 106, 97, 103, 104, 108, 96, 90, 99, 98]
8.0 字典
字典类似于组合类型,但是字典可以用整数和字符串来走索引,而组合类型只能用整数。
dict1 = {'mother': '妈妈', 'father': '爸爸'}
print(dict1," ",type(dict1))
dict2 = {1:'mother',2:'father'}
print(dict2," ",type(dict2))
dict3 = {}
#新增字典元素
dict3["test"]="测试"
dict3["hand"]="手"
#用key来访问对应的值,有点像java的map
print(dict3," ",dict3["test"])
dict3["test"]="测试(中文)"
print(dict3)
控制台返回信息:
{'father': '爸爸', 'mother': '妈妈'} <class 'dict'>
{1: 'mother', 2: 'father'} <class 'dict'>
{'test': '测试', 'hand': '手'} 测试
{'test': '测试(中文)', 'hand': '手'}
8.1字典操作
inventory = {'apples':430,'bananas':312,'oranges':525}
#del删除元素
del inventory['apples']
print(inventory)
#len返回字典元素的数量
print(len(inventory))
#clear方法,删除所有元素
inventory.clear()
print(inventory)
控制台消息:
{'oranges': 525, 'bananas': 312}
2
{}
8.2 别名和拷贝
opposites = {'up': 'down', 'right': 'wrong',\
'true': 'false'}
alias=opposites
print(id(alias)," ",id(opposites))
copy=opposites.copy()
print(id(copy)," ",id(opposites))
alias['right'] = 'left'
print(alias," ",opposites)
alias = (1,2,3)
print(alias," ",opposites)
控制台信息如下:
12173008 12173008
12877664 12173008
{'right': 'left', 'true': 'false', 'up': 'down'} {'right': 'left', 'true': 'false', 'up': 'down'}
(1, 2, 3) {'right': 'left', 'true': 'false', 'up': 'down'}
其中alias是别名,如果alias[0]元素改变,opposites[0]也会改变;而copy就是拷贝。
8.3 稀疏矩阵
matrix = [
[0,0,0,1,0],
[0,0,0,0,0],
[0,2,0,0,0],
[0,0,0,0,0],
[0,0,0,3,0] ]
#列表表示矩阵
print(matrix," ",type(matrix))
#字典表示举证,只指出非零的值
matrix = {(0,3): 1, (2, 1): 2, (4, 3): 3}
print(matrix," ",type(matrix))
#get方法得到值
print(matrix.get(0,3)," ",matrix.get(0,0))
控制台打印信息:
[[0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 3, 0]] <class 'list'>
{(0, 3): 1, (4, 3): 3, (2, 1): 2} <class 'dict'>
3 0
8.4 暗示
import time
#这个函数很耗时,n越大,计算次数越多
def fibonacci1(n):
if n == 0 or n == 1:
return 1
else:
return fibonacci1(n-1) + fibonacci1(n-2)
previous = {0:1, 1:1}
def fibonacci2(n):
if n in previous:
return previous[n]
else:
newValue = fibonacci2(n-1) + fibonacci2(n-2)
#每次计算把值追加到字典中了
previous[n] = newValue
return newValue
print(time.strftime("%Y-%m-%d %H:%M:%S"))
fibonacci1(31)
print(time.strftime("%Y-%m-%d %H:%M:%S"))
fibonacci1(32)
print(time.strftime("%Y-%m-%d %H:%M:%S"))
fibonacci2(50)
print(time.strftime("%Y-%m-%d %H:%M:%S"))
fibonacci2(60)
print(time.strftime("%Y-%m-%d %H:%M:%S"))
后台打印信息如下:
2010-01-13 20:22:07
2010-01-13 20:22:09
2010-01-13 20:22:13
2010-01-13 20:22:13
2010-01-13 20:22:13
貌似long和int长度的问题在python3里面没有出现,这里先不关心了。
由于这本电子书是python2的版本吧,我安装的是python3,所以可以在这里查询下差别:
http://diveintopython3.org/porting-code-to-python-3-with-2to3.html
8.5 计算字符串
letterCounts = {}
for letter in "Mississippiasdf":
#get后一个参数是给了个默认值
letterCounts[letter] = letterCounts.get(letter,0) + 1
print(letterCounts)
后台打印输出:
{'a': 1, 'd': 1, 'f': 1, 'i': 4, 'M': 1, 'p': 2, 's': 5}
参考书籍 python_note_pdq.pdf
7.1 序列
letters = (1,)
print(letters,":",type(letters))
letters = (1)
print(letters,":",type(letters))
letters = (1,2,3,4,5,6)
print(letters,":",type(letters))
#用索引找寻元素和片段
print(letters[0],":",letters[1:3])
#不能直接修改0号的元素,只能通过如下方法修改
letters = (16,) + letters[1:]
print(letters)
控制台返回信息:
(1,) : <class 'tuple'>
1 : <class 'int'>
(1, 2, 3, 4, 5, 6) : <class 'tuple'>
1 : (2, 3)
(16, 2, 3, 4, 5, 6)
7.2 序列赋值
#python交换几个变量值的YD方法
a = 1
b = 2
c = 3
a,b,c=c,b,a
print(a,b,c)
a,b,c=a*b,b*c,a+b+c
print(a,b,c)
控制台返回信息:
3 2 1
6 2 6
7.3 序列作为返回值
def swap1(x,y):
return y,x
a,b=(1,2)
print(a,type(a),b,type(b))
print(swap1(a,b))
def swap2(x, y):
x, y = y, x
a = 1
b = 2
swap2(a, b)
print(a, b)
打印的结果:
1 <class 'int'> 2 <class 'int'>
(2, 1)
1 2
在Python中如果变量具有相同的值,就可以认为这些变量是同一个值的别名。也就是这些变量指向同一个
值。这样做的好处也许是节省了内存空间。但是如果某一个变量改变了它自己的值,那么解释器就开辟出新内存空间存储这个变量的值。这个变量就指向新的值。
a = 1
b = 1
print(id(a),id(b))
a = 13
print(id(a),id(b))
打印信息:
505389048 505389048
505389240 505389048
7.4 随机函数
import random
for i in range(3):
print(random.random())
打印结果如下:
0.868696815445
0.280806794298
0.349025563434
7.5 随机数列表
import random
def randomList(n):
s = [0]*n
for i in range(n):
s[i] = random.random();
return s
print(randomList(2))
打印输出信息:
[0.5529235083808711, 0.9325466671041306]
7.6 计数
import random
def randomList(n):
s = [0]*n
for i in range(n):
s[i] = random.random();
return s
def inBucket(list,low,high):
count = 0
for num in list:
if(low < num < high):
count = count + 1
return count
print(inBucket(randomList(2000),0.0,0.5))
计算了出线在0---0.5之间的几率,控制台打印信息1003
7.7 分割范围
import random
#产生随意数列
def randomList(n):
s = [0]*n
for i in range(n):
s[i] = random.random();
return s
#检查在某范围的随机数的个数
def inBucket(list,low,high):
count = 0
for num in list:
if(low < num < high):
count = count + 1
return count
#产生大量范围看分布
def rangeBucket(numBuckets,list):
buckets = [0] * numBuckets
bucketWidth = 1.0 / numBuckets
for i in range(numBuckets):
low = i * bucketWidth
high = low + bucketWidth
buckets[i] = inBucket(list, low, high)
return buckets
print(rangeBucket(20,randomList(2000)))
后台打印结果如下:
[105, 94, 100, 89, 98, 97, 112, 93, 109, 106, 96, 106, 97, 103, 104, 108, 96, 90, 99, 98]
8.0 字典
字典类似于组合类型,但是字典可以用整数和字符串来走索引,而组合类型只能用整数。
dict1 = {'mother': '妈妈', 'father': '爸爸'}
print(dict1," ",type(dict1))
dict2 = {1:'mother',2:'father'}
print(dict2," ",type(dict2))
dict3 = {}
#新增字典元素
dict3["test"]="测试"
dict3["hand"]="手"
#用key来访问对应的值,有点像java的map
print(dict3," ",dict3["test"])
dict3["test"]="测试(中文)"
print(dict3)
控制台返回信息:
{'father': '爸爸', 'mother': '妈妈'} <class 'dict'>
{1: 'mother', 2: 'father'} <class 'dict'>
{'test': '测试', 'hand': '手'} 测试
{'test': '测试(中文)', 'hand': '手'}
8.1字典操作
inventory = {'apples':430,'bananas':312,'oranges':525}
#del删除元素
del inventory['apples']
print(inventory)
#len返回字典元素的数量
print(len(inventory))
#clear方法,删除所有元素
inventory.clear()
print(inventory)
控制台消息:
{'oranges': 525, 'bananas': 312}
2
{}
8.2 别名和拷贝
opposites = {'up': 'down', 'right': 'wrong',\
'true': 'false'}
alias=opposites
print(id(alias)," ",id(opposites))
copy=opposites.copy()
print(id(copy)," ",id(opposites))
alias['right'] = 'left'
print(alias," ",opposites)
alias = (1,2,3)
print(alias," ",opposites)
控制台信息如下:
12173008 12173008
12877664 12173008
{'right': 'left', 'true': 'false', 'up': 'down'} {'right': 'left', 'true': 'false', 'up': 'down'}
(1, 2, 3) {'right': 'left', 'true': 'false', 'up': 'down'}
其中alias是别名,如果alias[0]元素改变,opposites[0]也会改变;而copy就是拷贝。
8.3 稀疏矩阵
matrix = [
[0,0,0,1,0],
[0,0,0,0,0],
[0,2,0,0,0],
[0,0,0,0,0],
[0,0,0,3,0] ]
#列表表示矩阵
print(matrix," ",type(matrix))
#字典表示举证,只指出非零的值
matrix = {(0,3): 1, (2, 1): 2, (4, 3): 3}
print(matrix," ",type(matrix))
#get方法得到值
print(matrix.get(0,3)," ",matrix.get(0,0))
控制台打印信息:
[[0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 3, 0]] <class 'list'>
{(0, 3): 1, (4, 3): 3, (2, 1): 2} <class 'dict'>
3 0
8.4 暗示
import time
#这个函数很耗时,n越大,计算次数越多
def fibonacci1(n):
if n == 0 or n == 1:
return 1
else:
return fibonacci1(n-1) + fibonacci1(n-2)
previous = {0:1, 1:1}
def fibonacci2(n):
if n in previous:
return previous[n]
else:
newValue = fibonacci2(n-1) + fibonacci2(n-2)
#每次计算把值追加到字典中了
previous[n] = newValue
return newValue
print(time.strftime("%Y-%m-%d %H:%M:%S"))
fibonacci1(31)
print(time.strftime("%Y-%m-%d %H:%M:%S"))
fibonacci1(32)
print(time.strftime("%Y-%m-%d %H:%M:%S"))
fibonacci2(50)
print(time.strftime("%Y-%m-%d %H:%M:%S"))
fibonacci2(60)
print(time.strftime("%Y-%m-%d %H:%M:%S"))
后台打印信息如下:
2010-01-13 20:22:07
2010-01-13 20:22:09
2010-01-13 20:22:13
2010-01-13 20:22:13
2010-01-13 20:22:13
貌似long和int长度的问题在python3里面没有出现,这里先不关心了。
由于这本电子书是python2的版本吧,我安装的是python3,所以可以在这里查询下差别:
http://diveintopython3.org/porting-code-to-python-3-with-2to3.html
8.5 计算字符串
letterCounts = {}
for letter in "Mississippiasdf":
#get后一个参数是给了个默认值
letterCounts[letter] = letterCounts.get(letter,0) + 1
print(letterCounts)
后台打印输出:
{'a': 1, 'd': 1, 'f': 1, 'i': 4, 'M': 1, 'p': 2, 's': 5}
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 430NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 291Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 373Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 334Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 415NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 359Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 291Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 207GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 390GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 274GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 263Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 259Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 250Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 234Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 233NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 225Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 517NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 229Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 319Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 320Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
第七章 Python字典和集合 字典的表示 字典的使用 1 2 5 字典的遍历和嵌套 集合的表示和使用 4 字典的相关方法 3 第7章-python字典和集合-python基础-快速入门-课件教程全文共31页,当前为第1页。 1 字典的表示 ...
第Ⅱ部分 Python语言和标准库 第4章 做出决策 49 4.1 比较两个值是否相等 49 4.2 比较两个值是否不相等 51 4.3 比较两个值的大小 51 4.4 对真值和假值取反 53 4.5 观察多个比较运算的结果 54 4.6 循环 57 4.6.1 重复...
零基础入门学习Python P1. 000愉快的开始 P2. 001我和Python的第一次亲密接触 P3. 002用Python设计第一个游戏 P4. 003小插曲之变量和字符串 P5. 004改进我们的小游戏 P6. 005闲聊之Python的数据类型 P7. 006...
网盘文件永久链接 1-Python编程语言历史及...21-可调用对象,以及列表和字典的原处修改特性 22-Python的集合类型详解 23-容器数据类型特性总结 24-表达式与运算符 25-运算符优先级和Python语句概述 ............
字典由多个键及与其对应的值构成的对组成(把键值对成为项),每个键和它的值之间用冒号(:)隔开,项之间用逗号(,)隔开,而整个字典由一对大括号括起来。空字典由两个大括号组成:{} dict函数 可以用dict函数,...
python入门到高级全栈工程师培训视频学习资料;本资料仅用于学习,请查看后24小时之内删除。 【课程内容】 第1章 01 计算机发展史 02 计算机系统 03 小结 04 数据的概念 05 进制转换 06 原码补码反码 07 物理层和...
【编程】Python详细入门 一、选择题 1.王明编写了Python程序,根据输入的体重和身高计算BM值并显示是否超标。王明体重 80kg,身高170cm。其中"体重80kg""身高170cm"是对王明体重身高的符号记录属于( ) A.数据 B...
该文档是基于Python3.4的入门教程,简单介绍了Python(数据、字符串、列表等),流程控制(if,for,break等),数据结构(元组、序列、集合、字典),相关模块,输入输出控制格式,错误异常处理,类的描述、标准库...
6.Python.3开发环境的部署-Python虚拟环境的安装和使用Linux版 7.开发环境的部署-Python3解释器介绍和pythonl的简单使用 8.变量-变量命名规范和特性cmproj. 9.Python.3变量-多元复制和其他特性 10数据类型-序列类型...
001我和Python的第一次亲密接触 002用Python设计第一个游戏 003小插曲之变量和字符串 004改进我们的小游戏 005闲聊之Python的数据类型 006Pyhon之常用操作符 007了不起的分支和循环 008了不起的分支和循环2 009...
全书分为两个部分:第1部分占据了大约三分之二的篇幅,阐释这门语言的“核心”内容,包括基本的概念和语句、语法和风格、 Python对象、数字类型、序列类型、映射和集合类型、条件和循环、文件和输入/输出、错误和...
7.1.1 如何创建字典和给字典赋值 7.1.2 如何访问字典中的值 7.1.3 如何更新字典 7.1.4 如何删除字典元素和字典 7.2 映射类型操作符 7.2.1 标准类型操作符 7.2.2 映射类型操作符 ...
001我和Python的第一次亲密接触 002用Python设计第一个游戏 003小插曲之变量和字符串 004改进我们的小游戏 005闲聊之Python的数据类型 006Pyhon之常用操作符 007了不起的分支和循环 008了不起的分支和循环2 009...
对象与类的快速入门 使用列表 元组 使用元组 元组与打印语句 字典 使用字典 序列 使用序列 参考 对象与参考 更多字符串的内容 字符串的方法 概括 10. 解决问题——编写一个Python脚本 问题 ...
7.1.1 如何创建字典和给字典赋值 7.1.2 如何访问字典中的值 7.1.3 如何更新字典 7.1.4 如何删除字典元素和字典 7.2 映射类型操作符 7.2.1 标准类型操作符 7.2.2 映射类型操作符 ...
7.1.1 如何创建字典和给字典赋值 7.1.2 如何访问字典中的值 7.1.3 如何更新字典 7.1.4 如何删除字典元素和字典 7.2 映射类型操作符 7.2.1 标准类型操作符 7.2.2 映射类型操作符 ...
笔记_小甲鱼零基础入门学习python系列教程 1 目录 2 序 4 01讲:我和Python的第一次亲密接触 7 02讲:用Python设计第一个游戏 9 03讲:小插曲之变量和字符串 11 04讲:改进我们的小游戏 13 05讲:闲聊之Python的数据...
在Python中,有四大数据结构:列表List、元组Tuple、集合Set、字典Dict 因为软件需要大量的代码去解决需求,简单的代码不一定能解决问题,四大数据结构的结合调用可以完成需要的功能。 一.列表 List 1.什么是列表? ...
7.1.1 如何创建字典和给字典赋值 7.1.2 如何访问字典中的值 7.1.3 如何更新字典 7.1.4 如何删除字典元素和字典 7.2 映射类型操作符 7.2.1 标准类型操作符 7.2.2 映射类型操作符 ...