相关疑难解决方法(0)

使用+和+ =在python中添加列表之间的区别

我注意到在尝试p= p+i不同的列表时, p += i 例如:

test = [0, 1, 2, 3,]
p = test
test1 = [8]
p = p + test1
print test
Run Code Online (Sandbox Code Playgroud)

在上面的代码中test打印出原始值[0, 1, 2, 3,]

但是如果我在下面p = p + test1p += test1As 切换

test = [0, 1, 2, 3,]
p = test
test1 = [8]

p += test1

print test
Run Code Online (Sandbox Code Playgroud)

test 现在等于 [0, 1, 2, 3, 8]

价值不同的原因是什么?

python list

9
推荐指数
1
解决办法
187
查看次数

为什么'new_file + = line + string'比'new_file = new_file + line + string'快得多?

当我们使用时,我们的代码需要10分钟来虹吸68,000条记录:

new_file = new_file + line + string
Run Code Online (Sandbox Code Playgroud)

但是,当我们执行以下操作时,只需1秒钟:

new_file += line + string
Run Code Online (Sandbox Code Playgroud)

这是代码:

for line in content:
import time
import cmdbre

fname = "STAGE050.csv"
regions = cmdbre.regions
start_time = time.time()
with open(fname) as f:
        content = f.readlines()
        new_file_content = ""
        new_file = open("CMDB_STAGE060.csv", "w")
        row_region = ""
        i = 0
        for line in content:
                if (i==0):
                        new_file_content = line.strip() + "~region" + "\n"
                else:
                        country = line.split("~")[13]
                        try:
                                row_region = regions[country]
                        except KeyError:
                                row_region = "Undetermined"
                        new_file_content += …
Run Code Online (Sandbox Code Playgroud)

python string cpython string-concatenation python-internals

8
推荐指数
2
解决办法
1108
查看次数

列表是按值传递还是按引用传递?

考虑下面的代码,乍一看它做了同样的事情,但结果不同,有时列表似乎是按值传递,有时列表似乎是按引用传递:

lst = [1, 2]
def f(lst):
#     lst = lst + [3]  # seems pass by value
#     lst += [3]  # strange! same as above but seems pass by reference
    lst = lst.append(3)  # seems pass by reference
    return lst
f(lst)  
print(lst)
Run Code Online (Sandbox Code Playgroud)

谁能告诉我发生了什么事吗?

python list

5
推荐指数
1
解决办法
1万
查看次数

+和+ =运算符不同?

>>> c = [1, 2, 3]
>>> print(c, id(c))
[1, 2, 3] 43955984
>>> c += c
>>> print(c, id(c))
[1, 2, 3, 1, 2, 3] 43955984
>>> del c
>>> c = [1, 2, 3]
>>> print(c, id(c))
[1, 2, 3] 44023976
>>> c = c + c
>>> print(c, id(c))
[1, 2, 3, 1, 2, 3] 26564048
Run Code Online (Sandbox Code Playgroud)

有什么不同?是+ =和+不应该仅仅是语法糖?

python list operators

4
推荐指数
1
解决办法
457
查看次数

类变量和实例变量之间的区别

我已经在Stack Exchange上阅读了很多像Python一样的答案- 为什么在课堂上使用"self"? 阅读完这些内容之后,我了解到实例变量对于类的每个实例都是唯一的,而类变量是在所有实例之间共享的.在玩的时候我发现这个代码 -

class A:
    x = []
    def add(self):
        self.x.append(1)

x = A()
y = A()
x.add()
print "Y's x:",y.x
Run Code Online (Sandbox Code Playgroud)

确实给出了输出[1].但是,这段代码 -

class A:
    x = 10
    def add(self):
        self.x += 1

x = A()
y = A()
x.add()

print "Y's x:",y.x
Run Code Online (Sandbox Code Playgroud)

给出输出,就像10我认为的那样11.如果这是一个非常无聊的问题,请原谅我,但我在编程方面不是很有经验.

python oop class

4
推荐指数
1
解决办法
2454
查看次数

为什么'x + = y'并不总是与Python(2.7)中的'x = x + y'相同?

我总是这么认为

x += y
Run Code Online (Sandbox Code Playgroud)

只是一个捷径

x = x + y
Run Code Online (Sandbox Code Playgroud)

但似乎不是列表的情况:

x = []

x2 = x

print x is x2
True

x += [2]

print x is x2
True

x = x + [2]

print x is x2
False
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
1
解决办法
335
查看次数

Python Pandas 插入 DF 不起作用

我有一个如下所示的数据框:

df1

        A        Q
0   Apple    chair
1  orange     desk
2    pear  monitor
3   salad     room

df2 = df1[['Q']]

print(df2)

           Q
0      chair
1       desk
2    monitor
3       room


df2 = df2.insert(1, 'file_id_value', range(0, len(df2)))

print(df2)
Run Code Online (Sandbox Code Playgroud)

不返回任何内容,特别是单词'None'。你知道我做错了什么吗?我没有收到错误消息。

python pandas

1
推荐指数
1
解决办法
1356
查看次数