惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
D
Docker
J
Java Code Geeks
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
腾讯CDC
罗磊的独立博客
U
Unit 42
爱范儿
爱范儿
Vercel News
Vercel News
MyScale Blog
MyScale Blog

Posts on WKLKEN THINKING

apisix 中的 lrucache apisix 中的服务发现机制 apisix 中的负载均衡 apisix etcd机制 聊聊框架 关于 k8s 的 zero downtime deployment 一些建议 apisix 遇到的一些问题 关于在除夕前一天换了一个洗衣机的故事 Django DRF 性能优化 DRF 的一些实践 Part1: Serializer DRF继承关系图 Better Code: 关于接口的灵活性 新的仓库: wklken/naming 缓存使用的一些经验 Better Code: 抽象: 可扩展性与可维护性的抉择 Better Code: 异常时, 该提示用户哪些信息? Better Code: 更好的异常日志打印 Go: some libs Go: go-redis/cache升级的坑 Go: logrus性能提升 Go: gin validation 远程办公的一点总结 Go: 开发过程中的一些bug 项目管理实践: 风险驱动开发 Go: 一种error wrap调用链处理方式 漫谈技术选型 Go: 基于 apitest 做handler层单元测试 Go: go-sql-driver interpolateparams参数优化 [分享]深度工作 你需要更多的思考时间
Python-基础-元组小结
2013-03-09 · via Posts on WKLKEN THINKING

##元组

###简介

tuple

1.元组是以圆括号“()”包围的数据集合,不同成员以“,”分隔。通过下标进行访问

2.不可变序列,可以看做不可变的列表,与列表不同:元组中数据一旦确立就不能改变(所以没有类似列表的增删改操作,只有基本序列操作)

3.支持任意类型,任意嵌套以及常见的序列操作

4.元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变

###声明及使用

:::python
t = ()  #空元组
t =(1,)  #单个元素元组,注意逗号必须
t =(1,2,3)


1 in t #判断
2 not in t

#其他同序列基本操作:分片,索引
print t[0]
print t[-1]
print t[:2]

#不会对原来元组造成影响
print t+(4,5)  #返回新元组(1,2,3,4,5)
print t * 2    #(1,2,3,1,2,3)
t.index(1)
t.count(1)

#列表元组转换
l = [1,2,3]
lt = tuple(l)
tl = list(lt)
lt_sorted = sorted(l)  #对元组进行排序,返回是列表

#字符串转元组(得到字符元组序列)
print tuple('hello)   #('h','e','l','l','o')

tuple没有append/extend/remove/pop等增删改操作 tuple没有find

查看帮助

:::python
help(tuple)

###用途

1.赋值

:::python
t = 1,2,3   #等价 t = (1, 2, 3)
x, y, z = t   #序列拆封,要求左侧变量数目和右侧序列长度相等

2.函数多个返回值

:::python
def test():
    return (1,2)

x, y = test()

3.传参[强制不改变原始序列]

:::python
def print_list(l):
    t = tuple(l)   #或者t = l[:]
    dosomething()

4.字符串格式化

:::python
print '%s is %s years old' % ('tom', 20)

5.作为字典的key

###优点 1.性能

tuple比列表操作速度快

若需要定义一个常量集,或者是只读序列,唯一的操作是不断遍历之,使用tuple代替list

>>> a = tuple(range(1000))
>>> b = range(1000)
>>> def test_t():
...     for i in a:
...             pass
...
>>> def test_l():
...     for i in b:
...             pass
...
>>> from timeit import Timer
>>> at = Timer("test_t()", "from __main__ import test_t")
>>> bt = Timer("test_l()", "from __main__ import test_l")

简单测试

>>> at.repeat(3, 100000)
[1.526214838027954, 1.5191287994384766, 1.5181210041046143]

>>> bt.repeat(3, 100000)
[1.5545141696929932, 1.557785987854004, 1.5511009693145752]

2.不可变性

对不需要的数据进行“写保护”,使代码更加安全

不可变性,若在程序中以列表形式传递对象集合,可能在任何地方被改变,使用元组,则不能

不可变性只适用于元组本身顶层而非其内容,例如元组内部的列表可以修改

:::python
l = [1,2,3]
t = (1,2,l)
l.append(4)

不可变性提供了某种完整性,规范化,确保不会被修改,保持某种固定关系

修改的方法

tuple -> list -> tuple

补充

元组定义易错点

2013-08-31

一个和多个的区别是定义1个后面必须有",“否则就是 类型 “str”

>>> t = ("a")
>>> type (t)
<type 'str'>
>>> t = ("a",)
>>> type (t)
<type 'tuple'>
>>> t = ("a","b")
>>> type (t)
<type 'tuple'>
>>> t = "a",
>>> type(t)
<type 'tuple'>

The end!

To be continue

wklken

Email: wklken@yeah.net

Blog: http://blog.csdn.net/wklken

2013-03-09

转载请注明出处,谢谢