一个列表项如何有 2 个索引?
How can one list item have 2 indexes?
假设我写了一个列表:
foo=[1,2,3,4,5,6,7,8,9,1]
当我试图在foo
中找出1
的索引时,结果是0。但是我想最后找到1
的索引(它的预期索引是 9)所以我写了这个简单的代码,它会给我列表中所有项目的所有索引:
for i in foo:
print(foo.index(i))
结果是:
0
1
2
3
4
5
6
7
8
0
请注意,根据上述迭代,foo 中的最后一个 1
的索引也为零。
现在我尝试使用以下方法删除索引为 9 的列表项:
del foo[9]
和最后一个 1
被删除,即使迭代证明它有一个索引 0!
为什么会这样?
让我们看看index
函数的文档:
list.index(x[, start[, end]])
Return zero-based index in the list of the first item whose value is x
. Raises a ValueError
if there is no such item.
因此 returns 第一个 项具有该值。这就解释了为什么 index(1)
的结果总是 0。
当您使用 del foo[9]
时,您是在告诉它删除列表中位置 9 的对象。虽然它与元素 0 具有相同的值,但它不是同一个对象。我们可以通过做
来证明这一点
>>>foo[0] is foo[9]
False
这是预期的行为。仅仅因为两个对象具有相同的值并不意味着它们是同一个对象。
当您使用 index()
方法时,它 returns 您是搜索项出现的第一个实例。
看看这个
>>> a = [5, 4 , 1 , 3 , 1 , 2]
>>> a.index(1)
2
无论你做什么,迭代多少次,这都不会改变。
您可以改为使用
for index, item in enumerate(foo):
print(index)
index()
从列表开头搜索,returns 一次找到。所以 1
的索引是 0。一种解决方案是枚举你自己。
def index_all(lst, x):
return [i for i, v in enumerate(lst) if v == x]
print(index_all([1,2,3,4,5,6,7,8,9,1], 1))
在 Python 中,如果找到一个项目,迭代将退出并且不会检查其他项目。
因此,如果您使用必须查找的索引元素调用索引函数。 index 函数将只检索它找到的与您的参数匹配的第一个元素的索引(在本例中为 i)
如果你想要所有可能的索引,你需要使用 list comphrehension
foo=[1,2,3,4,5,6,7,8,9,1]
bar = [i for i,x in enumerate(foo) if i == 1]
print(bar)
不明白的请看下面源码解释
1. help(list)
2.
def index(self, value, start=None, stop=None):
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
获取所有索引:
>>> [i for i, num in enumerate(foo) if num == 1]
[0, 9]
获取最大索引:
>>> max([i for i, num in enumerate(foo) if num == 1])
9
问题是,您将传递 i 作为函数索引的参数
并且文档说 index(x) "Return the smallest i such that i is the index of the first occurrence of x"。
所以每次你通过 1,它总是第一次出现
但检查这个
foo=[1,2,3,4,5,6,7,8,9,10]
>>> for i in foo:
... print(foo.index(i))
...
0
1
2
3
4
5
6
7
8
9
这会产生不同的结果,因为所有元素都是不同的
你可以这样试试:
foo=[1,2,3,4,5,6,7,8,9,1]
for i, val in enumerate(foo):
if val == 1:
print(i)
'''
output :
0
9
'''
假设我写了一个列表:
foo=[1,2,3,4,5,6,7,8,9,1]
当我试图在foo
中找出1
的索引时,结果是0。但是我想最后找到1
的索引(它的预期索引是 9)所以我写了这个简单的代码,它会给我列表中所有项目的所有索引:
for i in foo:
print(foo.index(i))
结果是:
0
1
2
3
4
5
6
7
8
0
请注意,根据上述迭代,foo 中的最后一个 1
的索引也为零。
现在我尝试使用以下方法删除索引为 9 的列表项:
del foo[9]
和最后一个 1
被删除,即使迭代证明它有一个索引 0!
为什么会这样?
让我们看看index
函数的文档:
list.index(x[, start[, end]])
Return zero-based index in the list of the first item whose value is
x
. Raises aValueError
if there is no such item.
因此 returns 第一个 项具有该值。这就解释了为什么 index(1)
的结果总是 0。
当您使用 del foo[9]
时,您是在告诉它删除列表中位置 9 的对象。虽然它与元素 0 具有相同的值,但它不是同一个对象。我们可以通过做
>>>foo[0] is foo[9]
False
这是预期的行为。仅仅因为两个对象具有相同的值并不意味着它们是同一个对象。
当您使用 index()
方法时,它 returns 您是搜索项出现的第一个实例。
看看这个
>>> a = [5, 4 , 1 , 3 , 1 , 2]
>>> a.index(1)
2
无论你做什么,迭代多少次,这都不会改变。 您可以改为使用
for index, item in enumerate(foo):
print(index)
index()
从列表开头搜索,returns 一次找到。所以 1
的索引是 0。一种解决方案是枚举你自己。
def index_all(lst, x):
return [i for i, v in enumerate(lst) if v == x]
print(index_all([1,2,3,4,5,6,7,8,9,1], 1))
在 Python 中,如果找到一个项目,迭代将退出并且不会检查其他项目。
因此,如果您使用必须查找的索引元素调用索引函数。 index 函数将只检索它找到的与您的参数匹配的第一个元素的索引(在本例中为 i)
如果你想要所有可能的索引,你需要使用 list comphrehension
foo=[1,2,3,4,5,6,7,8,9,1]
bar = [i for i,x in enumerate(foo) if i == 1]
print(bar)
不明白的请看下面源码解释
1. help(list) 2. def index(self, value, start=None, stop=None): """ L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0
获取所有索引:
>>> [i for i, num in enumerate(foo) if num == 1]
[0, 9]
获取最大索引:
>>> max([i for i, num in enumerate(foo) if num == 1])
9
问题是,您将传递 i 作为函数索引的参数 并且文档说 index(x) "Return the smallest i such that i is the index of the first occurrence of x"。 所以每次你通过 1,它总是第一次出现 但检查这个
foo=[1,2,3,4,5,6,7,8,9,10]
>>> for i in foo:
... print(foo.index(i))
...
0
1
2
3
4
5
6
7
8
9
这会产生不同的结果,因为所有元素都是不同的
你可以这样试试:
foo=[1,2,3,4,5,6,7,8,9,1]
for i, val in enumerate(foo):
if val == 1:
print(i)
'''
output :
0
9
'''