Questions #1


There are two lists - keys and values. How to make a dictionary out of them?



Download 85,32 Kb.
bet5/24
Sana06.07.2022
Hajmi85,32 Kb.
#752107
1   2   3   4   5   6   7   8   9   ...   24
Bog'liq
interview en

There are two lists - keys and values. How to make a dictionary out of them?
keys = [ 'foo' , 'bar' , 'baz' ]
vals = [ 1 , 2 , 3 ]
dict ( zip (keys, vals))
>>> { 'baz' : 3 , 'foo' : 1 , 'bar' : 2 }

The zip function returns a list of pairs of Nth elements. The dict constructor takes a list of pairs. It treats each pair as a key and a value, respectively.


Where is it faster to find an element - in a list or a set?
In a set, because a set works like a dictionary. The value is looked up by the hash of the key. Computing a hash and matching an address are operations of constant complexity, so it's common to say that looking up a dictionary is O(1).
The exception only works for very small lists up to 5 elements long. In this case, it will be faster for the interpreter to run through the list than to calculate the hash.

Functions


What are *args, **kwargs, when are they required?
The *args and **kwargs expressions are declared in the function signature. They mean that variables named args and kwargs (without asterisks) will be available inside the function. You can use other names, but this is considered bad form.
args is a tuple that accumulates positional arguments. kwargs is a dictionary of positional arguments, where key is the name of the parameter, value is the value of the parameter.
Important: if no parameters are passed to the function, the variables will be equal to an empty tuple and an empty dictionary, respectively, and not None.
Please don't confuse a tuple with a list. The next question explains why.
Why is def foo(bar=[]): bad? Give an example of a bad case. How to fix?
The function is created once when the module is loaded. Named parameters and their default values are also created once and stored in one of the function object's fields.
In our example, bar is equal to an empty list. The list is a mutable collection, so the value of bar can change from call to call. Example :

Download 85,32 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   24




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish