我们似乎无法获得我们在四个小组中花费的最后三个小时的输出,试图解决这个问题。下面你将看到我们的代码以及列表中的代码,我们从中调用字符串和所需的输出。请帮忙。
我们的代码是:
def remove_value(my_list, remove_position):
new_list = []
count = 0
if remove_position < 0 or remove_position == 0:
remove_position = 0
for i in my_list:
if (i) != (value):
new_list.append(i)
count = count+1
return new_list
if remove_position>length(my_list):
new_list.append(value)
return new_list
我试图获取输出的列表是:
print("\nremove_value Test")
str_list5 = ['r','i','n','g']
new_list = list_function.remove_value(str_list5, 2)
print(new_list)
new_list = list_function.remove_value(str_list5, -1)
print(new_list)
new_list = list_function.remove_value(str_list5, 10)
print(new_list)
要求外出是:
remove_value Test
['r', 'i', 'g']
['i', 'n', 'g']
['r', 'i', 'n']
答案 0 :(得分:2)
既然你们正在努力学习它,而不是给你解决方案。以下是从列表中删除对象的提示。
根据索引从原始列表中删除:
>>> my_list = [0, 1, 2, 3, 4, 5]
>>> del my_list[1]
>>> my_list # Deleted value at index 1
[0, 2, 3, 4, 5]
根据索引创建列表跳过值的副本:
>>> my_list = [0, 1, 2, 3, 4, 5]
>>> new_list = my_list[:2] + my_list[3:]
>>> new_list # new list with the value of index `2` of original list
[0, 1, 3, 4, 5]
根据索引从列表中删除对象,返回删除的值:
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list.pop(3) # remove object at index 3
4 # value of 3rd index returned
>>> my_list # remaining list
[1, 2, 3, 5]
答案 1 :(得分:1)
所以我认为最简单的方法就是这样:
def remove_value(my_list, remove_position):
if remove_position < 0:
remove_position = 0
return [ value for index,value in enumerate(my_list) if index != remove_position ]
或者您可以在列表副本上使用就地pop
函数,如下所示:
new_list = list(my_list) # copy it
new_list.pop(remove_position) # remove the index @ remove position
答案 2 :(得分:0)
我不打算修改您的算法/代码,这是您的努力。只是代码中的一些注释:
在您现有的通话中,对于所需的输出,此通话是错误的。 [-1]是最后一个元素,所以&#39; g&#39;将不在列表中。您应该执行索引为0的函数。
new_list = list_function.remove_value(str_list5, -1) ==> (str_list5, 0)
这种情况是无意义的,因为在这种情况下remove_position已经为0。
if remove_position == 0:
remove_position = 0
没有长度(列表),它是len(列表)。
永远不会使用变量计数。
你要回来两次,其中一次是时间之前(建议)
答案 3 :(得分:0)
适用的代码版本:
def remove_value(my_list, remove_position):
new_list = []
if remove_position < 0:
remove_position = 0
if remove_position >= len(my_list):
remove_position = len(my_list)-1
for i in range(len(my_list)):
if i != remove_position:
new_list.append(my_list[i])
return my_list
更好的方法:
def remove_value(my_list, remove_position):
if remove_position < 0:
remove_position = 0
if remove_position >= len(my_list):
remove_position = len(my_list)-1
new_list = my_list[:]
new_list.pop(remove_position)
return new_list
答案 4 :(得分:0)
我认为问题在于for i in my_list
。
查看代码,my_list
为空。
答案 5 :(得分:0)
因为lists
是python中的可变对象
每次执行时都需要创建多个列表
对特定字符串的任何操作都会变异(更新)。
为此问题设计解决方案有不同的方法:
# remove item on index 2 of `str_list1`
str_list1 = ['r','i','n','g']
del str_list1[2]
# remove item on first of `str_list2`
str_list2 = ['r','i','n','g']
del str_list2[0]
# remove item on last index of `str_list3`
str_list3 = ['r','i','n','g']
del str_list3[3] # or you can use -1
# remove item on index 2 of `str_list1`
str_list1 = ['r','i','n','g']
str_list1.remove('n')
# remove item on first of `str_list2`
str_list2 = ['r','i','n','g']
str_list2.remove('r')
# remove item on last index of `str_list3`
str_list3 = ['r','i','n','g']
str_list3.remove('g')
=====结果======
# Results
print(str_list1)
print(str_list2)
print(str_list3)
Results
:
['r', 'i', 'g']
['i', 'n', 'g']
['r', 'i', 'n']
希望这也有助于您使用代码进行工作/学习。
如需更深入了解list
,请按照此处的语法和文档进行操作: