Python 使用def无法搜索列表中的元素
有人能帮我解决这个问题吗?我只是想做一个简单的程序,但我被卡住了,现在我很沮丧Python 使用def无法搜索列表中的元素,python,python-3.x,list,Python,Python 3.x,List,有人能帮我解决这个问题吗?我只是想做一个简单的程序,但我被卡住了,现在我很沮丧 from tabulate import tabulate myList = [] def addnew(): name = [] name.append(input("Enter name: ")) myList.append(name) print(myList) def find(): global myList src = input
from tabulate import tabulate
myList = []
def addnew():
name = []
name.append(input("Enter name: "))
myList.append(name)
print(myList)
def find():
global myList
src = input("Enter name: ")
if src in myList:
print("Yes, name exist")
elif src not in myList:
print("Name not exist")
def view():
print(tabulate(myList, tablefmt='psql'))
while True:
print("[1] to add")
print("[2] to search")
print("[3] to view")
inp = input(">> ")
if inp == '1':
addnew()
elif inp == '2':
find()
elif inp == '3':
view()
else:
print("Wrong input")
我的def find():
未按预期工作:(
根据我的发现,如果像这样的列表['bob']
结果是好的,但是当像这样的列表[['bob']]
结果名称不存在时
如果我删除name=[]
并更改myList.append(input())
表格结果太乱了:)
结果:
Enter name: bob
['bob']
[1] to add
[2] to search
[3] to view
>> 3
+---+---+---+
| b | o | b |
+---+---+---+
[1] to add
[2] to search
[3] to view
>>
此代码将name
作为一个列表。当您输入“bob”时,name
类似于['bob']
。然后将name
应用到myList
,这样myList
将是一个类似[['bob']]
的列表。在[['bob']
中找不到'bob'
所以你不必把name
列成一个列表。只需将这两行替换为:
name = input("Enter name: ")
输入新名称时,只需将字符串添加到
myList
。然后,当您使用tablate()
时,从此myList
创建一列数据:
from tabulate import tabulate
myList = []
def addnew():
myList.append(input("Enter name: ")) # <-- just append new string to myList
print(myList)
def find():
global myList
src = input("Enter name: ")
if src in myList:
print("Yes, name exist")
elif src not in myList:
print("Name not exist")
def view():
print(tabulate([[v] for v in myList], tablefmt='psql')) # <-- create one column data from myList
# or you can use:
# print(tabulate({'name': myList}, tablefmt='psql'))
while True:
print("[1] to add")
print("[2] to search")
print("[3] to view")
inp = input(">> ")
if inp == '1':
addnew()
elif inp == '2':
find()
elif inp == '3':
view()
else:
print("Wrong input")
myList=[['bob']]
是一个列表列表,它是另一回事。您需要在myList中编写['bob']才能找到它……我认为您不需要名称
列表,只需要myList.append(输入(“输入名称”)
@scarLópez我已经更改为myList=['bob']
,但表格结果不太好看。您可以稍后格式化表格,以使其看起来更漂亮;更重要的是程序运行正确,而不是它漂亮。@ÓscarLópez hihi。好的,好的
name = input("Enter name: ")
from tabulate import tabulate
myList = []
def addnew():
myList.append(input("Enter name: ")) # <-- just append new string to myList
print(myList)
def find():
global myList
src = input("Enter name: ")
if src in myList:
print("Yes, name exist")
elif src not in myList:
print("Name not exist")
def view():
print(tabulate([[v] for v in myList], tablefmt='psql')) # <-- create one column data from myList
# or you can use:
# print(tabulate({'name': myList}, tablefmt='psql'))
while True:
print("[1] to add")
print("[2] to search")
print("[3] to view")
inp = input(">> ")
if inp == '1':
addnew()
elif inp == '2':
find()
elif inp == '3':
view()
else:
print("Wrong input")
[1] to add
[2] to search
[3] to view
>> 1
Enter name: Andrej
['Andrej']
[1] to add
[2] to search
[3] to view
>> 1
Enter name: John
['Andrej', 'John']
[1] to add
[2] to search
[3] to view
>> 1
Enter name: Mary
['Andrej', 'John', 'Mary']
[1] to add
[2] to search
[3] to view
>> 3
+--------+
| Andrej |
| John |
| Mary |
+--------+
[1] to add
[2] to search
[3] to view
>> 2
Enter name: John
Yes, name exist
[1] to add
[2] to search
[3] to view