使用 Python 匹配用户输入列表中的相似项目?
Matching similar items in a list from user input with Python?
我是 Python 的新手,首先,如果标题不够具体,我想道歉,但我不知道其他表达方式。另外,如果这个问题是重复的,我们深表歉意。正如我所说,我真的不知道要寻找什么。无论如何,这就是我想要做的:
我有一个这样的列表:names = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
我希望用户能够输入名称,所以我将使用 name = input('Enter name:')
,这就是我卡住的地方。我希望用户能够输入像 shak
这样的字符串并让程序显示 1. William Shakespeare 2. Shakira
或者如果用户输入 ford
让程序显示 1. Tom Ford 2. Tim Ford
,如果用户变得更具体,例如:shakespeare
让程序只显示 1. William Shakespeare
。
我想这是一个正则表达式问题,但我发现正则表达式非常混乱。我试过看几个视频都无济于事。任何类型的帮助表示赞赏。提前致谢!
这对你有用吗?如果输出不是您想要的那样,我希望您稍后可以处理该部分。该逻辑找到每个适用的名称。
names = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
while True:
name = input('Enter name: ')
print([entry for entry in names if name in entry])
在我看来,正则表达式有点矫枉过正。您可以使用 for 循环遍历列表。然后使用 in
运算符,您可以检查输入的字符串是否在字符串(称为子字符串)内。我举一个简单的例子。
names = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
substring = input('Enter a substring: ')
for name in names:
if (substring in name):
print(name)
因为你说你是 python 的新手,所以我没有使用列表理解或任何复杂的东西。
你可以尝试这样的事情。
list = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
def get_matching_term(input, list):
matches = []
for word in list:
if input.lower() in word.lower():
matches.append(word)
return matches
thing_to_match = raw_input("Enter name to find:")
matches = get_matching_term(thing_to_match, list)
for idx, match in enumerate(matches):
print("Match #" + str((idx + 1)) + ": " + match)
然后就可以这样调用了。
python test.py
Enter name to find: shak
Match #1: william shakespeare
Match #2: shakira
正则表达式非常有用,但这个特定示例不需要正则表达式。在两个字符串上使用 string.lower() 会使这种情况不区分大小写。
i=0
For item in names:
if name.lower() in item.lower():
i++
print str(i) + '. ' + item
names = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
search = str(input("Please enter search term "))
ref = list(search.lower()) # this is your search text
out = []
count = 1
ind = 0
for i in names: # loops over your list
#print () #take the hash out to see what is i
for j in list(i.lower()): # list converts your variable into a list ie shakira = ['s','h'..]
if j == ref[ind]:
ind += 1
else:
ind = 0
if ind == len(ref): #ind is gonna go over each index in ref, if ind = the length of your
# search text then you have found a match
out.append(i)
ind = 0
break
if len(out) == 0:
out.append(" Nothing found ")
count = 0
for i in out:
print ("{}. {}".format(count,i), end = "\t\t")
count += 1
好的!有很多方法可以做到这一点,并试图使其尽可能简单。
缺陷:如果有两个相似的名字,例如Will和William,你搜索will,你会得到两个名字。如果你搜索 william,它只会给你 Willaim。此外,设计效率不高,两个 for 循环,基本上它 运行 与列表中的字母数(总字母数)成线性关系。看看结果如何。
dict_list = [{"First_Name": "Sam", "Last_Name": "John", "Age": 24},
{"First_Name": "Martin", "Last_Name": "Lukas", "Age": 34},
{"First_Name": "Jeff", "Last_Name": "Mark", "Age": 44},
{"First_Name": "Jones", "Last_Name": "alfred", "Age": 54}]
getdetails = input("Name: ")
for i in range(len(dict_list)):
if (getdetails in dict_list[i]["First_Name"]):
print(f'Details are {dict_list[i]}')
$$$$$$$ output $$$$$$
Name: Jeff
Details are {'First_Name': 'Jeff', 'Last_Name': 'Mark', 'Age': 44}
Process finished with exit code 0
我是 Python 的新手,首先,如果标题不够具体,我想道歉,但我不知道其他表达方式。另外,如果这个问题是重复的,我们深表歉意。正如我所说,我真的不知道要寻找什么。无论如何,这就是我想要做的:
我有一个这样的列表:names = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
我希望用户能够输入名称,所以我将使用 name = input('Enter name:')
,这就是我卡住的地方。我希望用户能够输入像 shak
这样的字符串并让程序显示 1. William Shakespeare 2. Shakira
或者如果用户输入 ford
让程序显示 1. Tom Ford 2. Tim Ford
,如果用户变得更具体,例如:shakespeare
让程序只显示 1. William Shakespeare
。
我想这是一个正则表达式问题,但我发现正则表达式非常混乱。我试过看几个视频都无济于事。任何类型的帮助表示赞赏。提前致谢!
这对你有用吗?如果输出不是您想要的那样,我希望您稍后可以处理该部分。该逻辑找到每个适用的名称。
names = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
while True:
name = input('Enter name: ')
print([entry for entry in names if name in entry])
在我看来,正则表达式有点矫枉过正。您可以使用 for 循环遍历列表。然后使用 in
运算符,您可以检查输入的字符串是否在字符串(称为子字符串)内。我举一个简单的例子。
names = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
substring = input('Enter a substring: ')
for name in names:
if (substring in name):
print(name)
因为你说你是 python 的新手,所以我没有使用列表理解或任何复杂的东西。
你可以尝试这样的事情。
list = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
def get_matching_term(input, list):
matches = []
for word in list:
if input.lower() in word.lower():
matches.append(word)
return matches
thing_to_match = raw_input("Enter name to find:")
matches = get_matching_term(thing_to_match, list)
for idx, match in enumerate(matches):
print("Match #" + str((idx + 1)) + ": " + match)
然后就可以这样调用了。
python test.py
Enter name to find: shak
Match #1: william shakespeare
Match #2: shakira
正则表达式非常有用,但这个特定示例不需要正则表达式。在两个字符串上使用 string.lower() 会使这种情况不区分大小写。
i=0
For item in names:
if name.lower() in item.lower():
i++
print str(i) + '. ' + item
names = ['william shakespeare', 'shakira', 'tom ford', 'tim ford']
search = str(input("Please enter search term "))
ref = list(search.lower()) # this is your search text
out = []
count = 1
ind = 0
for i in names: # loops over your list
#print () #take the hash out to see what is i
for j in list(i.lower()): # list converts your variable into a list ie shakira = ['s','h'..]
if j == ref[ind]:
ind += 1
else:
ind = 0
if ind == len(ref): #ind is gonna go over each index in ref, if ind = the length of your
# search text then you have found a match
out.append(i)
ind = 0
break
if len(out) == 0:
out.append(" Nothing found ")
count = 0
for i in out:
print ("{}. {}".format(count,i), end = "\t\t")
count += 1
好的!有很多方法可以做到这一点,并试图使其尽可能简单。 缺陷:如果有两个相似的名字,例如Will和William,你搜索will,你会得到两个名字。如果你搜索 william,它只会给你 Willaim。此外,设计效率不高,两个 for 循环,基本上它 运行 与列表中的字母数(总字母数)成线性关系。看看结果如何。
dict_list = [{"First_Name": "Sam", "Last_Name": "John", "Age": 24},
{"First_Name": "Martin", "Last_Name": "Lukas", "Age": 34},
{"First_Name": "Jeff", "Last_Name": "Mark", "Age": 44},
{"First_Name": "Jones", "Last_Name": "alfred", "Age": 54}]
getdetails = input("Name: ")
for i in range(len(dict_list)):
if (getdetails in dict_list[i]["First_Name"]):
print(f'Details are {dict_list[i]}')
$$$$$$$ output $$$$$$
Name: Jeff
Details are {'First_Name': 'Jeff', 'Last_Name': 'Mark', 'Age': 44}
Process finished with exit code 0