Pythonda dict bilan ichki o'tgan ro'yxatdagi qiymatlarni almashtiringmi?

Men ichki ro'yxatim bor:

my_list = ['a','b','c','dog', ['pig','cat'], 'd']

va dict:

my_dict = {'dog':'c','pig':'a','cat':'d'}

Dictdan foydalanishni istayman, men ro'yxatni olaman:

new_list = ['a', 'b', 'c', 'c', ['a', 'd'], 'd']

Men shunga o'xshash narsani sinab ko'rdim:

new_list = []
for idx1, item1 in enumerate(my_list):
    for idx2, item2 in enumerate(item1):
        new_list[idx1][idx2] = my_dict[item2]

lekin dict kodida item2 yo'q bo'lganda xatoga yo'l qo'yaman. Buning uchun yaxshiroq yo'l bormi?

1

7 javoblar

Natijani yaratish uchun ro'yxatni tushunishni ishlatadigan oddiy rekursiv funktsiyani yozing. Ro'yxatdagi element ro'yxat bo'lsa, u holda uni qaytaring, aks holda dict .get elementni aylantirish uchun standart parametr bilan:

my_list = ['a','b','c','dog', ['pig','cat'], 'd']
my_dict = {'dog':'c','pig':'a','cat':'d'}

def convert(l, d):
    return [convert(x, d) if isinstance(x, list) else d.get(x, x) for x in l]

print(convert(my_list, my_dict))

Chiqish:

['a', 'b', 'c', 'c', ['a', 'd'], 'd']
5
qo'shib qo'ydi

Natijani yaratish uchun ro'yxatni tushunishni ishlatadigan oddiy rekursiv funktsiyani yozing. Ro'yxatdagi element ro'yxat bo'lsa, u holda uni qaytaring, aks holda dict .get elementni aylantirish uchun standart parametr bilan:

my_list = ['a','b','c','dog', ['pig','cat'], 'd']
my_dict = {'dog':'c','pig':'a','cat':'d'}

def convert(l, d):
    return [convert(x, d) if isinstance(x, list) else d.get(x, x) for x in l]

print(convert(my_list, my_dict))

Chiqish:

['a', 'b', 'c', 'c', ['a', 'd'], 'd']
5
qo'shib qo'ydi

Siz ichki qismga almashtirishga harakat qilyapsiz. Bu funksiyani sinchkovlik bilan siz uchun qadr-qimmatini almashtirishga harakat qilib ko'ring:

def replace_vals(_list, my_dict):
    new_list = []
    for item in _list:
        if isinstance(item, list):
              new_list.append(replace_vals(item, my_dict))
        elif item in my_dict:
            new_list.append(my_dict.get(item))
        else:
            new_list.append(item)    
    return new_list

print replace_vals(my_list, my_dict)
1
qo'shib qo'ydi

Siz ichki qismga almashtirishga harakat qilyapsiz. Bu funksiyani sinchkovlik bilan siz uchun qadr-qimmatini almashtirishga harakat qilib ko'ring:

def replace_vals(_list, my_dict):
    new_list = []
    for item in _list:
        if isinstance(item, list):
              new_list.append(replace_vals(item, my_dict))
        elif item in my_dict:
            new_list.append(my_dict.get(item))
        else:
            new_list.append(item)    
    return new_list

print replace_vals(my_list, my_dict)
1
qo'shib qo'ydi

Siz ichki qismga almashtirishga harakat qilyapsiz. Bu funksiyani sinchkovlik bilan siz uchun qadr-qimmatini almashtirishga harakat qilib ko'ring:

def replace_vals(_list, my_dict):
    new_list = []
    for item in _list:
        if isinstance(item, list):
              new_list.append(replace_vals(item, my_dict))
        elif item in my_dict:
            new_list.append(my_dict.get(item))
        else:
            new_list.append(item)    
    return new_list

print replace_vals(my_list, my_dict)
1
qo'shib qo'ydi
my_list = ['a','b','c','dog', ['pig','cat'], 'd']
my_dict = {'dog':'c','pig':'a','cat':'d'}


def replace(item):
    if item in my_dict:
        return my_dict[item]
    else:
        return item
new_list=[];
for item in my_list:
    if isinstance(item,list):
        temp_list=[]
        for subitem in item:
            temp_list.append(replace(subitem))
        new_list.append(temp_list)
    else:
        new_list.append(replace(item))
print new_list

my_list ichidagi ro'yxat o'z ichida bo'lsa, ushbu kod qismi ishlamaydi.

0
qo'shib qo'ydi
my_list = ['a','b','c','dog', ['pig','cat'], 'd']
my_dict = {'dog':'c','pig':'a','cat':'d'}


def replace(item):
    if item in my_dict:
        return my_dict[item]
    else:
        return item
new_list=[];
for item in my_list:
    if isinstance(item,list):
        temp_list=[]
        for subitem in item:
            temp_list.append(replace(subitem))
        new_list.append(temp_list)
    else:
        new_list.append(replace(item))
print new_list

my_list ichidagi ro'yxat o'z ichida bo'lsa, ushbu kod qismi ishlamaydi.

0
qo'shib qo'ydi
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming