>>> 1, 2, 3 #这样写成一行相当于一个元组
(1, 2, 3)
>>> x = 1, 2, 3
>>> x
(1, 2, 3)
>>> type(x)
<class 'tuple'>
>>> x, y, z = 4, 6, 5
>>> x, y, z
(4, 6, 5)
>>> tx = x, y, z
>>> tx
(4, 6, 5)
>>> a, b, c = tx
>>> a
4
>>> b
6
>>> c
5
>>> (1, 3, 5)
(1, 3, 5)
>>>

>>> li = [('张三', 50), ('李四', 30)]
>>> for name,_ in li:
... print(name)
... print(_)
...
张三
50
李四
30

>>> for inf in li:
... print('%s__%s' % inf)  #这个功能只有%s 能做到, '{}'.format()做就会出错
...
张三__50
李四__30
>>> for inf in li:
... print('{}, {}'.format(inf))
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IndexError: tuple index out of range
>>> for inf in li:
... print('{}, {}'.format inf)
File "<stdin>", line 2
print('{}, {}'.format inf)
^
SyntaxError: invalid syntax

>>> for inf in li:
... print('{}, {}'.format(*inf)) # 如果用*拆包, 就可以了
...
张三, 50
李四, 30

'%s'无法拆字符串的包,  但是'{}'.format()却可以:

>>> st = 'abcd'
>>> *st
File "<stdin>", line 1
SyntaxError: can't use starred expression here
>>> s, *a = st
>>> a
['b', 'c', 'd']
>>> s
'a'
>>> print('%s %s %s %s' % st)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

>>> print('%s %s %s %s' % (st))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

>>> print('%s %s %s %s' %(*st))
File "<stdin>", line 1
SyntaxError: can't use starred expression here

>>> print('{}'.format(*st))
a
>>> print('{2}'.format(*st))
c
>>>

>>> l = (4, 5)
>>> def jia(x, y):
... return x+y
...
>>> jia(*l)
9
>>>

Python 序列类型拆包 %s 和'{}'.format 的功能差异之一的更多相关文章

  1. Python序列类型

    Python序列类型 序列:字符.列表.元组 所有序列都支持迭代 序列表示索引为非负整数的有序对象集合 字符和元组属于不可变序列,列表可变 1)字符 字符串字面量:把文本放入单引号.双引号或三引号中: ...

  2. python 序列类型

    1.不可变的序列类型:tuple.range.str.set 001:对于tuple 类型有如下几种构造方式 1.() 构造一个空的元组. 2.a | (a,) 构造一个只有一个元素的元组. 3.tu ...

  3. Python序列类型各自方法

    在Python输入dir(str).dir(list).dir(tuple)可查看各种序列类型的所有方法. 对于某个方法不懂怎么使用的情况,可以直接help(str.split)对某个方法进行查询. ...

  4. Python序列类型方法

    列表的常用方法 append.insert.extend.pop.remove 元组的两个方法count.index 字符串的常用方法及转义count.find.index.replace.split ...

  5. Python 序列类型小结

    序列是python中最基本的数据结构. 序列中每一个元素都有其对应的索引,索引是从0开始,0,1,2......依次类推 python中的序列类型有:字符串str.列表list.元组tuple.Uni ...

  6. python序列类型及一些操作

    序列分类 1.按存放的数据类型分类: 容器类型(能存放不同类型的数据):list.tuple.coolections.deque 扁平序列(只能存放一种类型的数据):str.bytes.bytearr ...

  7. python序列类型字符串的方法L.index()与L.find()区别

    首先官方解释 S.index(sub[, start[, end]]) -> int Like S.find() but raise ValueError when the substring ...

  8. python高级(二)—— python内置序列类型

    本文主要内容 序列类型分类: (1)容器序列.扁平序列 (2)可变序列.不可变序列 列表推导式 生成器表达式 元组拆包 切片 排序(list.sort方法和sorted函数) bisect pytho ...

  9. Python 基本数据类型和序列类型

    python 3.6.4 中,有9种数据类型: int, float, bool, complex, list, tuple, string, set, dict (1).int 整型,不可变 (2) ...

随机推荐

  1. Facebook被指控通过其应用程序进行监视用户照片

    Facebook被批使用其应用程序收集有关用户及其朋友的信息,其中包括一些尚未注册社交网络,阅读短信,跟踪其位置并在手机上查看照片的人. 有关大众监督的声称是前创业公司Six4Three对该公司提起的 ...

  2. tensorflow的boolean_mask函数

    在mask中定义true,保留与其进行运算的tensor里的部分内容,相当于投影的功能. mask与tensor的维度可以不相同的,但是对应的长度一定要相同,也就是要有一一对应的部分: 结果的维度 = ...

  3. centos 6.5 配置 DNS

    编辑 vi /etc/resolv.conf 修改 DNS nameserver 202.96.134.133 nameserver 202.96.128.86 nameserver 8.8.8.8 ...

  4. webpack 图片文件处理loader

    目录结构: 引用图片: body { /*background: red;*/ /*background: url("../img/test2.jpg"); 小图片*/ backg ...

  5. Tomcat 中get请求中含有中文字符时乱码的处理

    Tomcat 中get请求中含有中文字符时乱码的处理

  6. 【HDU6621】K-th Closest Distance【线段树】

    题目大意:给你一堆数,每次询问区间[l,r]中离p第k小的|ai-p| 题解:考虑二分答案,对于每个可能的答案,我们只需要看在区间[l,r]里是否有≥k个比二分的答案还要接近于p的 考虑下标线段树,并 ...

  7. 【HDU6609】Find the answer【线段树】

    题目大意:给你一个序列,对于每个i,你可以选择1~i-1中任意多的数并将它删去,剩余的数(包括i)∑≤m,问对于每个i最少删几个数可以达到要求 题解: 考虑朴素的思想,对于每个i,我只需要删去最大的若 ...

  8. [HDU2276]Kiki & Little Kiki 2

    题目:Kiki & Little Kiki 2 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2276 分析: 1)如果前一盏灯亮着,则改变这一盏灯 ...

  9. php 封装原生数据导入的方法(csv文件格式)

    //前端---部分代码 <form class="form-inline" style="margin-top: 20px" method="p ...

  10. handler消息机制入门

    handler消息机制入门 为什么要用handle? 我们在网络上读取图片信息时,是不能把耗时操作放在主线程里面的,当我们在子线程中获取到了图片的消息的时候,我们就需要把这个数据传给主线程. 而直接使 ...