如何查看目录/列表并获得位置和bool

2024-10-03 15:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道,当有人进来时,是否有人进来,比如说“070718604545”。它会查找它,如果它在那里,它会打印列表中的其他内容,如下例

Patt = [
                {'Phone': "0718604545", 'Name': "Tom", 'Age': '2007'}
                {'Phone': "0718123567", 'Name': "Katy", 'Age': '1998'}
                {'Phone': "0718604578", 'Name': "BillyW", 'Age': '1970'}
                 ....
                 ....
                 ....
                 ....
                {'Phone': "0714565778", 'Name': "Sony", 'Age': '1973'}
            ]

他将以exmaple的身份输入“0718604545”

x = input("Enter Phone")
Search for x in Patt[Phone]:
   name = Patt[Name] where Phone = x
   print(name)

所以答案应该是汤姆

谢谢


Tags: name内容列表inputagephone身份enter
3条回答

您可以通过迭代列表patt获得号码,然后访问patt项目中的每个电话键,并使用==运算符与您要查找的电话号码进行比较。下面的函数完成这项工作

def answer(search):
    for data in patt: # iterate over each item in patt
        if data["Phone"] == search: # compare the current value if it is the searched one
            return data["Name"] # return the name in the dictionary when the number is found
    return None # if it is not found, return None
print(answer('0718604545'))

下面的方法应该有效。对于每个项目,检查“phone”键是否具有与x匹配的值。如果是,则返回“name”键的值

x = input("Enter Phone")
for item in Patt:
  if item["Phone"] == x:
    print(item["Name"])

在准备答案时,我并没有注意到有一个正确的答案已经发布并被接受。尽管如此,我还是在下面发布了我的答案,增加了持续提问的功能,并且能够退出程序

Patt = [
    {'Phone': "0718604545", 'Name': "Tom", 'Age': '2007'},
    {'Phone': "0718123567", 'Name': "Katy", 'Age': '1998'},
    {'Phone': "0718604578", 'Name': "BillyW", 'Age': '1970'},
    {'Phone': "0714565778", 'Name': "Sony", 'Age': '1973'}
]

print(Patt)


def search_phone_records(user_phone):
    for record in Patt:  # Iterate over all the phone records in the dictionary
        if user_phone == record['Phone']:  # stop if found phone number in the dictionary
            return record['Name']  # Return user's name from the phone record
    return None


while True:
    user_input = input("Enter Phone or press 'x' to exit: ")

    if user_input in ('x', 'X'):
        print("Have a nice day!!! Thank you using our service!!!")
        break  # End the programme

    # Search for the phone number
    user_name = search_phone_records(user_input)

    #print("[{0}]".format(user_name))
    if type(user_name) == type(None):  # Phone number is not found
        print("Oops!!! Entered phone number ({0}) is not found in the dictionary!!!".format(user_input))
    else:  # Phone number is found
        print("Entered phone number ({0}) is found in the dictionary!!!".format(user_input))
        print("It is {1}'s phone number.".format(user_input, user_name))

另一个使用字典理解的解决方案:

Patt = [
    {'Phone': "0718604545", 'Name': "Tom", 'Age': '2007'},
    {'Phone': "0718123567", 'Name': "Katy", 'Age': '1998'},
    {'Phone': "0718604578", 'Name': "BillyW", 'Age': '1970'},
    {'Phone': "0714565778", 'Name': "Sony", 'Age': '1973'}
]

print(Patt)


def search_phone_records_using_dictionary_comprehension(user_phone):
    return {'Name': record['Name'] for record in Patt if user_phone == record['Phone']}


while True:
    user_input = input("Enter Phone or press 'x' to exit: ")

    if user_input in ('x', 'X'):
        print("Have a nice day!!! Thank you using our service!!!")
        break  # End the programme

    result = search_phone_records_using_dictionary_comprehension(user_input)
    print("result = {0}".format(result))

    if len(result) == 0:   # Phone number is not found
        print("Oops!!! Entered phone number ({0}) is not found in the dictionary!!!".format(user_input))
    else:  # Phone number is found
        print("Entered phone number ({0}) is found in the dictionary!!!".format(user_input))
        print("It is {1}'s phone number.".format(user_input, result['Name']))

相关问题 更多 >