S
S
Sergey Ilyin2020-04-24 22:52:08
Python
Sergey Ilyin, 2020-04-24 22:52:08

How to organize search by dictionary values?

We have a dictionary:

cities= {
        'г. Москва':
    ['Москва', 'Moscow', 'Moscow > Moskva', 'г. Москва', 'г.Москва', 'Moskva (Moscow)'],
         'г. Калиниград':
    ['Калининград', 'г.Калининград', 'Kaliningrad (Konigsberg)']
         }

for ease of access to cities:
cv = cities.values()
ck = cities.keys()


we have a list:
adreses_alforder = [('Moscow > Moskva, ул. xxx, д. xxx' , 'Moscow > Moskva, ул. xxx, д. xxx'), ('Kaliningrad (Konigsberg), ' , 'Kaliningrad (Konigsberg), ')]


writing:
for i in adreses_alforder:
    x = i[0].split(',') #взять первый элемент в каждом адресе и разделить адрес по запятым
    print(x[0])

everything is ok here, of course.
but if
for i in adreses_alforder:
    x = i[0].split(',') #беру первый элемент внутри i и разбиваю по запятой
    xr = x[0] #здесь я беру первый элемент внутри переменной Х - город
    if xr in cv():
        print(x)

then everything stops working ((

task: take a city (zero element in X), check it against the values ​​in the cities dictionary and return the corresponding key

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2020-04-24
@sunsexsurf

It?

cities= {
    'г. Москва': [
        'Москва', 'Moscow', 'Moscow > Moskva', 'г. Москва', 'г.Москва', 'Moskva (Moscow)'
    ],
    'г. Калиниград': [
        'Калининград', 'г.Калининград', 'Kaliningrad (Konigsberg)'
    ]
}

addresses_folder = [
    ('Moscow > Moskva, ул. xxx, д. xxx' , 'Moscow > Moskva, ул. xxx, д. xxx'), 
    ('Kaliningrad (Konigsberg), ' , 'Kaliningrad (Konigsberg), ')
]


for address in addresses_folder:
    address_city = address[0].split(',')[0]
    for city, values in cities.items():
        if address_city in values:
            print(city)

Output:
г. Москва
г. Калиниград

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question