python - 如何打印列表中字符串之间的距离

标签 python python-3.x

我想打印列表中字符串之间的距离。

例如,如果我的列表包含如下内容,我想知道从 DeerCrazy 的距离,但是 Deer 可以是列表中的任何位置,Crazy 可以位于列表中 Deer 之前或之后。

['black', 'white', 'Deer', 'Blue', 'More', 'Crazy']

最佳答案

使用list.index()方法:

>>> my_list = ['black', 'white', 'Deer', 'Blue', 'More', 'Crazy']
>>> abs(my_list.index('Deer') - my_list.index('Crazy'))
3

关于python - 如何打印列表中字符串之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719825/

相关文章:

NLTK 中的 Python 索引命令

python - 为什么 createDataFrame 的推断程序不将此数据创建为字符串的列?

python - 为什么纹理索引 31 在 Moderngl 中被覆盖?

python - 使用 PyGame 中的操纵杆模块

python - 具有多个属性级别的类 [初学者]

python - Django Admin 和 ImageField 尺寸限制

python - 在python中将001格式的数字添加到文件名中

python - 如何取消最后调用的方法?

Python - 使用重新采样而不使用平均值/均值进行下采样

python - Pandas Groupby : 'observed' parameter with multiple categoricals