我如何访问指数,同时在一个序列上以一个为旋转的序列?

xs = [8, 23, 45]

for x in xs:
    print("item #{} = {}".format(index, x))

所需的产量:

item #1 = 8
item #2 = 23
item #3 = 45

当前回答

接入指数与性能平衡方法

在 Python 3.7 中访问列表指数的最快方法是使用小、中等和大列表的列表方法。

请在下面的代码样本中查看可以使用的不同方法,以便在列表和访问指数值和其性能测量(我认为这对您有用):

# Using range
def range_loop(iterable):
    for i in range(len(iterable)):
        1 + iterable[i]

# Using enumerate
def enumerate_loop(iterable):
    for i, val in enumerate(iterable):
        1 + val

# Manual indexing
def manual_indexing_loop(iterable):
    index = 0
    for item in iterable:
        1 + item
        index += 1

查看下面的每个方法的性能测量:

from timeit import timeit

def measure(l, number=10000):
    print("Measure speed for list with %d items" % len(l))
    print("range: ", timeit(lambda :range_loop(l), number=number))
    print("enumerate: ", timeit(lambda :enumerate_loop(l), number=number))
    print("manual_indexing: ", timeit(lambda :manual_indexing_loop(l), number=number))

# Measure speed for list with 1000 items
measure(range(1000))
# range:  1.161622366
# enumerate:  0.5661940879999996
# manual_indexing:  0.610455682

# Measure speed for list with 100000 items
measure(range(10000))
# range:  11.794482958
# enumerate:  6.197628574000001
# manual_indexing:  6.935181098000001

# Measure speed for list with 10000000 items
measure(range(10000000), number=100)
# range:  121.416859069
# enumerate:  62.718909123
# manual_indexing:  69.59575057400002

因此,使用列出的方法是当指数需要时最快的 iteration 方法。

下面添加一些有用的链接:

在 Python 2.X 中,范围和 xrange 函数有什么区别? 使用列表或使用 xrange 在 Python 中使用列表更快的是什么? 范围(len(list)) 或列表(list)?

其他回答

使用列表以获取与元素的指数,如您引用:

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

请注意,Python的指数从零开始,所以你会得到0到4与上面的。

count = 0 # in case items is empty and you need it after the loop
for count, item in enumerate(items, start=1):
    print(count, item)

无线控制流

索引 = 0 # Python 的索引从零开始,以便在项目中的项目: # Python 的索引为“每个”卷印(索引,项目)索引 += 1

指数在范围(列(项目)):印刷(指数,项目(项目))

使用列出的功能

for index, item in enumerate(items, start=0):   # default is zero
    print(index, item)

得到一个计算

count = 0 # in case items is empty
for count, item in enumerate(items, start=1):   # default is zero
    print(item)

print('there were {0} items printed'.format(count))


items = ['a', 'b', 'c', 'd', 'e']

enumerate_object = enumerate(items) # the enumerate object

iteration = next(enumerate_object) # first iteration from enumerate
print(iteration)

(0, 'a')

我们可以使用所谓的“序列脱包”来提取这些二重元素:

index, item = iteration
#   0,  'a' = (0, 'a') # essentially this.

>>> print(index)
0
>>> print(item)
a

结论

Python 指数从零开始 若要从一个不可分割的指数中获取这些指数,当您在其上方进行引用时,使用列表函数以异常的方式使用列表(与无包装一起)创建更可读、更可维持的代码:

for index, item in enumerate(items, start=0):   # Python indexes start at zero
    print(index, item)

在你的问题上,你写着“我如何访问路径指数,从1到5在这种情况下?”

但是,一个列表的指数从零开始,所以我们需要知道你真正想要的是列表中的每个项目的指数和项目,或者你真的想要从1开始的数字。

首先,为了澄清,列出的函数以序列返回指数和列表中的每个项目的相应项目。

alist = [1, 2, 3, 4, 5]

for n, a in enumerate(alist):
    print("%d %d" % (n, a))

此时此刻,上面的结果是:

0 1
1 2
2 3
3 4
4 5

请注意,指数从 0 运行,这种类型的指数是现代编程语言中常见的,包括 Python 和 C。

如果您希望您的路径扩展列表的一部分,您可以使用标准Python合成列表的一部分。 例如,从列表中的第二个项目转向,但不包括最后一个项目,您可以使用

for n, a in enumerate(alist[1:-1]):
    print("%d %d" % (n, a))

请注意,再次,输出指数从0运行。

0 2
1 3
2 4

for n, a in enumerate(alist, start=1):
    print("%d %d" % (n, a))

其产量为何

1 1
2 2
3 3
4 4
5 5

使用内置函数列():

for idx, x in enumerate(xs):
    print(idx, x)

它是非皮托尼的手动索引通过 i 在范围(len(xs)): x = xs(i) 或手动管理一个额外的状态变量。

查看 PEP 279 更多信息

除了上述所有优秀的答案之外,这里还有解决这个问题的解决方案,当与Pandas系列对象一起工作时。在许多情况下,Pandas系列有自定义/独特的指标(例如,独特的识别线),无法通过列出的()函数访问。

下面我们可以看到,列表()不会给我们所需的结果:

id, x in enumerate(xs): print("id #{} = {}".format(id, x)) 输出: # id #0 = 8 # id #1 = 23 # id #2 = 45

我们可以使用.items( ) 访问一个 pandas 系列的指标,以便使用 A for loop:

对于 id, x 在 xs.items(): 打印("id #{} = {}".format(id, x)) 输出: # id #G923002 = 8 # id #G923004 = 23 # id #G923005 = 45

您可以使用指数方法:

ints = [8, 23, 45, 12, 78]
inds = [ints.index(i) for i in ints]

在评论中强调,如果在英寸中有重复,这种方法不会工作,下面的方法应该为英寸中的任何值工作:

ints = [8, 8, 8, 23, 45, 12, 78]
inds = [tup[0] for tup in enumerate(ints)]

或替代

ints = [8, 8, 8, 23, 45, 12, 78]
inds = [tup for tup in enumerate(ints)]

如果你想得到指数和值在英寸作为列表的<unk>。

它使用列表方法在这个问题的选择答案,但与列表理解,使它更快的代码较少。