Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

python 3.x - Searching dictionary by record

I can't manage to find people in dictionary if by more than one record.

For example: display people by age+sex+city if given input age, sex, city or sex+city if given input sex, city.

people={'1': {'name': 'John', 'surname': 'Mclachan', 'age': '27', 'city': 'London', 'sex': 'Male', 'married': 'Yes', 'phoneNo': '00001'}, '2': {'name': 'Marie', 'surname': 'Rose', 'age': '22', 'city': 'London', 'sex': 'Female', 'married': 'No', 'phoneNo': '00002'}, '3': {'name': 'Luna', 'surname': 'Stallone', 'age': '24', 'city': 'Edinburgh', 'sex': 'Female', 'married': 'No', 'phoneNo': '00003'}, '4': {'name': 'Peter', 'surname': 'Griffin', 'age': '29', 'city': 'Edinburgh', 'sex': 'Male', 'married': 'Yes', 'phoneNo': '00004'}}

def find_people():
  
    city = input('Input city >> ').title()
    surname = input('surname >> ').title()
    name_p = input('name >> ').title()
    sex = input('sex >> ').title()
    age = input('age >> ').title()
    if any(record["name"] == name_p or record['city'] == city or record['sex'] == sex or record['age'] == age or record['surname']== surname for record in people.values()):
        for x in people:
            if people[x]['name'] == name_p and people[x]['city'] == city or people[x]['name'] == name_p or people[x]['city'] == city or people[x]['sex'] == sex
                    or people[x]['city'] == city and people[x]['sex'] == sex and people[x]['age'] == age:
            #if people[x]['name'] == name_p and people[x]['city'] == city:
                print('
 Person_ID:', x)
                print('-------------')
                for key, value in people[x].items():
                    print(key+':', value)
question from:https://stackoverflow.com/questions/65839551/searching-dictionary-by-record

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your description is quite confusing, so I've tried to put the minimum change of code to just find a matching record, and you can try to play with the following simplified code and modify for your own required display format:

Basically it works to search any matching by surname+ first_name+ city and display the record.

def find_people():
  
    city = input('Input city >> ').title()
    surname = input('surname >> ').title()
    first_name = input('name >> ').title()
    sex = input('sex >> ').title()
    age = input('age >> ')         # don't need .title()

    records = people.values()
    #print(records)

    # search for a good matching record - by Surname+ Name + city

    for record in records:
        if (record['name'] == first_name and record['surname'] == surname and record['city'] == city):
            print(record)
        """
            #if people[x]['name'] == name_p and people[x]['city'] == city:
                #
                print('
 Person_ID:', p)
                print('-------------')
                for key, value in people[p].items():
                    print(key +':', value)
        """

if __name__ == '__main__':

    find_people()

    print('Done')

Output:

Input city >> Edinburgh
surname >> Stallone
name >> Luna
sex >> Male
age >> 24
{'name': 'Luna', 'surname': 'Stallone', 'age': '24', 'city': 'Edinburgh', 'sex': 'Female', 'married': 'No', 'phoneNo': '00003'}
Done    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...