Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6762277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:18:04+00:00 2026-05-26T14:18:04+00:00

This is a homework question, I got the basics down, but I can’t seem

  • 0

This is a homework question, I got the basics down, but I can’t seem to find the correct method of searching two parallel arrays.

Original Question: Design a program that has two parallel arrays: a String array named people that is initialized with the names of seven people, and a String array named phoneNumbers that is initialized with your friends’ phone numbers. The program should allow the user to enter a person’s name (or part of a person’s name). It should then search for that person in the people array. If the person is found, it should get that person’s phone number from the phoneNumbers array and display it. If the person is not found, program should display a message indicating so.

My current code:

# 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"]

Now, my entire problem begins here. How do I conduct a search (or partial search) on a name, and return the index of the persons name in the people array and print the phone number accordingly?

Update: I added this to the bottom of the code in order to conduct the search.

lookup = dict(zip(people, phoneNumbers))
if person in lookup:
    print "Name: ", person ," \nPhone:", lookup[person]

But this only works for full matches, I tried using this to get a partial match.

[x for x in enumerate(people) if person in x[1]]

But when I search it on 'tim' for example, it returns [(5, 'timmy')]. How do I get that index of 5 and apply it in print phoneNumbers[the index returned from the search]?

Update 2: Finally got it to work perfectly. Used this code:

# 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."
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:18:04+00:00Added an answer on May 26, 2026 at 2:18 pm

    Since this is homework, I’ll refrain from giving the code outright.

    You can create a dict that works as a lookup table with the name as the key and the phone number as its value.

    Creating the lookup table:

    You can easily convert the parallel arrays into a dict using dict() and zip(). Something along the lines of:

    lookup = dict(zip(people, phoneNumbers))
    

    To see how that works, have a look at this example:

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

    Finding if a person exist:

    You can quickly figure out if a person (key) exist in the lookup table using:

    if name in lookup:
        # ... phone number will be lookup[name]
    

    List of people whose name matches substring:

    This answer should put you on the right track.

    And of course, if the search returns an empty list there are no matching names and you can display an appropriate message.


    Alternative suggestion

    Another approach is to search the list directly and obtain the index of matches which you can then use to retrieve the phone number.

    I’ll offer you this example and leave it up to you to expand it into a viable solution.

    >>> people = ["john", "jacob", "bob", "jacklyn", "cojack", "samantha"]
    >>> [x for x in enumerate(people) if "jac" in x[1]] 
    [(1, 'jacob'), (3, 'jacklyn'), (4, 'cojack')]
    

    If you hit a snag along the way, share what you’ve done and we’ll be glad to assist.

    Good luck, and have fun.


    Response to updated question

    Note that I’ve provided two alternative solutions, one using a dict as a lookup table and another searching the list directly. Your updates indicate you’re trying to mix both solutions together, which is not necessary.

    If you need to search through all the names for substring matches, you might be better off with the second solution (searching the listdirectly). The code example I provided returns a list (since there may be more than one name that contain that substring), with each item being a tuple of (index, name). You’ll need to iterate throught the list and extract the index and name. You can then use the index to retrieve the phone number.

    To avoid just giving you the solution, here’s related example:

    >>> 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this question for homework, and all I can think of is to
This is a homework question, but as you can see I've written most of
This is part of a homework assignment. I've got several questions asking find the
Disclaimer: This is for a homework assignment, but the question is not regarding the
Ok this is a homework questions, but I cannot find the answer anywhere, not
I'm stuck with this Java homework question: Write a new method, Refund, that simulates
This is a homework question I am sorry but I am lost. What happens
This is a homework question, binary search has already been introduced: Given two arrays,
This is my homework question: Expand on your Circle class by adding a method
I got this question for homework, and I'm not sure how to answer it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.