可以用list为tuple赋值?搞不懂
>>> l =>>> (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)
谢谢 实践出真理,可以 wuqramy 发表于 2020-4-26 14:40
实践出真理,可以
谢谢您的关注和回复 这就是规定啊,多变量赋值就是如此,左边加不加括号都可以,不加括号就是多个独立变了,加上括号,就是一个元组
多变量赋值,右侧可以是多个变量,可以是一个元组,也可以是一个列表
list1=
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) sunrise085 发表于 2020-4-26 15:16
这就是规定啊,多变量赋值就是如此,左边加不加括号都可以,不加括号就是多个独立变了,加上括号,就是一个 ...
谢谢您的耐心回复。 本帖最后由 成亟亟 于 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 ::="[" "]"
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
| "(" ")"
| "[" "]"
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.
... ...
学习了,谢谢 本帖最后由 成亟亟 于 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/tutorial/datastructures.html#tuples-and-sequences
页:
[1]