我应该创建一个以列表和标题为字符串的函数,然后从基于标题的列表。
def find_appointment(lst, title = ""):
if title in lst:
funn = lst.find(title)
return funn
else:
print("No result")
appointments = ["Zoo: 11.03.22", "Shopping: 13.08.22", "Christmas: 24.12.22", "Funeral: 25.12.22"]
find_appointment(appointments, "Zoo")
我希望得到";动物园:11.03.22〃;,而是得到了";没有结果";
这里的清单只是我随便编的。在实际列表中,我不知道项目。
这是我的解决方案:
def find_appointment(lst, title = ""):
for i in lst:
if title in i:
return index
else:
print("No result")
appointments = ["Zoo: 11.03.22", "Shopping: 13.08.22", "Christmas: 24.12.22", "Funeral: 25.12.22"]
print(find_appointment(appointments, "Zoo"))
您需要使用的是字典。就像:
appointments = {"Zoo":"11.03.22", "Shopping": "13.08.22"}
something_to_find = "Zoo"
然后在字典里找到一些东西:
def find_something(lst, title=" "):
print(lst[title])
find_something(appointments,something_to_find)
所以,是的,你可能想读字典。W3schools是一个很好的起点[https://www.w3schools.com/python/python_dictionaries.asp]
def find_appointment(lst, title = ""):
for i in lst:
if title in i:
a=title.find(title)
return a
else:
print("No result")
appointments = ["Zoo: 11.03.22", "Shopping: 13.08.22", "Christmas: 24.12.22",
"Funeral: 25.12.22"]
print(find_appointment(appointments, "Zoo"))
#hope this helps. in order to iterate through all elements in list. we have to
use a loop. once the value is found we can get its index by find method of
string.