“Pemahaman Dikt Python” Kode Jawaban

Python Set dan Dictionary Comprompleensions

simple_dict = {
    'a': 1,
    'b': 2
}
my_dict = {key: value**2 for key,value in simple_dict.items()}
print(my_dict)
#result = {'a': 1, 'b': 4}
Blue-eyed Bat

Dikt Pemahaman Python

# dict comprehension we use same logic, with a difference of key:value pair
# {key:value for i in list}

fruits = ["apple", "banana", "cherry"]
print({f: len(f) for f in fruits})

#output
{'apple': 5, 'banana': 6, 'cherry': 6}
Handsome Hamster

Pemahaman Dikt Python

>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
Splendid Serval

Jenis Pemahaman Dikt

# Dictionary Comprehension Types

{j:j*2 for i in range(10)}

#using if 
{j:j*2 for j in range(10) if j%2==0}

#using if and 
{j:j*2 for j in range(10) if j%2==0 and j%3==0}

#using if else
{j:(j*2 if j%2==0 and j%3==0 else 'invalid' )for j in range(10) }
Tired Pasha rocks

Kamus Pemahaman Python

print({i:j for i,j in zip(txt_list,num) if i!="All"})
Grieving Goshawk

pemahaman dikt

# Dictionary comprehension

{i:i*2 for i in range(10)}
alberduris

Jawaban yang mirip dengan “Pemahaman Dikt Python”

Pertanyaan yang mirip dengan “Pemahaman Dikt Python”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya