Python 在没有列表理解、切片或使用 [ ] 的情况下替换列表中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18776420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacing element in list without list comprehension, slicing or using [ ]s
提问by StacyM
I'm taking this online Python courseand they do not like the students using one-line solutions. The course will not accept brackets for this solution.
我正在参加这个在线 Python 课程,他们不喜欢使用单行解决方案的学生。本课程不接受此解决方案的括号。
I already solved the problem using list comprehension, but the course rejected my answer.
我已经使用列表理解解决了这个问题,但是课程拒绝了我的答案。
The problem reads:
问题是:
Using
index
and other list methods, write a functionreplace(list, X, Y)
which replaces all occurrences ofX
inlist
withY
. For example, ifL = [3, 1, 4, 1, 5, 9]
thenreplace(L, 1, 7)
would change the contents ofL
to[3, 7, 4, 7, 5, 9]
. To make this exercise a challenge, you are not allowed to use[]
.Note: you don't need to use return.
使用
index
和其他列表的方法,编写一个函数replace(list, X, Y)
它取代所有出现X
在list
用Y
。例如,如果L = [3, 1, 4, 1, 5, 9]
thenreplace(L, 1, 7)
会将内容更改L
为[3, 7, 4, 7, 5, 9]
。为了使此练习成为一项挑战,您不得使用[]
.注意:您不需要使用 return。
This is what I have so far, but it breaks because of TypeError: 'int' object is not iterable.
到目前为止,这是我所拥有的,但由于 TypeError: 'int' object is not iterable 而中断。
list = [3, 1, 4, 1, 5, 9]
def replace(list, X, Y):
while X in list:
for i,v in range(len(list)):
if v==1:
list.remove(1)
list.insert(i, 7)
replace(list, 1, 7)
This was my original answer, but it was rejected.
这是我最初的答案,但被拒绝了。
list = [3, 1, 4, 1, 5, 9]
def replace(list, X, Y):
print([Y if v == X else v for v in list])
replace(list, 1, 7)
Any ideas on how to fix my longer solution?
关于如何修复我更长的解决方案的任何想法?
采纳答案by Asad Saeeduddin
range()
returns a flat list of integers, so you can't unpack it into two arguments. Use enumerate
to get index and value tuples:
range()
返回整数的平面列表,因此您无法将其解包为两个参数。使用enumerate
获得指数和价值的元组:
def replace(l, X, Y):
for i,v in enumerate(l):
if v == X:
l.pop(i)
l.insert(i, Y)
l = [3, 1, 4, 1, 5, 9]
replace(l, 1, 7)
If you're not allowed to use enumerate
, use a plain old counter:
如果不允许使用enumerate
,请使用普通的旧计数器:
def replace(l, X, Y):
i = 0
for v in l:
if v == X:
l.pop(i)
l.insert(i, Y)
i += 1
l = [3, 1, 4, 1, 5, 9]
replace(list, 1, 7)
Finally, you could use what the authors of the question were probably looking for (even though this is the most inefficient approach, since it linear searches through the list on every iteration):
最后,您可以使用问题的作者可能正在寻找的内容(尽管这是效率最低的方法,因为它在每次迭代时线性搜索列表):
def replace(l, X, Y):
for v in l:
i = l.index(v)
if v == X:
l.pop(i)
l.insert(i, Y)
l = [3, 1, 4, 1, 5, 9]
replace(l, 1, 7)
回答by arshajii
You can also try this (not using []
s or enumerate()
, as required):
你也可以试试这个(不使用[]
s 或enumerate()
,根据需要):
for i in range(len(l)): # loop over indices
if l.__index__(i) == X: # i.e. l[i] == X
l.__setitem__(i, Y) # i.e. l[i] = Y
This probably isn't what the assignment wants you to do, but I'll leave it here for learning purposes.
这可能不是作业想要你做的,但我会把它留在这里以供学习。
Note: You shouldn't use list
as a variable name since that's already used by a built-in function. I've used l
here instead.
注意:您不应将其list
用作变量名,因为它已被内置函数使用。我已经l
在这里使用了。
回答by CT Zhu
If enumerate
is not allowed, you can also use a while
loop.
如果enumerate
不允许,也可以使用while
循环。
>>> def replace(L_in, old_v, new_v):
while old_v in L_in:
idx=L_in.index(old_v)
L_in.pop(idx)
L_in.insert(idx, new_v)
>>> L = [3, 1, 4, 1, 5, 9]
>>> replace(L, 1, 7)
>>> L
[3, 7, 4, 7, 5, 9]
回答by dansalmo
Do not use list
as a name, it will cause you much pain.
不要list
用作名字,它会给你带来很大的痛苦。
def replace(my_list, X, Y):
while X in my_list:
my_list.insert(my_list.index(X), Y)
my_list.pop(my_list.index(X))
回答by aclark
This worked for me. Pretty straight forward. Probably a way of doing it with less lines, but based on what has been taught on the website so far, this works.
这对我有用。很直接。可能是一种用更少的行来完成它的方法,但根据迄今为止在网站上所教授的内容,这是有效的。
def replace(L, X, Y):
while X in L:
i = L.index(X)
L.insert(i, Y)
L.remove(X)
回答by ToanHD
totally agree with Asad Saeeduddin but, 1st value of i must be -1, it will help replace the 1st object in list in need
完全同意 Asad Saeeduddin 但是,i 的第一个值必须是 -1,这将有助于替换列表中需要的第一个对象
def replace(l, X, Y):
i = -1
for v in l:
i += 1
if v == X:
l.pop(i) # remove item at given position (position number i)
l.insert(i, Y) # insert item Y at position i
l = [1, 1, 4, 1, 5, 9]
replace(l, 1, 7)
print(l)