【发布时间】:2012-10-20 13:19:19
【问题描述】:
my_list = ['apple', 'pear', 'orange', 'raspberry']
# I know that I'm always looking for pear.
print 'pear' in my_list # prints True
# I want to be able to get a key by its value.
pear_key = my_list['pear'].key # should be 1
# Print the next item in the list.
print my_list[pear_key + 1] # should print orange
我知道pear 将始终是我列表中的一个项目(虽然不是位置),我正在寻找一种方法来获取该列表中下一个项目的值,或者通过获取当前键通过了解它的价值并将其提升一个(就像我在上面的示例中所做的那样)或使用类似 my_list.next 的东西。
【问题讨论】: