在Python中通过字典或列表执行FOR循环时,如何获得项的键?

时间:2022-03-18 14:28:23

I am new to Python.

我对Python不熟。

Say I have a list:

假设我有一个列表:

list = ['A','B','C','D']

The key for each item respectively here is 0,1,2,3 - right?

这里每一项的键分别是0 1 2 3 -对吧?

Now I am going to loop through it with a for loop...

现在我要用for循环来循环…

for item in list:
    print item

That's great, I can print out my list.

太好了,我可以把我的清单打印出来。

How do I get the key here? For example being able to do:

我怎么才能拿到钥匙?例如:

print key
print item

on each loop?

在每个循环?

If this isn't possible with a list, where keys are not declared myself, is it possible with a Dictionary?

如果在列表中,键不被声明,这是不可能的,那么在字典中呢?

Thanks

谢谢

3 个解决方案

#1


9  

The answer is different for lists and dicts.

答案不同于列表和dicts。

A list has no key. Each item will have an index. You can enumerate a list like this:

列表没有键。每个项目都有一个索引。你可以列举如下清单:

>>> l = ['A','B','C','D']
>>> for index, item in enumerate(l):
...     print index
...     print item
... 
0
A
1
B
2
C
3
D

I used your example here, but called the list 'l', to avoid a * with a reserved word.

我在这里用了你的例子,但把列表叫做“l”,以避免与保留词发生冲突。

If you iterate over a dict, you are handed the key:

如果你遍历一条法令,你会得到钥匙:

>>> d = {0: 'apple', 1: 'banana', 2: 'cherry', 3: 'damson', }
>>> for k in d:
...     print k
...     print d[k]
... 
0
apple
1
banana
2
cherry
3
damson

#2


10  

You want the index, not the key. For this you can use enumerate:

你想要索引,而不是键。为此,可以使用enumerate:

for index, item in enumerate(l):
    print index
    print item

This is mentioned in the section Looping Techniques in the Python tutorial which also covers looping over dictionaries while getting both the key and the value.

在Python教程的循环技术一节中提到了这一点,该节还介绍了在获取键和值的同时对字典进行循环。

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.iteritems():
...     print k, v
...
gallahad the pure
robin the brave

#3


3  

You should use a dictionary, anyway you could do something like:

你应该使用字典,无论如何你可以做一些像:

for index, item in enumerate(yourlist):
    print "index:%d item:%s" % (index, item)

#1


9  

The answer is different for lists and dicts.

答案不同于列表和dicts。

A list has no key. Each item will have an index. You can enumerate a list like this:

列表没有键。每个项目都有一个索引。你可以列举如下清单:

>>> l = ['A','B','C','D']
>>> for index, item in enumerate(l):
...     print index
...     print item
... 
0
A
1
B
2
C
3
D

I used your example here, but called the list 'l', to avoid a * with a reserved word.

我在这里用了你的例子,但把列表叫做“l”,以避免与保留词发生冲突。

If you iterate over a dict, you are handed the key:

如果你遍历一条法令,你会得到钥匙:

>>> d = {0: 'apple', 1: 'banana', 2: 'cherry', 3: 'damson', }
>>> for k in d:
...     print k
...     print d[k]
... 
0
apple
1
banana
2
cherry
3
damson

#2


10  

You want the index, not the key. For this you can use enumerate:

你想要索引,而不是键。为此,可以使用enumerate:

for index, item in enumerate(l):
    print index
    print item

This is mentioned in the section Looping Techniques in the Python tutorial which also covers looping over dictionaries while getting both the key and the value.

在Python教程的循环技术一节中提到了这一点,该节还介绍了在获取键和值的同时对字典进行循环。

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.iteritems():
...     print k, v
...
gallahad the pure
robin the brave

#3


3  

You should use a dictionary, anyway you could do something like:

你应该使用字典,无论如何你可以做一些像:

for index, item in enumerate(yourlist):
    print "index:%d item:%s" % (index, item)