我如何访问指数,同时在一个序列上以一个为旋转的序列?
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
当前回答
您可以使用此代码:
ints = [8, 23, 45, 12, 78]
index = 0
for value in (ints):
index +=1
print index, value
使用此代码,如果您需要在路径结束时重新设置指数值:
ints = [8, 23, 45, 12, 78]
index = 0
for value in (ints):
index +=1
print index, value
if index >= len(ints)-1:
index = 0
其他回答
一个具有“对手”变量的圈子设置为一个启动器,这将是一个参数,在格式化行,作为项目号。
当我们通过“i”访问列表时,“i”被格式化为项目价格(或它是什么)。
listos = [8, 23, 45, 12, 78]
counter = 1
for i in listos:
print('Item #{} = {}'.format(counter, i))
counter += 1
出口:
Item #1 = 8
Item #2 = 23
Item #3 = 45
Item #4 = 12
Item #5 = 78
首先,指数将从 0 到 4. 编程语言开始从 0 计算; 不要忘记,或者你会遇到一个 index-out-of-bounds 例外。
for x in range(0, 5):
请记住,我写了0到5因为旋转停止一个数字之前的最高。
要获得指数的值,使用
list[index]
如果列表中没有双重值:
for i in ints:
indx = ints.index(i)
print(i, indx)
根据此讨论:对象列表指数
Loop 对抗 iteration
在指标上旋转的当前字符串使用内置范围功能:
for i in range(len(sequence)):
# Work with index i
超越两个元素和指标可以通过旧的字符或使用新的内置Zip功能实现:
for i in range(len(sequence)):
e = sequence[i]
# Work with index i and element e
或
for i, e in zip(range(len(sequence)), sequence):
# Work with index i and element e
通过PEP 212 - Loop Counter Iteration。
这里是如何使用 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