Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—在列表中查找值位置并作为新列表输出_Python_List - Fatal编程技术网

Python—在列表中查找值位置并作为新列表输出

Python—在列表中查找值位置并作为新列表输出,python,list,Python,List,目前正在我的课程中学习python,这一部分我有点困惑,不幸的是我的下一个prac要到下周才能开始,所以我想在这里问一下 我们应该编写一个名为find()的函数,它获取一个列表,搜索列表中的值,并将找到的数字作为列表中位置的新列表返回。例如: list = 2, 3, 7, 2, 3, 3, 5 number = 3 输出将是:0,4,5 这个问题要求我们使用循环,但除非指定,否则不要使用内置函数、切片表达式、列表方法或字符串方法,为此部分指定的唯一方法是:.append()和range()

目前正在我的课程中学习python,这一部分我有点困惑,不幸的是我的下一个prac要到下周才能开始,所以我想在这里问一下

我们应该编写一个名为find()的函数,它获取一个列表,搜索列表中的值,并将找到的数字作为列表中位置的新列表返回。例如:

list = 2, 3, 7, 2, 3, 3, 5
number = 3
输出将是:
0,4,5

这个问题要求我们使用循环,但除非指定,否则不要使用内置函数、切片表达式、列表方法或字符串方法,为此部分指定的唯一方法是:
.append()
range()

我们收到了一个不允许编辑的文件:

import test_lists

list_A = ['q', 'w', 'e', 'r', 't', 'y']

print(test_lists.find(list_A, 'r'))
print(test_lists.find(list_A, 'b'))
根据我在一个名为test_list的文件中尝试的内容,输出给出了

有谁能把我推到正确的方向并向我解释一下吗?我们被告知可以使用
.append()
,但我似乎不明白这将如何适应这种情况,因为据我所知,append只会添加到字符串中。我觉得我离目标太远了

感谢您使用和:

顺便说一句,不要使用
list
作为变量名。它隐藏内置类型/功能
列表

更新

不使用
枚举

>>> lst = 2, 3, 7, 2, 3, 3, 5
>>> number = 3
>>> [i for i in range(len(lst)) if lst[i] == number]
[1, 4, 5]

我想你不能用
枚举

然后,您必须自己做同样的事情:迭代列表的索引,对于每个索引,查看元素是否是预期的元素。如果是,则将索引放入结果列表中:

expected_element = 3
my_list = [...]
result_list = []
for i in range(len(my_list)):
    if my_list[i] == expected_element:
        result_list.append(i)
这不是很像蟒蛇,但如果你有这么多的禁忌,我觉得很好

如果您不能使用
len(我的列表)
,您可以通过第一次迭代获得大小:

def size(l):
    s = 0
    for _ in l:
        s += 1
    return s

列表理解将是实现这一点的最有效的方法。但是为了教学目的明确地写它,这里有另一种方法

def find(number, numList):
    indexMatches = []                    # initialize your index array
    for index in range(len(numList)):    # iterate over the list of values
        if number == numList[index]:     # compare the number against the current value
            indexMatches.append(index)   # if they match, add it to the index array
    return indexMatches                  # return the list of indexes where matches occured

find(3, [2,3,7,2,3,3,5])
输出

[1, 4, 5]
这样行吗

def find(list1, listValue):
    found = []
    for index in range(0,len(list1)):
        if list1[index]==listValue:
            found.append(index)
    return found

你是说
1,4,5
?嗯,我假设它从0开始计算,而不是从1开始。这只是一个例子。希望在下面的部分中找到r在该列表中的位置感谢您的回复,但是正如我在问题中所述,我们不能使用内置函数,其中包括
enumerate()
。我们只能使用
range()
函数和
.append()
方法来获得输出。那么快速的变化是:
[i for i in range(len(lst)),如果lst[i]==number]
[1, 4, 5]
def find(list1, listValue):
    found = []
    for index in range(0,len(list1)):
        if list1[index]==listValue:
            found.append(index)
    return found