相关疑难解决方法(0)

如何查找列表中所有出现的元素?

index()只会在列表中首次出现一个项目.是否有一个巧妙的技巧可以返回列表中的所有索引?

python list

326
推荐指数
9
解决办法
35万
查看次数

使用列表查找列表中多个项目的索引

我想本质上使用一个列表,即。

L = [10, 10, 100, 10, 17, 15] 
Run Code Online (Sandbox Code Playgroud)

并使用另一个列表

R = [10, 15] 
Run Code Online (Sandbox Code Playgroud)

想回来

N = [0, 1, 3, 5] // indices of L that return the values in R
Run Code Online (Sandbox Code Playgroud)

索恩的尝试

我尝试使用L.index()获取索引,但是仅返回第一个值。然后,我尝试在L上运行一个for循环,并每次使用L.index(R [0]),但是类似地,它仅返回其找到的第一个索引。

 for i in range(len(L)):
       j = R[i]
       N.append(L.index(j))
 return N
Run Code Online (Sandbox Code Playgroud)

这将返回超出范围的索引,这是有意义的,但是我如何使其遍历L?

谢谢

python list

5
推荐指数
1
解决办法
4455
查看次数

给出两个列表的匹配元素的索引,其中一个列表具有冗余条目

我有两个清单,a并且b.a包含我想知道的匹配元素索引的元素b.在b,每个元素都是独一无二的,不同于a.

a = [1993, 1993, 1994, 1995, 1996, 1996, 1998, 2003, 2005, 2005]
b = [1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014]
Run Code Online (Sandbox Code Playgroud)

使用Python中查找列表中匹配元素索引的解决方案:

matching …
Run Code Online (Sandbox Code Playgroud)

python indexing list

2
推荐指数
1
解决办法
128
查看次数

标签 统计

list ×3

python ×3

indexing ×1