【发布时间】:2021-12-23 03:39:56
【问题描述】:
我是 python 的初学者。我遇到了一个难题,我试图在 python 中创建一个搜索函数而不导入任何东西,在成功搜索后我希望它从主要列表中打印出那个次要列表。
在这个例子中: [['Darren', '19'], ['John', '17'], ['Dave', '16']] #['name', 'age']
输入要搜索的名称:Dave #Example input
找到了,Dave 16 #Expected Output
下面是我写的代码:
'''
定义搜索名称():
with open("namelist.txt") as f:
datafile = f.read()
found = False
name = input("Enter name: ")
while True:
if name in datafile:
print("found")
CleanData = []
#CleanData = [['Darren', '19'], ['John', '17'], ['Dave', '16']]
h = open("namelist.txt", "r")
for record in h:
stripped = record.strip()
recordlist = stripped.split("\t")
CleanData.append(recordlist)
h.close()
print(?) #Place where i'm stuck
found = True
return True
else:
print("User nor found")
break
'''
任何知道如何做到这一点的人请发表评论以解决这个问题????,我已经研究了我遇到的问题,但仍然没有解决......做得够多了,或者我一直在寻找错误的材料来学习
【问题讨论】:
标签: python list search multidimensional-array