在Python中搜索并行数组

3 投票
2 回答
6752 浏览
提问于 2025-04-17 05:11

这是一个作业问题,我掌握了基本知识,但似乎找不到正确的方法来搜索两个平行数组。

原问题:设计一个程序,包含两个平行数组:一个名为 peopleString 数组,里面存放七个人的名字;另一个名为 phoneNumbersString 数组,里面存放你朋友的电话号码。这个程序应该允许用户输入一个人的名字(或者部分名字)。然后,它应该在 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"]

现在,我的问题就出在这里。我该如何对名字进行搜索(或部分搜索),并返回这个人在 people 数组中的索引,然后相应地打印电话号码呢?

更新:我在代码底部添加了这个内容来进行搜索。

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 并将其应用到 print phoneNumbers[搜索返回的索引] 中呢?

更新 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."

2 个回答

0

你可以在这里找到关于如何返回人名在人员数组中的索引的问题答案。

6

因为这是作业,我就不直接给你代码了。

你可以创建一个 dict,它可以作为查找表,名字是键,电话号码是值。

创建查找表:

你可以很简单地把两个平行的数组转换成一个字典,使用 dict()zip()。大概是这样的:

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')]

如果在过程中遇到问题,分享你做的事情,我们会很乐意帮助你。

祝你好运,玩得开心。


对更新问题的回应

注意,我提供了两种替代解决方案,一种是使用字典作为查找表,另一种是直接搜索列表。你的更新表明你试图把这两种方案混合在一起,但其实没必要。

如果你需要在所有名字中查找包含子串的匹配项,第二种方案(直接搜索列表)可能更合适。我提供的代码示例返回一个列表(因为可能有多个名字包含那个子串),每个项目都是一个 元组,格式是 (index, name)。你需要遍历这个列表,提取索引和名字。然后你可以用这个索引来获取电话号码。

为了避免直接给你解决方案,这里有个相关的例子:

>>> 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

撰写回答