python - 在Python中查找循环列表中点之间的前向距离
问题描述
我将使用的一个示例圆形列表是[0, 1, 2, 3, 4, 5, 6, 7]
,类似于棋盘游戏中的正方形,其中正方形 0 跟随正方形 7。我试图通过仅向前移动而不是最小距离来找到任意两点之间的距离. 例如,0 和 7 之间的距离为 7,但 7 和 0 之间的距离为 1。7 和 7 之间的距离为 0,依此类推。我曾尝试使用迭代器中的循环生成器,但无法提出解决方案,因为我不明白如何解决这个问题。我真的很感激任何帮助,非常感谢:)!
解决方案
尝试这样的事情......
numbers = [0, 1, 2, 3, 4, 5, 6, 7]
def fxn(point_a, point_b):
count = 0
position = numbers.index(point_a)
while numbers[position] != point_b:
if position + 1 == len(numbers):
position = 0
count += 1
else:
position += 1
count += 1
return count
推荐阅读
- python - ctypes:如何正确更新指针?
- javascript - 时刻时区 - 无效日期
- function - 使用 Keras 自定义损失函数来惩罚更多的负面预测
- python - 在 C 中调用从 Python 接收指向函数的指针的函数
- powershell - 删除 HTML 中的区域
- ruby-on-rails - Ransack Rails - 数组中的一个参数
- javascript - 如何循环遍历vuejs中的嵌套数组
- python - 如何替换所有数字,除了python中的特定数字?
- php - PHP 在按钮单击时执行 Python 脚本
- django - Django restful API - 使用令牌获取用户 ID