Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming
Python Projects for Beginners A Ten Week Bootcamp Approach to Python
Dictionaries are helpful data collections for not only keeping information connected but also improving efficiency. Keep this in mind when you’re trying to answer programming questions or making a program faster. Like the information on Big O Notation, this is simply an introduction into hash tables. If you’d like to learn more, be sure to look it up using Google, Quora, etc. Dictionaries vs. ListsTo understand the true power of a hash table and Python dictionaries, let’s compare it against a list. We’ll write a conditional statement to have Python check for a given item within a dictionary and list, and we’ll time how long each one takes. We’re going to separate the code into two cells. The first cell will generate the dictionary and list with 10 million items: # creating data collections to test for time complexity import time d = { } # generate fake dictionary for i in range(10000000): d[ i ] = "value" big_list = [ x for x in range(10000000) ] # generate fake list Go ahead and run the cell. Nothing will happen yet. We’ve simply made the variables within this cell so that we don’t have to re-create them, as it takes a couple seconds depending on your computer. In the following cell, we’re going to keep a timer on how long each data collection takes to find the last element. We’ll use the time module in order to track the start and end time: 1| # retrieving information and tracking time to see which is faster 3| start_time = time.time( ) # tracking time for dictionary 5| if 9999999 in d: 6| print("Found in dictionary") 8| end_time = time.time( ) – start_time 10| print( "Elapsed time for dictionary: { }".format(end_time) ) 12| start_time = time.time( ) # tracking time for list 14| if 9999999 in big_list: 15| print("Found in list") 17| end_time = time.time( ) – start_time 19| print( "Elapsed time for list: { }".format(end_time) ) Go ahead and run the cell. On lines 3 and 12, we access the current time in UTC format. After checking our conditions, we get the current time in UTC format again; however, we subtract the start time from it to get the number of seconds the entire execution took. You’ll notice there’s a large difference between the two times. The list will usually take between 1 and 1.5 seconds, whereas the dictionary is almost instant every time. Now this doesn’t seem like that big of a difference, but what if you needed to search for 1000 items. Using a list now becomes a problem, as a dictionary would continue to do it instantly, but the list would take much longer. Note the time module gets time in UtC (universal time) unless otherwise stated. UtC began on January 1, 1970. the number you see when you output time.time() is the number of seconds since that day at 12:00 Am. Download 2,61 Mb. Do'stlaringiz bilan baham: 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 |