我如何访问指数,同时在一个序列上以一个为旋转的序列?
xs = [8, 23, 45]
for x in xs:
print("item #{} = {}".format(index, x))
所需的产量:
item #1 = 8
item #2 = 23
item #3 = 45
我如何访问指数,同时在一个序列上以一个为旋转的序列?
xs = [8, 23, 45]
for x in xs:
print("item #{} = {}".format(index, x))
所需的产量:
item #1 = 8
item #2 = 23
item #3 = 45
当前回答
这里是如何使用 for-in 插槽访问指标和序列元素。
items = [8, 23, 45, 12, 78]
counter = 0
for value in items:
print(counter, value)
counter += 1
结果:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
二、使用列表( )方法进行列表。
items = [8, 23, 45, 12, 78]
for i in enumerate(items):
print("index/value", i)
结果:
# index/value (0, 8)
# index/value (1, 23)
# index/value (2, 45)
# index/value (3, 12)
# index/value (4, 78)
3、使用指数和值单独。
items = [8, 23, 45, 12, 78]
for index, value in enumerate(items):
print("index", index, "for value", value)
结果:
# index 0 for value 8
# index 1 for value 23
# index 2 for value 45
# index 3 for value 12
# index 4 for value 78
您可以将指数数字更改到任何增加。
items = [8, 23, 45, 12, 78]
for i, value in enumerate(items, start=1000):
print(i, value)
# 1000 8
# 1001 23
# 1002 45
# 1003 12
# 1004 78
5、自动对比增加与范围(len(...))。
items = [8, 23, 45, 12, 78]
for i in range(len(items)):
print("Index:", i, "Value:", items[i])
结果:
# ('Index:', 0, 'Value:', 8)
# ('Index:', 1, 'Value:', 23)
# ('Index:', 2, 'Value:', 45)
# ('Index:', 3, 'Value:', 12)
# ('Index:', 4, 'Value:', 78)
6、使用内部函数。
items = [8, 23, 45, 12, 78]
def enum(items, start=0):
counter = start
for value in items:
print(counter, value)
counter += 1
enum(items)
结果:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
当然,我们不能忘记在旋转时。
items = [8, 23, 45, 12, 78]
counter = 0
while counter < len(items):
print(counter, items[counter])
counter += 1
结果:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
8、输出返回发电机对象的声明。
def createGenerator():
items = [8, 23, 45, 12, 78]
for (j, k) in enumerate(items):
yield (j, k)
generator = createGenerator()
for i in generator:
print(i)
结果:
# (0, 8)
# (1, 23)
# (2, 45)
# (3, 12)
# (4, 78)
9. Inline 表达与 for-in loop 和 lambda。
items = [8, 23, 45, 12, 78]
xerox = lambda upperBound: [(i, items[i]) for i in range(0, upperBound)]
print(xerox(5))
结果:
# [(0, 8), (1, 23), (2, 45), (3, 12), (4, 78)]
10. 使用 Python 的 zip() 函数同时插入两个列表。
items = [8, 23, 45, 12, 78]
indices = [0, 1, 2, 3, 4]
for item, index in zip(items, indices):
print("{}: {}".format(index, item))
结果:
# 0: 8
# 1: 23
# 2: 45
# 3: 12
# 4: 78
超過 2 個列表,使用一時 loop 和 iter() 和 next() 方法。
items = [8, 23, 45, 12, 78]
indices = range(len(items))
iterator1 = iter(indices)
iterator2 = iter(items)
try:
while True:
i = next(iterator1)
element = next(iterator2)
print(i, element)
except StopIteration:
pass
结果:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
其他回答
在你的问题上,你写着“我如何访问路径指数,从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 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)
这里是如何使用 for-in 插槽访问指标和序列元素。
items = [8, 23, 45, 12, 78]
counter = 0
for value in items:
print(counter, value)
counter += 1
结果:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
二、使用列表( )方法进行列表。
items = [8, 23, 45, 12, 78]
for i in enumerate(items):
print("index/value", i)
结果:
# index/value (0, 8)
# index/value (1, 23)
# index/value (2, 45)
# index/value (3, 12)
# index/value (4, 78)
3、使用指数和值单独。
items = [8, 23, 45, 12, 78]
for index, value in enumerate(items):
print("index", index, "for value", value)
结果:
# index 0 for value 8
# index 1 for value 23
# index 2 for value 45
# index 3 for value 12
# index 4 for value 78
您可以将指数数字更改到任何增加。
items = [8, 23, 45, 12, 78]
for i, value in enumerate(items, start=1000):
print(i, value)
# 1000 8
# 1001 23
# 1002 45
# 1003 12
# 1004 78
5、自动对比增加与范围(len(...))。
items = [8, 23, 45, 12, 78]
for i in range(len(items)):
print("Index:", i, "Value:", items[i])
结果:
# ('Index:', 0, 'Value:', 8)
# ('Index:', 1, 'Value:', 23)
# ('Index:', 2, 'Value:', 45)
# ('Index:', 3, 'Value:', 12)
# ('Index:', 4, 'Value:', 78)
6、使用内部函数。
items = [8, 23, 45, 12, 78]
def enum(items, start=0):
counter = start
for value in items:
print(counter, value)
counter += 1
enum(items)
结果:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
当然,我们不能忘记在旋转时。
items = [8, 23, 45, 12, 78]
counter = 0
while counter < len(items):
print(counter, items[counter])
counter += 1
结果:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
8、输出返回发电机对象的声明。
def createGenerator():
items = [8, 23, 45, 12, 78]
for (j, k) in enumerate(items):
yield (j, k)
generator = createGenerator()
for i in generator:
print(i)
结果:
# (0, 8)
# (1, 23)
# (2, 45)
# (3, 12)
# (4, 78)
9. Inline 表达与 for-in loop 和 lambda。
items = [8, 23, 45, 12, 78]
xerox = lambda upperBound: [(i, items[i]) for i in range(0, upperBound)]
print(xerox(5))
结果:
# [(0, 8), (1, 23), (2, 45), (3, 12), (4, 78)]
10. 使用 Python 的 zip() 函数同时插入两个列表。
items = [8, 23, 45, 12, 78]
indices = [0, 1, 2, 3, 4]
for item, index in zip(items, indices):
print("{}: {}".format(index, item))
结果:
# 0: 8
# 1: 23
# 2: 45
# 3: 12
# 4: 78
超過 2 個列表,使用一時 loop 和 iter() 和 next() 方法。
items = [8, 23, 45, 12, 78]
indices = range(len(items))
iterator1 = iter(indices)
iterator2 = iter(items)
try:
while True:
i = next(iterator1)
element = next(iterator2)
print(i, element)
except StopIteration:
pass
结果:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
你也可以尝试一下:
data = ['itemA.ABC', 'itemB.defg', 'itemC.drug', 'itemD.ashok']
x = []
for (i, item) in enumerate(data):
a = (i, str(item).split('.'))
x.append(a)
for index, value in x:
print(index, value)
产量是
0 ['itemA', 'ABC']
1 ['itemB', 'defg']
2 ['itemC', 'drug']
3 ['itemD', 'ashok']
您可以使用指数方法:
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>。
它使用列表方法在这个问题的选择答案,但与列表理解,使它更快的代码较少。