【问题标题】:Python-check what position it is in a list [duplicate]Python-检查它在列表中的位置[重复]
【发布时间】:2016-05-10 06:53:04
【问题描述】:
list1 = ['physics', 'chemistry', "dog", "cat","dog"];

我想让它说,如果我输入了一个 dog,它会告诉我 dog 是第四个和第六个。

【问题讨论】:

  • 我已将欺骗目标换成了更合适的问题。

标签: python list


【解决方案1】:

使用列表推导:

list1 = ['physics', 'chemistry', "dog", "cat","dog"]

string = input()
res = [i for i, el in enumerate(list1) if el == string]

print(res)

输出:

[2, 4]

【讨论】:

  • 感谢您将我的课程作业保存为 20 分钟的实验时间
  • 作为参考这是如何工作的,所以我可以使用我搜集的任何信息以供将来参考
  • @jcdenton 检查此docs.python.org/2/tutorial/…
猜你喜欢
  • 1970-01-01
  • 2018-02-12
  • 2020-01-04
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2018-06-26
相关资源
最近更新 更多