我有一个关于 python 中列表的内存使用情况的假设问题。我有一个很长的列表my_list,如果加载到内存中,它会消耗多个千兆字节。我想循环该列表并在迭代期间仅使用每个元素一次,这意味着我可以在循环它们后从列表中删除它们。当我循环时,我正在内存中存储其他内容,这意味着我分配的内存my_list现在需要用于其他内容。因此,理想情况下,我想在循环遍历列表元素时删除列表元素并释放内存。
I assume, in most cases, a generator would make the most sense here. I could dump my list to a csv file and then read it line by line in a for-loop. In that case, my_list would never be loaded into memory in the first place. However, let's assume for the sake of discussion I don't want to do that.
Is there a way of releasing the memory of a list as I …
我有一个给定格式的列表:
[['John', 'Smith'], ['Linus', 'Torvalds'], ['Bart', 'Simpson']]
Run Code Online (Sandbox Code Playgroud)
列表中有一些这样的元素['Linus Torvalds',''],我想删除它们.那么为什么以下代码不能删除它们呢?
for i in people:
if(i[0] == '' or i[1] == ''):
print people.pop(people.index(i))
Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的问题.有人看到我的代码有什么问题吗?
for x in questions:
forms.append((SectionForm(request.POST, prefix=str(x.id)),x))
print "Appended " + str(x)
for (form, question) in forms:
print "Testing " + str(question)
if form.is_valid():
forms.remove((form,question))
print "Deleted " + str(question)
a = form.save(commit=False)
a.audit = audit
a.save()
else:
flag_error = True
Run Code Online (Sandbox Code Playgroud)
结果是:
Appended Question 50
Appended Question 51
Appended Question 52
Testing Question 50
Deleted Question 50
Testing Question 52
Deleted Question 52
Run Code Online (Sandbox Code Playgroud)
它似乎跳过问题51.它被附加到列表中,但for循环跳过它.有任何想法吗?
I am trying to write a for loop in python to pop out all the items in a list but two, so I tried this:
guest = ['john', 'phil', 'andy', 'mark', 'frank', 'joe']
for people in guest:
popped_guest = guest.pop()
print("I am sorry " + popped_guest + " I can no longer invite you to dinner")
Run Code Online (Sandbox Code Playgroud)
and this is what I get when I run it:
I am sorry joe I can no longer invite you to dinner
I am …
这是我在尝试学习python编程时遇到的最常见问题.问题是,当我尝试使用"range()"函数迭代列表来检查列表中的给定项是否满足给定条件,如果是,则删除它,它将始终给出"IndexError".那么,有没有一种特殊的方法可以在不使用任何其他中间列表或"while"语句的情况下执行此操作?以下是一个例子:
l = range(20)
for i in range(0,len(l)):
if l[i] == something:
l.pop(i)
Run Code Online (Sandbox Code Playgroud) 在我的 Python 2.7.2 IDLE 解释器中:
>>> mylist = [1, 2, 3, 4, 5]
>>> for item in mylist:
mylist.remove(item)
>>> mylist
[2, 4]
Run Code Online (Sandbox Code Playgroud)
为什么?
我有一个对象“ a”,我想将其转换为列表,这样它没有任何字符,而是数字。虽然,类似的问题谈论列表理解,但我一直在寻找一种可以帮助我收集数字的函数方法。
>>> a
['(', '0', ',', ' ', '1', ',', ' ', '2', ',', ' ', '3', ',', ' ', '4', ',', ' ', '5', ',', ' ', '6', ',', ' ', '7', ',', ' ', '8', ',', ' ', '9', ')']
Run Code Online (Sandbox Code Playgroud)
现在,我正在考虑分两部分解决这个问题。首先,我想删除所有的','和''(空格)。
为此,我编写了代码:
>>> for x in a :
if x == ',' or x == ' ':
del x
Run Code Online (Sandbox Code Playgroud)
这根本不会更改列表。
另外,有什么方法可以遍历每个对象并检查它是否为数字,如果不是,则将其删除。我认为这会简单得多。
此外,如果有解决此问题的更好方法,请提出建议。
在相当长的一段时间里,我一直试图想出一种方法来遍历列表并删除我当前的项目.我似乎无法像我希望的那样工作.它只循环一次,但我希望它循环2次.当我删除删除行 - 它循环2次.
a = [0, 1]
for i in a:
z = a
print z.remove(i)
Run Code Online (Sandbox Code Playgroud)
输出:
[1]
Run Code Online (Sandbox Code Playgroud)
我期待的输出:
[1]
[0]
Run Code Online (Sandbox Code Playgroud) 这就是我做的:
for word in doc:
if len(word) < 3:
doc.remove(word)
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做:
for word in doc:
if len(word) < 3:
print word
Run Code Online (Sandbox Code Playgroud)
返回的结果是:'O',''''30''''' '一世.' '如果'等等.
大多数两个角色项目被删除,但仍有一些仍然存在,我做错了什么?
我有一个元组列表:
from itertools import product
l1 = list((product((0,1), repeat = n)))
Run Code Online (Sandbox Code Playgroud)
对于 n=4,输出如下:
[(0, 0, 0, 0),
(0, 0, 0, 1),
(0, 0, 1, 0),
(0, 0, 1, 1),
(0, 1, 0, 0),
(0, 1, 0, 1),
(0, 1, 1, 0),
(0, 1, 1, 1),
(1, 0, 0, 0),
(1, 0, 0, 1),
(1, 0, 1, 0),
(1, 0, 1, 1),
(1, 1, 0, 0),
(1, 1, 0, 1),
(1, 1, 1, 0),
(1, 1, 1, 1)]
Run Code Online (Sandbox Code Playgroud)
我想删除至少有两个“1”彼此相邻的元组,例如(0,1,1,0). …
python ×10
list ×3
for-loop ×2
django ×1
enumeration ×1
nested-lists ×1
python-3.x ×1
tuples ×1