Ro'yxatni juda qimmatli lug'atga aylantirish

Har bir ro'yxatdagi birinchi qiymat kalit bo'lgan 4 ta qiymat lug'atiga aylantirmoqchi bo'lgan ro'yxatlar ro'yxati bor. Misol uchun, ro'yxat shunday bo'ladi:

[['267-10-7633', '66', '85', '74', 0], ['709-40-8165', '71', '96', '34', 0]]

va men buni istayman

{"267-10-7633":[66,85,74,0], "709-40-8165", [71,96,34,0] }
1

6 javoblar

Lug'at tushunchasini ishlatishingiz mumkin:

lst = [['267-10-7633', '66', '85', '74', 0], ['709-40-8165', '71', '96', '34', 0]]

{k: v for k, *v in lst}       
# {'267-10-7633': ['66', '85', '74', 0], '709-40-8165': ['71', '96', '34', 0]}

Agar siz python2da bo'lsangiz, siz bir nechta elementni ochish uchun * v dan foydalana olmaysiz.

{x[0]: x[1:] for x in lst}  
# {'267-10-7633': ['66', '85', '74', 0], '709-40-8165': ['71', '96', '34', 0]}

Ushbu turdagi konvertatsiya haqida g'amxo'rlik qilmadingiz. O'ylaymanki, buni qanday qilib bajarish kerakligi haqida boshqa javoblarni topa olasiz.

3
qo'shib qo'ydi
Savolga ko'ra, siz ro'yxatni butun ro'yxatga o'tkazishingiz kerak deb o'ylayman. Py2 ver: {x [0]: xarita (int, x [1:]) ning lst} uchun .
qo'shib qo'ydi muallif JRodDynamite, manba
@JRodDynamite Bu Py2 va boshqalar Py3ning boshqa bir sonini ochadi, bu erda ro'yxati kerak bo'ladi (xarita (int, x [1:]) .
qo'shib qo'ydi muallif schwobaseggl, manba
@JRodDynamite Fikringiz yaxshi javob beradi deb o'ylayman, python 3 bo'lsa: {k: list (map (int, v)) uchun k, * v lst}
qo'shib qo'ydi muallif Psidom, manba

Lug'atni kompilyatsiya qilish uchun dict tushunchasi ro'yxat bilan tanishishingiz mumkin. parchalarni int ga aylantiring:

> lst = [['267-10-7633', '66', '85', '74', 0], ['709-40-8165', '71', '96', '34', 0]]
> {l[0]: [int(x) for x in l[1:]] for l in lst}
{'267-10-7633': [66, 85, 74, 0], '709-40-8165': [71, 96, 34, 0]}
1
qo'shib qo'ydi
ll = [['267-10-7633', '66', '85', '74', 0], ['709-40-8165', '71', '96', '34', 0]]

mydict = {}
for item in ll:
  key,*values = item
  mydict[key] = values

print(mydict)
0
qo'shib qo'ydi

Oddiy yondashuv:

your_list = [['267-10-7633', '66', '85', '74', 0], ['709-40-8165', '71', '96', '34', 0]]

dictionary = {}
for item in your_list:
    dictionary[item[0]] = [int(i) for i in item[1:]]

print(dictionary) 

ro'yxat va dict tushunchasi bilan:

dictionary = {item[0]: [int(i) for i in item[1:]] for item in your_list}
print(dictionary)

Ikkala holatda ham chiqish:

{'267-10-7633': [66, 85, 74, 0], '709-40-8165': [71, 96, 34, 0]}
0
qo'shib qo'ydi

Taqqoslash iboralari bu holatga mos keladi

{element[0]: [int(x) for x in element[1:]] for element in\
    [['267-10-7633', '66', '85', '74', 0], ['709-40-8165', '71', '96', '34', 0]]}
0
qo'shib qo'ydi

Oddiy va tekis old hal.

lst = [['267-10-7633', '66', '85', '74', 0], ['709-40-8165', '71', '96', '34', 0]]

# create an empty dict
new_dict = {}

# iterate through the list
for item in lst:

    # key is first element in the inner list
    # value is second element in the inner list
    key = item[0]
    value = item[1:]
    new_dict[key] = value

print new_dict
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