python 中的赋值语句在更改函数调用的位置后给出不同的结果
Assignment statement in python giving different results after changing the position of function calls
为什么
is_correct = list(llist) == list(reverse(flipped))
is_correct 输出 False 但
is_correct = list(reverse(flipped)) == list(llist)
is_correct 输出 True
我正在编写代码来反转链表所以
- reverse() 的功能是反转链表
- flipped 和 llist 是如下所示的 LinkedList 对象。
完整代码是
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def __init__(self):
self.head = None
def append(self, value):
if self.head is None:
self.head = Node(value)
return
node = self.head
while node.next:
node = node.next
node.next = Node(value)
def __iter__(self):
node = self.head
while node:
yield node.value
node = node.next
def __repr__(self):
return str([v for v in self])
def reverse(linked_list):
head = linked_list.head
nh = head.next
head.next = None
while nh:
temp = nh.next
nh.next = head
head = nh
nh = temp
linked_list.head = head
return linked_list
llist = LinkedList()
li = [4,2,5,1,-3,0]
for value in li:
llist.append(value)
flipped = reverse(llist)
is_correct = list(llist) == list(reverse(flipped))
print("Pass" if is_correct else "Fail")
llist = LinkedList()
li = [4,2,5,1,-3,0]
for value in li:
llist.append(value)
flipped = reverse(llist)
is_correct = list(reverse(flipped)) == list(llist)
print("Pass" if is_correct else "Fail")
提前致谢!!!
鉴于reverse
是一个就地操作,你的代码等同于
flipped = reverse(llist) # two names for the same reversed list
t1 = list(llist) # a copy of the reversed list
t2 = list(reverse(flipped)) # a copy of the re-reversed list, identical to the original
is_correct = t1 == t2 # False; one is in the original order, the other reverse order
flipped = reverse(llist) # two names for the same reversed list
t1 = list(reverse(flipped)) # a copy of the re-reversed list
t2 = list(llist) # another copy of the re-reversed list
is_correct = t1 == t2 # True; both lists are in the original non-reversed order
==
的操作数是从左到右求值的,因此 reverse
是第一次调用还是第二次调用会影响创建的 list
个对象。
为什么
is_correct = list(llist) == list(reverse(flipped))
is_correct 输出 False 但
is_correct = list(reverse(flipped)) == list(llist)
is_correct 输出 True
我正在编写代码来反转链表所以
- reverse() 的功能是反转链表
- flipped 和 llist 是如下所示的 LinkedList 对象。
完整代码是
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def __init__(self):
self.head = None
def append(self, value):
if self.head is None:
self.head = Node(value)
return
node = self.head
while node.next:
node = node.next
node.next = Node(value)
def __iter__(self):
node = self.head
while node:
yield node.value
node = node.next
def __repr__(self):
return str([v for v in self])
def reverse(linked_list):
head = linked_list.head
nh = head.next
head.next = None
while nh:
temp = nh.next
nh.next = head
head = nh
nh = temp
linked_list.head = head
return linked_list
llist = LinkedList()
li = [4,2,5,1,-3,0]
for value in li:
llist.append(value)
flipped = reverse(llist)
is_correct = list(llist) == list(reverse(flipped))
print("Pass" if is_correct else "Fail")
llist = LinkedList()
li = [4,2,5,1,-3,0]
for value in li:
llist.append(value)
flipped = reverse(llist)
is_correct = list(reverse(flipped)) == list(llist)
print("Pass" if is_correct else "Fail")
提前致谢!!!
鉴于reverse
是一个就地操作,你的代码等同于
flipped = reverse(llist) # two names for the same reversed list
t1 = list(llist) # a copy of the reversed list
t2 = list(reverse(flipped)) # a copy of the re-reversed list, identical to the original
is_correct = t1 == t2 # False; one is in the original order, the other reverse order
flipped = reverse(llist) # two names for the same reversed list
t1 = list(reverse(flipped)) # a copy of the re-reversed list
t2 = list(llist) # another copy of the re-reversed list
is_correct = t1 == t2 # True; both lists are in the original non-reversed order
==
的操作数是从左到右求值的,因此 reverse
是第一次调用还是第二次调用会影响创建的 list
个对象。