如何访问索引本身以获取如下列表?
ints = [8, 23, 45, 12, 78]
for i in ints:
print('item #{} = {}'.format(???, i))
Run Code Online (Sandbox Code Playgroud)
当我使用循环遍历它时for,如何访问循环索引,在这种情况下从1到5?
我已经检查了一些想法和原因,下面针对这个问题进行了调查...... "太多值解包"异常 (Stefano Borini的解释)
但是在这里,我正在迭代列表作为理解列表并将结果移动到列表中......!所以输入的数量读取输出变量的数量,即tempList......
然后,这个过程出了什么问题?!
def DoProcess(self, myList):
tempList = []
tempList = [[x,y,False] for [x,y] in myList]
return tempList
Run Code Online (Sandbox Code Playgroud)
编辑1:myList是列表,就像[[x1, y1], [x2, y2], [x3, y3], [x4 y4]].
class Agent(object):
def __init__(self, point = None):
self.locationX = point.x
self.locationY = point.y
def __iter__(self):
return self
def __next__(self):
return [self.locationX, self.locationY]
def __getItem__(self):
return [self.locationX, self.locationY]
def GenerateAgents(self, numberOfAgents):
agentList = []
while len(agentList) < numberOfAgents:
point = Point.Point()
point.x = random.randint(0, 99)
point.y …Run Code Online (Sandbox Code Playgroud)