Python 中的数组索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15726618/
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
Array Indexing in Python
提问by user2228590
Beginner here, learning python, was wondering something.
这里的初学者,学习 python,想知道一些事情。
This gives me the second element:
这给了我第二个元素:
list = [1,2,3,4]
list.index(2)
2
But when i tried this:
但是当我尝试这个时:
list = [0] * 5
list[2] = [1,2,3,4]
list.index[4]
I get an error. Is there some way to pull the index of an element from an array, no matter what list it's placed into? I know it's possible with dictionaries:
我收到一个错误。有没有办法从数组中提取元素的索引,不管它放在什么列表中?我知道字典是可能的:
info = {first:1,second:2,third:3}
for i in info.values:
print i
1
2
3
Is there something like that for lists?
列表有类似的东西吗?
回答by sapi
In your second example, your list is going to look like this:
在您的第二个示例中,您的列表将如下所示:
[0, 0, [1, 2, 3, 4], 0, 0]
There's therefore no element 4in the list.
因此4列表中没有元素。
This is because when you set list[2], you are changing the third element, not updating further elements in the list.
这是因为当您设置时list[2],您正在更改第三个元素,而不是更新列表中的其他元素。
If you want to replace a range of values in the list, use slicing notation, for example list[2:](for 'every element from the third to the last').
例如,如果要替换列表中的某个范围的值,请使用切片符号list[2:](对于“从第三个到最后一个的每个元素”)。
More generally, the .indexmethod operates on identities. So the following will work, because you're asking python where the particular list object you inserted goes in the list:
更一般地,该.index方法对身份进行操作。因此,以下将起作用,因为您要询问 python 您插入的特定列表对象在列表中的位置:
lst = [0]*5
lst2 = [1,2,3,4]
lst[2] = lst2
lst.index(lst2) # 2
回答by icktoofay
The indexmethod does not do what you expect. To get an item at an index, you must use the []syntax:
该index方法不符合您的预期。要在索引处获取项目,您必须使用以下[]语法:
>>> my_list = ['foo', 'bar', 'baz']
>>> my_list[1] # indices are zero-based
'bar'
indexis used to get an index from an item:
index用于从项目中获取索引:
>>> my_list.index('baz')
2
If you're asking whether there's any way to get indexto recurse into sub-lists, the answer is no, because it would have to return something that you could then pass into [], and []never goes into sub-lists.
如果你问是否有办法index递归到子列表中,答案是否定的,因为它必须返回一些你可以传入的东西[],而[]永远不会进入子列表。
回答by Burhan Khalid
The answer to your question is no, but you have some other issues with your code.
您的问题的答案是否定的,但是您的代码还有其他一些问题。
First, do not use listas a variable name, because its also the name of the built-in function list.
首先,不要list用作变量名,因为它也是内置函数的名称list。
Secondly, list.index[4]is different than list.index(4); both will give errors in your case, but they are two different operations.
其次,list.index[4]不同于list.index(4); 两者都会在您的情况下出错,但它们是两种不同的操作。
回答by Veerabahu
If you want to pull the index of a particular element then index function will help. However, enumeratewill do similar to the dictionary example,
如果您想拉取特定元素的索引,那么 index 函数会有所帮助。然而,enumerate会做类似于字典的例子,
>>> l=['first','second','third']
>>> for index,element in enumerate(l):
... print index,element
...
output
输出
0 first
1 second
2 third
回答by Idrisi_Kasim
list is an inbuilt function don't use it as variable name it is against the protocol instead use lst.
list 是一个内置函数,不要将它用作变量名,它违反协议,而是使用 lst。
To access a element from a list use [ ] with index number of that element
要访问列表中的元素,请使用 [] 和该元素的索引号
lst = [1,2,3,4]
lst[0]
1
one more example of same
另一个相同的例子
lst = [1,2,3,4]
lst[3]
4
Use (:) semicolon to access elements in series first index number before semicolon is Included & Excluded after semicolon
使用 (:) 分号访问系列中的元素,分号前的第一个索引号在分号后包含和排除
lst[0:3]
[1, 2, 3]
If index number before semicolon is not specified then all the numbers is included till the start of the list with respect to index number after semicolon
如果未指定分号前的索引号,则所有数字都包含在分号后的索引号的列表开始之前
lst[:2]
[1, 2]
If index number after semicolon is not specified then all the numbers is included till the end of the list with respect to index number before semicolon
如果未指定分号后的索引号,则所有数字都包含到列表末尾,相对于分号前的索引号
lst[1:]
[2, 3, 4]
If we give one more semicolon the specifield number will be treated as steps
如果我们再给一个分号,则特定字段数将被视为步骤
lst[0:4:2]
[1, 3]
This is used to find the specific index number of a element
这用于查找元素的特定索引号
lst.index(3)
2
This is one of my favourite the pop function it pulls out the element on the bases of index provided more over it also remove that element from the main list
这是我最喜欢的 pop 函数之一,它从索引的基础上拉出元素,提供更多信息,它还从主列表中删除该元素
lst.pop(1)
2
Now see the main list the element is removed..:)
现在看到元素被删除的主列表..:)
lst
[1, 3, 4]
For extracting even numbers from a given list use this, here i am taking new example for better understanding
为了从给定的列表中提取偶数,请使用这个,这里我举了一个新的例子来更好地理解
lst = [1,1,2,3,4,44,45,56]
import numpy as np
lst = np.array(lst)
lst = lst[lst%2==0]
list(lst)
[2, 4, 44, 56]
For extracting odd numbers from a given list use this (Note where i have assingn 1 rather than 0)
为了从给定的列表中提取奇数,请使用这个(注意我的位置是 1 而不是 0)
lst = [1,1,2,3,4,44,45,56]
import numpy as np
lst = np.array(lst)
lst = lst[lst%2==1]
list(lst)
[1, 1, 3, 45]
Happy Learning...:)
快乐学习...:)

