+=python中的运算符似乎在列表上意外运行.谁能告诉我这里发生了什么?
class foo:
bar = []
def __init__(self,x):
self.bar += [x]
class foo2:
bar = []
def __init__(self,x):
self.bar = self.bar + [x]
f = foo(1)
g = foo(2)
print f.bar
print g.bar
f.bar += [3]
print f.bar
print g.bar
f.bar = f.bar + [4]
print f.bar
print g.bar
f = foo2(1)
g = foo2(2)
print f.bar
print g.bar
Run Code Online (Sandbox Code Playgroud)
OUTPUT
[1, 2]
[1, 2]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3]
[1]
[2] …Run Code Online (Sandbox Code Playgroud) 如果我们采取b = [1,2,3]并尝试这样做:b+=(4,)
它返回b = [1,2,3,4],但是如果我们尝试这样做b = b + (4,)是行不通的。
b = [1,2,3]
b+=(4,) # Prints out b = [1,2,3,4]
b = b + (4,) # Gives an error saying you can't add tuples and lists
Run Code Online (Sandbox Code Playgroud)
我预计b+=(4,)会失败,因为您无法添加列表和元组,但它确实有效。因此,我尝试b = b + (4,)期望得到相同的结果,但是没有用。
可能重复:
Python中的加号等于(+ =)做什么?
今天我发现了python语言的一个有趣的"特性",这给了我很多悲伤.
>>> a = [1, 2, 3]
>>> b = "lol"
>>> a = a + b
TypeError: can only concatenate list (not "str") to list
>>> a += b
>>> a
[1, 2, 3, 'l', 'o', 'l']
Run Code Online (Sandbox Code Playgroud)
那个怎么样?我以为这两个本来是相同的!更糟糕的是,这是我有一段时间调试的代码
>>> a = [1, 2, 3]
>>> b = {'omg': 'noob', 'wtf' : 'bbq'}
>>> a = a + b
TypeError: can only concatenate list (not "dict") to list
>>> a += b
>>> a
[1, 2, 3, …Run Code Online (Sandbox Code Playgroud) 当我试图在 stackoverflow 上回答另一个人关于Python=和+=Python之间的区别的问题时,我遇到了以下问题:
class Foo:
def __init__(self, value, name="default"):
self.value = value
self.name = name
def __add__(self, that):
return Foo(self.value + that.value)
def __iadd__(self, that):
self.value = self.value + that.value
return self
def __str__(self):
return "name: {}, value: {:d}".format(self.name, self.value)
a = Foo(1, 'alice')
b = Foo(2, 'bob')
print(a+=b)
Run Code Online (Sandbox Code Playgroud)
最后一个print电话没有成功,给了我这个:
File "<ipython-input-8-0faa82ba9e4a>", line 3
print(a+=b)
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我不知道为什么这不起作用。也许它与关键字参数传递机制有关?我只是找不到关于这个主题的任何资源,因为重载的__iadd__方法已经返回了一个Foo对象。
************** 更新 ******************
如果我__iadd__像这样更改方法(只需删除return语句):
... …Run Code Online (Sandbox Code Playgroud) 我试图将我的解决方案提交给 leetcode 问题,其中x和y是列表和使用
x = x + y
Run Code Online (Sandbox Code Playgroud)
给我一个时间超过限制 而使用
x += y
Run Code Online (Sandbox Code Playgroud)
通过了测试用例并给了我AC。
两者之间的执行时间差异以及两者执行方式的差异是多少?
我一直在研究这个挑战:Count Triplets,经过大量的努力,我的算法并没有适用于每个测试用例。
由于在讨论中,我看到了一段代码并试图找出代码的真正功能,但我仍然无法理解这段代码是如何工作的。
解决方案:
from collections import defaultdict
arr = [1,3,9,9,27,81]
r = 3
v2 = defaultdict(int)
v3 = defaultdict(int)
count = 0
for k in arr:
count += v3[k]
v3[k*r] += v2[k]
v2[k*r] += 1
print(count)
Run Code Online (Sandbox Code Playgroud)
上面的代码完美地适用于每个测试用例。我已经测试的值k,v2,v3理解,但还是不知道如何代码工作,以便与计数三胞胎顺利。我也无法在梦中想到那个解决方案。我想知道人们如何如此聪明地制定出这个解决方案。尽管如此,如果我能得到正确的解释,我会很高兴。谢谢
k,v2,v3 的输出
from collections import defaultdict
arr = [1,3,9,9,27,81]
r = 3
v2 = defaultdict(int)
v3 = defaultdict(int)
count = 0
for k in arr:
count += v3[k]
v3[k*r] += v2[k]
v2[k*r] += 1 …Run Code Online (Sandbox Code Playgroud) 我是Python的新手.如何将带有整数值的变量一起添加?
balance = 1000
deposit = 50
balance + deposit
print "Balance: " + str(balance)
Run Code Online (Sandbox Code Playgroud)
我想要平衡和存款一起添加到它1050,但我只是得到1000.我知道我显然没有正确格式化它(余额+存款),但我无法弄清楚正确的格式化方式.
谢谢.
为什么它给出不同的输出:
+ 和 += 之间有区别吗
def func1(lst1, lst2):
lst1 += lst2
lst1 = [1, 2, 3]
lst2 = ["a", "b"]
func1(lst1, lst2)
print(lst1)
Run Code Online (Sandbox Code Playgroud)
和
def func1(lst1, lst2):
lst1 = lst1 + lst2
lst1 = [1, 2, 3]
lst2 = ["a", "b"]
func1(lst1, lst2)
print(lst1)
Run Code Online (Sandbox Code Playgroud)
提前致谢