python使用逗号交换值会引起混淆 [英] python swaping values using comma is causing confusions

查看:37
本文介绍了python使用逗号交换值会引起混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这涉及到我在尝试解决链表反向问题时遇到的一个问题.

this involves a problem that I encountered when try to solve a linked-list reverse problem.

先放一些链表定义的初步代码和生成链表的快捷方法:

First let me put some preliminary codes for the definition of the linked list and quick method to generate a linked list:

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

    def __repr__(self):
        if self.next:
            return "{}->{}".format(self.val, repr(self.next))
        else:
            return "{}".format(self.val)

def genNode(*nodes, end=None):
    if len(nodes) == 1 and type(nodes[0]) == list:
        nodes = nodes[0]
    for i in nodes[::-1]:
        n = ListNode(i)
        n.next, end = end, n
    return n if nodes else None

我的问题是我发现交换机制仍然取决于我写的变量的顺序.

The problem I have is that I found the swapping mechanism is still depending on the sequence of the variable that I write.

最初当我们谈论在 python 中交换值时,我们可以这样做:

Originally when we talk about swapping values in python we can do:

a, b = b, a

如果我有,它应该以相同的方式工作

and it should work the same way if I have

b, a = a, b

我正在尝试编写的这种反向链表方法有 3 个变量交换,想法很简单,创建一个虚拟头,并在 dummy 和 dummy.next 之间不断添加节点,以便可以反转.

This reverse linked list method that I am trying to write has 3 variables swapping, the idea is simple, to create a dummy head, and consistently adding nodes between dummy and dummy.next, so that it can be reversed.

def rev(head):
    dummy = ListNode('X')
    while head:
        dummy.next, head.next, head = head, dummy.next, head.next
    return dummy.next

a = genNode(1,2,3,4)
print(rev(a)) # >>> 4->3->2->1

但是如果我稍微改变一下 3 个变量的顺序:

But If I slightly switch the sequence of the 3 variables:

def rev2(head):
    dummy = ListNode('X')
    while head:
        dummy.next, head, head.next, = head, head.next, dummy.next,
    return dummy.next

a = genNode(1,2,3,4)
print(rev2(a))  # >>> AttributeError: 'NoneType' object has no attribute 'next'

所以看起来顺序在这里很重要,如果有两个以上的变量,谁能告诉我python如何评估交换值.

So it does seem like that the sequence matters here, and can anyone let me know how python evaluate swapping values if there is more than 2 variables.

谢谢!

推荐答案

从左到右

查看https://docs.python.org/3/reference/simple_stmts.html#assignment-statements

CPython 实现细节:在当前实现中,targets 的语法与表达式的语法相同,在代码生成阶段拒绝无效语法,导致不太详细的错误信息.

CPython implementation detail: In the current implementation, the syntax for targets is taken to be the same as for expressions, and invalid syntax is rejected during the code generation phase, causing less detailed error messages.

尽管赋值的定义暗示左右手之间的重叠是同时"的(例如a, b = b, a 交换两个变量),但在assigned-to 的集合内重叠变量从左到右出现,有时会导致混淆.例如,以下程序打印 [0, 2]:

Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ‘simultaneous’ (for example a, b = b, a swaps two variables), overlaps within the collection of assigned-to variables occur left-to-right, sometimes resulting in confusion. For instance, the following program prints [0, 2]:

x = [0, 1]
i = 0
i, x[i] = 1, 2         # i is updated, then x[i] is updated
print(x)

这篇关于python使用逗号交换值会引起混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆