任何修补Python足够长的人都被以下问题咬伤(或撕成碎片):
def foo(a=[]):
a.append(5)
return a
Run Code Online (Sandbox Code Playgroud)
Python新手希望这个函数总能返回一个只包含一个元素的列表:[5].结果却非常不同,而且非常惊人(对于新手来说):
>>> foo()
[5]
>>> foo()
[5, 5]
>>> foo()
[5, 5, 5]
>>> foo()
[5, 5, 5, 5]
>>> foo()
Run Code Online (Sandbox Code Playgroud)
我的一位经理曾经第一次遇到这个功能,并称其为该语言的"戏剧性设计缺陷".我回答说这个行为有一个潜在的解释,如果你不理解内部,那确实非常令人费解和意想不到.但是,我无法回答(对自己)以下问题:在函数定义中绑定默认参数的原因是什么,而不是在函数执行时?我怀疑经验丰富的行为有实际用途(谁真的在C中使用静态变量,没有繁殖错误?)
编辑:
巴泽克提出了一个有趣的例子.再加上你的大部分评论和特别是Utaal,我进一步阐述了:
>>> def a():
... print("a executed")
... return []
...
>>>
>>> def b(x=a()):
... x.append(5)
... print(x)
...
a executed
>>> b()
[5]
>>> b()
[5, 5]
Run Code Online (Sandbox Code Playgroud)
对我而言,似乎设计决策是相对于放置参数范围的位置:在函数内部还是"与它一起"?
在函数内部进行绑定意味着在调用函数时x有效地绑定到指定的默认值,而不是定义,这会产生一个深层次的缺陷:def在某种意义上,该行将是"混合"的(部分绑定)函数对象)将在定义时发生,并在函数调用时发生部分(默认参数的赋值).
实际行为更加一致:执行该行时,该行的所有内容都会得到评估,这意味着在函数定义中.
python language-design least-astonishment default-parameters
我有一个Python脚本,它将整数列表作为输入,我需要一次使用四个整数.不幸的是,我无法控制输入,或者我将它作为四元素元组列表传入.目前,我正在以这种方式迭代它:
for i in xrange(0, len(ints), 4):
# dummy op for example code
foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]
Run Code Online (Sandbox Code Playgroud)
它看起来很像"C-think",这让我怀疑有更多的pythonic方式来处理这种情况.迭代后会丢弃该列表,因此无需保留.也许这样的事情会更好吗?
while ints:
foo += ints[0] * ints[1] + ints[2] * ints[3]
ints[0:4] = []
Run Code Online (Sandbox Code Playgroud)
但是,仍然没有"感觉"正确.: - /
我有一个非常大的文件4GB,当我尝试阅读它时,我的电脑挂起.所以我想逐一阅读它并在处理完每件之后将处理过的零件存储到另一个文件中并阅读下一篇文章.
yield这些作品有什么方法吗?
我很想拥有一种懒惰的方法.
我这样做是为了测试randint的随机性:
>>> from random import randint
>>>
>>> uniques = []
>>> for i in range(4500): # You can see I was optimistic.
... x = randint(500, 5000)
... if x in uniques:
... raise Exception('We duped %d at iteration number %d' % (x, i))
... uniques.append(x)
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
Exception: We duped 887 at iteration number 7
Run Code Online (Sandbox Code Playgroud)
我尝试了大约10倍以上,我得到的最好结果是在转发器之前迭代了121次.这是您从标准库中获得的最佳结果吗?
我需要一次读取最多N行读取一个大文件,直到EOF.在Python中最有效的方法是什么?就像是:
with open(filename, 'r') as infile:
while not EOF:
lines = [get next N lines]
process(lines)
Run Code Online (Sandbox Code Playgroud) 我有一个大文本文件(约7 GB).我正在寻找是否存在阅读大文本文件的最快方法.我一直在阅读有关使用多种方法作为读取chunk-by-chunk以加快进程的过程.
例如,effbot建议
# File: readline-example-3.py
file = open("sample.txt")
while 1:
lines = file.readlines(100000)
if not lines:
break
for line in lines:
pass # do something**strong text**
Run Code Online (Sandbox Code Playgroud)
为了每秒处理96,900行文本.其他作者建议使用islice()
from itertools import islice
with open(...) as f:
while True:
next_n_lines = list(islice(f, n))
if not next_n_lines:
break
# process next_n_lines
Run Code Online (Sandbox Code Playgroud)
list(islice(f, n))将返回n文件的下一行列表f.在循环中使用它将为您提供大量n行的文件
我有一个包含 7946479 条记录的文件,我想逐行读取该文件并插入到数据库(sqlite)中。我的第一种方法是打开文件逐行读取记录并同时插入数据库,因为它处理大量数据,需要很长时间。我想改变这种幼稚的方法,所以当我在互联网上搜索时我看到了这个 [python-csv-to-sqlite][1] ,他们在 csv 文件中有数据,但我的文件是dat格式,但我喜欢这个问题的答案,所以现在我尝试这样做在解决方案中。
/sf/ask/415968171/
他们使用的方法就像首先将整个文件分成块,然后执行数据库事务,而不是一次写入每条记录。
所以我开始编写一个代码来将我的文件分割成块这是我的代码,
file = r'files/jan.dat'
test_file = r'random_test.txt'
def chunks(file_obj, size=10000):
counter = 0
file_chunks = []
temp_chunks = []
for line in file_obj:
if line == '\n':
continue
if counter != size:
temp_chunks.append(line)
counter += 1
else:
file_chunks.append(temp_chunks)
temp_chunks = []
counter = 0
file_obj.close()
if len(temp_chunks) != 0:
file_chunks.append(temp_chunks)
yield file_chunks
if __name__ == '__main__':
split_files = chunks(open(test_file))
for chunk in split_files:
print(len(chunk))
Run Code Online (Sandbox Code Playgroud)
输出是 795,但我想要的是将整个文件分割成大小为 10000 的块
我不知道这里出了什么问题,我无法在这里共享我的整个文件,因此为了测试可以使用此代码生成一个包含 …