我正在迭代Python中的元组列表,并且如果它们符合某些条件,我会尝试删除它们.
for tup in somelist:
if determine(tup):
code_to_remove_tup
Run Code Online (Sandbox Code Playgroud)
我应该用什么代替code_to_remove_tup?我无法弄清楚如何以这种方式删除项目.
下面是我遇到问题的python代码:
for i in range (0,10):
if i==5:
i+=3
print i
Run Code Online (Sandbox Code Playgroud)
我期望输出为:
0
1
2
3
4
8
9
Run Code Online (Sandbox Code Playgroud)
然而翻译吐出:
0
1
2
3
4
8
6
7
8
9
Run Code Online (Sandbox Code Playgroud)
我知道for循环为C中的变量创建了一个新的作用域,但不知道python.任何人都可以解释为什么python i中的for循环中没有变化的值,并且为了获得预期的输出有什么补救措施.
我正在尝试使用for循环修改列表中的项目,但是我收到错误(参见下文).示例代码:
#!/usr/bin/env python
# *-* coding: utf8 *-*
data = []
data.append("some")
data.append("example")
data.append("data")
data.append("here")
for item in data:
data[item] = "everything"
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "./testy.py", line 11, in <module>
data[item] = "everything"
TypeError: list indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)
有什么方法可以解决这个问题吗?
在Python中循环遍历列表时,我无法在没有列表推导的情况下修改元素.以供参考:
li = ["spam", "eggs"]
for i in li:
i = "foo"
li
["spam", "eggs"]
li = ["foo" for i in li]
li
["foo", "foo"]
Run Code Online (Sandbox Code Playgroud)
那么,为什么我不能通过Python中的循环修改元素呢?肯定有一些我想念的东西,但我不知道是什么.我敢肯定,这是一个重复的,但我无法找到一个关于这个问题,如果有一个链接,这将是绰绰有余.先感谢您!
我有一种预感,我需要访问列表中的项目(字符串),修改该项目(作为字符串),并将其放回同一索引的列表中
我很难将一个项目放回到相同的索引中
for item in list:
if "foo" in item:
item = replace_all(item, replaceDictionary)
list[item] = item
print item
Run Code Online (Sandbox Code Playgroud)
现在我收到一个错误
TypeError: list indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)
由于这条线 list[item] = item
这很有意义!但我不知道如何使用python将项目放回到同一索引的列表中
这是什么语法?理想情况下,for循环可以跟踪我当前所处的索引
>>arr = [4, 2, 1, 3]
>>arr[0], arr[arr[0]-1] = arr[arr[0]-1], arr[0]
>>arr
Run Code Online (Sandbox Code Playgroud)
我期待的结果
>>[3, 2, 1, 4]
结果我得到
>>[3, 2, 4, 3]
基本上我试图交换 #4 和 #3(在我的实际问题中,索引不会是 0,而是一个迭代器 "i" 。所以我不能只做 arr[0], arr[3] = arr[ 3], arr[0]) 我认为我对同时分配的理解相当好。显然我错了。我不明白为什么赋值左侧的 arr[arr[0]-1] 计算的是 arr[2] 而不是 arr[3]。如果分配同时发生(从右侧评估),
arr[0](在左侧第二个元素的索引内)仍应为“4”
arr[0] -1(左侧第二个元素的索引)因此应为“3”
这个问题不是在迭代过程中Dictionary更改大小的重复-代码在Py2中有效,而在Py3中无效。这个问题问为什么不对此强制执行list,而不是如何修复代码。
Python将愉快地执行
li = [1, 2, 3]
for n in li:
if n % 2 == 0:
li.remove(n)
Run Code Online (Sandbox Code Playgroud)
但是,如果我们尝试使用字典:
a = {1: '', 2: '', 3: ''}
for n in a:
if n % 2 == 0:
a.pop(n)
Run Code Online (Sandbox Code Playgroud)
我们得到
RuntimeError: dictionary changed size during iteration
Run Code Online (Sandbox Code Playgroud)
因为我假设这是实现细节,所以我会注意到这已经在CPython 3.7.0和3.4.2中进行了测试。
我是Python新手并尝试使用Python 3.2.3(默认,2012年10月19日,20:13:42),linux2上的[GCC 4.6.3]
这是我的样本代码
>>> l=[1,2,3,4,5,6]
>>> for i in l:
... l.pop(0)
... print(l)
...
Run Code Online (Sandbox Code Playgroud)
我期待以下输出
1
[2, 3, 4, 5, 6]
2
[3, 4, 5, 6]
3
[4, 5, 6]
4
[5, 6]
5
[6]
6
[]
Run Code Online (Sandbox Code Playgroud)
相反,我得到了这个
1
[2, 3, 4, 5, 6]
2
[3, 4, 5, 6]
3
[4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
for循环在3转后停止迭代.有人可以解释一下原因吗?
例如:
def update_condition(self, type, params):
for condition in self.conditions:
condition_loaded = json.loads(condition)
if condition_loaded['type'] == type:
condition_loaded['params'] = params
condition = json.dumps(condition_loaded)
Run Code Online (Sandbox Code Playgroud)
上面的代码什么都不做,因为条件不是通过引用.这样做的正确方法是什么?
我想创建一个函数,在数组中的每个字符串前面添加一个特定的单词.最后我想改变数组.我有这个代码:
def make_great(magicians):
"""Change magicians"""
for magician in magicians:
magician = "the Great" + magician
magicians = ["hudini", "angel", "teller", "anderson", "copperfield"]
make_great(magicians)
print(magicians)
Run Code Online (Sandbox Code Playgroud)
此代码不会更改数组.如何使我的功能工作?