【问题标题】:Searching a Parallel Array in Python在 Python 中搜索并行数组
【发布时间】:2011-12-17 09:00:06
【问题描述】:

这是一道家庭作业题,我已经掌握了基础知识,但我似乎找不到搜索两个并行数组的正确方法。

原问题:设计一个具有两个并行数组的程序:一个名为peopleString 数组以七个人的名字初始化,一个名为@987654324 的String 数组@ 用你朋友的电话号码初始化。该程序应允许用户输入人名(或人名的一部分)。然后它应该在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 的索引并将其应用于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."

【问题讨论】:

  • 你也许可以建立一个中间字典,def create_dict(list1,list2): d = {} for i in list1: d.insert(list1[i],list2[i]) return d 但像肖恩钦说的那样使用 zip 更有效

标签: python arrays parallel-arrays


【解决方案1】:

由于这是家庭作业,我不会直接给出代码。

您可以创建一个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]

姓名与子字符串匹配的人员列表:

This answer 应该会让你走上正轨。

当然,如果搜索返回一个空列表,则没有匹配的名称,您可以显示相应的消息。


替代建议

另一种方法是直接搜索列表并获取匹配索引,然后您可以使用该索引检索电话号码。

我会为您提供这个示例,并由您自己将其扩展为可行的解决方案。

>>> 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 作为查找表,另一种直接搜索列表。您的更新表明您正在尝试将两种解决方案混合在一起,这不是必需的。

如果您需要在所有名称中搜索子字符串匹配项,则最好使用第二种解决方案(直接搜索列表)。我提供的代码示例返回一个列表(因为可能有多个名称包含该子字符串),每个项目都是(index, name)tuple。您需要遍历列表并提取索引和名称。然后,您可以使用索引来检索电话号码。

为避免只为您提供解决方案,以下是相关示例:

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

【讨论】:

  • 可以使用声明try: people = dict[name] except KeyError: print "person not found"
  • 好的,我设法完成了。使用if name in lookup: 的搜索功能效果很好。但仅适用于列表中的全名。如何修改它以接受部分名称,例如“timmy”的“tim”?
  • @ThiagoM。查看我在“名称与子字符串匹配的人员列表”标题下提供的链接。这将搜索 dict 并返回包含子字符串的名称的键/值对。
  • @ShawnChin 看看我的更新,这就是我迄今为止能够得到的工作。我无法通过搜索返回的索引来获取相应的电话号码。
  • 用回复更新答案
【解决方案2】:

您可能想查看here 以获取How do I ... return the index of the persons name in the people array 的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多