在Python中搜索并行数组
这是一个家庭作业问题,我记下了基础知识,但我似乎找不到搜索两个并行阵列的正确方法 原始问题:设计一个具有两个并行数组的程序:一个名为在Python中搜索并行数组,python,arrays,parallel-arrays,Python,Arrays,Parallel Arrays,这是一个家庭作业问题,我记下了基础知识,但我似乎找不到搜索两个并行阵列的正确方法 原始问题:设计一个具有两个并行数组的程序:一个名为人的字符串数组,用七个人的名字初始化;一个名为电话号码的字符串数组,用朋友的电话号码初始化。该程序应允许用户输入一个人的姓名(或姓名的一部分)。然后,它应该在people数组中搜索该人员。如果找到此人,它应该从phoneNumbers数组中获取此人的电话号码并显示出来。如果找不到此人,程序应显示一条消息,指示是否找到此人 我的当前代码: # create main
人的字符串
数组,用七个人的名字初始化;一个名为电话号码
的字符串
数组,用朋友的电话号码初始化。该程序应允许用户输入一个人的姓名(或姓名的一部分)。然后,它应该在people
数组中搜索该人员。如果找到此人,它应该从phoneNumbers
数组中获取此人的电话号码并显示出来。如果找不到此人,程序应显示一条消息,指示是否找到此人
我的当前代码:
# create main
def main():
# take in name or part of persons name
person = raw_input("Who are you looking for? \n> ")
# convert string to all lowercase for easier searching
person = person.lower()
# run people search with the "person" as the parameters
peopleSearch(person)
# create module to search the people list
def peopleSearch(person):
# create list with the names of the people
people = ["john",
"tom",
"buddy",
"bob",
"sam",
"timmy",
"ames"]
# create list with the phone numbers, indexes are corresponding with the names
# people[0] is phoneNumbers[0] etc.
phoneNumbers = ["5503942",
"9543029",
"5438439",
"5403922",
"8764532",
"8659392",
"9203940"]
现在,我的问题就从这里开始。如何对姓名进行搜索(或部分搜索),并在人员数组中返回人员姓名的索引,并相应地打印电话号码
更新:为了进行搜索,我将此添加到代码底部
lookup = dict(zip(people, phoneNumbers))
if person in lookup:
print "Name: ", person ," \nPhone:", lookup[person]
但这只适用于完全匹配,我尝试使用它来获得部分匹配
[x for x in enumerate(people) if person in x[1]]
但是当我在'tim'
上搜索它时,它返回[(5,'timmy')]
。如何获取5
的索引并将其应用于打印电话号码[
搜索返回的索引]
更新2:终于让它完美地工作了。使用此代码:
# conduct a search for the person in the people list
search = [x for x in enumerate(people) if person in x[1]]
# for each person that matches the "search", print the name and phone
for index, person in search:
# print name and phone of each person that matches search
print "Name: ", person , "\nPhone: ", phoneNumbers[index]
# if there is nothing that matches the search
if not search:
# display message saying no matches
print "No matches."
因为这是家庭作业,所以我不会直接给出代码
您可以创建一个dict
,作为一个查找表,以姓名为键,电话号码为值
创建查找表:
您可以使用and轻松地将并行数组转换为dict。大致如下:
lookup = dict(zip(people, phoneNumbers))
要了解其工作原理,请查看以下示例:
>>> people = ["john", "jacob", "bob"]
>>> phoneNumbers = ["5503942", "8659392", "8659392"]
>>> zip(people, phoneNumbers)
[('john', '5503942'), ('jacob', '8659392'), ('bob', '8659392')]
>>> dict(zip(people, phoneNumbers))
{'jacob': '8659392', 'bob': '8659392', 'john': '5503942'}
查找某人是否存在:
您可以使用以下方法快速确定查找表中是否存在人员(键):
if name in lookup:
# ... phone number will be lookup[name]
姓名与子字符串匹配的人员列表:
应该会让你走上正轨
当然,如果搜索返回空列表,则没有匹配的名称,您可以显示相应的消息
替代建议
另一种方法是直接搜索列表并获取匹配项的索引,然后使用该索引检索电话号码
我将向您提供这个示例,并让您将其扩展为一个可行的解决方案
>>> people = ["john", "jacob", "bob", "jacklyn", "cojack", "samantha"]
>>> [x for x in enumerate(people) if "jac" in x[1]]
[(1, 'jacob'), (3, 'jacklyn'), (4, 'cojack')]
如果你在路上遇到了困难,请分享你所做的事情,我们很乐意为你提供帮助
祝你好运,玩得开心
回应最新问题
请注意,我提供了两种备选解决方案,一种使用dict作为查找表,另一种直接搜索列表。您的更新表明您正在尝试将这两种解决方案混合在一起,这是不必要的
如果需要在所有名称中搜索子字符串匹配项,最好使用第二种解决方案(直接搜索列表)。我提供的代码示例返回一个列表(因为可能有多个名称包含该子字符串),每个项都是(索引,名称)
。您需要遍历列表并提取索引和名称。然后,您可以使用索引检索电话号码
为避免仅给出解决方案,以下是相关示例:
>>> people = ["john", "jacob", "bob", "jacklyn", "cojack", "samantha"]
>>> matches = [x for x in enumerate(people) if "jac" in x[1]]
>>> for index, name in matches:
... print index, name
...
1 jacob
3 jacklyn
4 cojack
>>> matches = [x for x in enumerate(people) if "doesnotexist" in x[1]]
>>> if not matches:
... print "no matches"
...
no matches
您可能想寻找如何。。。返回人员数组中人员姓名的索引
您可以构建一个中间字典,defcreate_dict(list1,list2):d={}用于列表1中的i:d.insert(list1[i],list2[i])返回d
,但更有效的是使用类似zip的Shawn Chin saidit语句可以使用该语句try:people=dict[name],除了KeyError:print“person not found”
好的,我设法完成了。在查找中使用if name:
的搜索功能非常有效。但只限于名单上的全名。我如何修改它以接受部分名称,例如“tim”代表“timmy”?@ThiagoM。查看我在“姓名与子字符串匹配的人员列表”标题下提供的链接。这将搜索dict并返回包含子字符串的键/值对名称。@ShawnChin请看我的更新,这是我迄今为止能够得到的结果。我无法获取搜索返回的索引以获取相应的电话号码。请使用响应更新答案