people = ["James","COP","George","COP","Sam","Mac","Johnny","Karina"]
cops = [(idx+1) for idx, val in enumerate(people) if val == "COP"] #cops' positions
peoplePositions = [(x+1) for x in range(len(people))] #index positions
distances = []
for x in peoplePositions:
for y in cops:
distances.append(abs(x-y))
#the output would be "Johnny"
大家好!我正在研究这个问题,基本上我有一个包含一些人/物体的列表,它实际上是一个圆桌,即它的头部连接到它的尾部。现在我想获取“COP”和人之间的(索引位置)距离,然后输出与“COP”距离最大的人。如果它们都具有相同的距离,则输出必须是它们全部。
这是我的代码,最后我得到了所有人和“COP”之间的距离。我考虑过用一系列 len(peoplePositions) 和一系列 len(cops) 进行距离的嵌套循环,但没有任何进展。
Cats萌萌
相关分类