鱼C论坛

 找回密码
 立即注册
查看: 2466|回复: 7

[已解决]可以用list为tuple赋值?搞不懂

[复制链接]
发表于 2020-4-26 14:39:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
>>> l = [4,5,6]
>>> (mydic['id'],mydic['name'],mydic['sex']) = l            #左侧是tuple,右侧是list,居然可以直接这么划等号是为什么?
>>> type((mydic['id'],mydic['name'],mydic['sex']))
<class 'tuple'>
>>> (mydic['id'],mydic['name'],mydic['sex'])
(4, 5, 6)

谢谢
最佳答案
2020-4-26 15:16:00
这就是规定啊,多变量赋值就是如此,左边加不加括号都可以,不加括号就是多个独立变了,加上括号,就是一个元组
多变量赋值,右侧可以是多个变量,可以是一个元组,也可以是一个列表
list1=[1,2,32]
b,c,d=list1
print(b,c,d)
b,c,d=3,6,1
print(b,c,d)
(b,c,d)=(5,8,13)
print(b,c,d)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-26 14:40:36 | 显示全部楼层
实践出真理,可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-26 14:41:49 | 显示全部楼层
wuqramy 发表于 2020-4-26 14:40
实践出真理,可以

谢谢您的关注和回复
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-26 15:16:00 | 显示全部楼层    本楼为最佳答案   
这就是规定啊,多变量赋值就是如此,左边加不加括号都可以,不加括号就是多个独立变了,加上括号,就是一个元组
多变量赋值,右侧可以是多个变量,可以是一个元组,也可以是一个列表
list1=[1,2,32]
b,c,d=list1
print(b,c,d)
b,c,d=3,6,1
print(b,c,d)
(b,c,d)=(5,8,13)
print(b,c,d)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-26 16:10:10 | 显示全部楼层
sunrise085 发表于 2020-4-26 15:16
这就是规定啊,多变量赋值就是如此,左边加不加括号都可以,不加括号就是多个独立变了,加上括号,就是一个 ...

谢谢您的耐心回复。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-28 14:59:26 | 显示全部楼层
本帖最后由 成亟亟 于 2020-4-29 08:50 编辑

A list display is a possibly empty series of expressions enclosed in square brackets:(6.2.5. List display)
list_display ::=  "[" [starred_list | comprehension] "]"

Except when part of a list or set display, an expression list containing at least one comma yields a tuple: (6.15 Expression lists)
expression_list    ::=  expression ("," expression)* [","]
An asterisk * denotes iterable unpacking. Its operand must be an iterable. The iterable is expanded into a sequence of items, which are included in the new tuple, list, or set, at the site of the unpacking.
The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases. A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: ().)

Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects: (7.2 Assignment statements)
assignment_stmt ::=  (target_list "=")+ (starred_expression | yield_expression)
target_list           ::=  target ("," target)* [","]
target                 ::=  identifier
                              | "(" [target_list] ")"
                              | "[" [target_list] "]"
An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.
Assignment is defined recursively depending on the form of the target (list). When a target is part of a mutable object, the mutable object must ultimately perform the assignment and decide about its validity, and may raise an exception if the assignment is unacceptable.
Assignment of an object to a target list, optionally enclosed in parentheses or square brackets, is recursively defined as follows.
        If the target list is a single target with no trailing comma, optionally in parentheses, the object is assigned to that target.
        ! Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.
                            ... ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-28 23:58:53 From FishC Mobile | 显示全部楼层
学习了,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-29 09:33:52 | 显示全部楼层
本帖最后由 成亟亟 于 2020-5-7 09:21 编辑

The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple.
The reverse operation is also possible:

>>> x, y, z = t

This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side.
Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence.
Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.

參考:https://docs.python.org/3.8/tuto ... uples-and-sequences
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-11 19:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表