Ketika Anda tidak harus menggunakan daftar dengan Python

Terjemahan artikel disiapkan untuk mengantisipasi dimulainya kursus dasar "Pengembang Python" .








Python, , (list). , : , . , , . .



, , .



1. โ€“



. . . , 4 . list . - . .



>>> code_list_saved = [5, 9, 0, 3]
>>> code_list_entered = [5, 9, 0, 3]
>>> code_list_saved == code_list_entered
True
>>> code_list_saved[2] = 7
>>> code_list_saved == code_list_entered
False


, . , . , , , .



. , Python, , . .



>>> code_tuple_saved = (5, 9, 0, 3)
>>> code_tuple_entered = (5, 9, 0, 3)
>>> code_tuple_saved == code_tuple_entered
True
>>> code_tuple_saved[2] = 7
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment


, (tuple). TypeError, . , , Python. tuple, - , . :



>>> code_picked = (5, 9, 0, 3)
>>> stored_code = hash(code_picked)
>>> -8016007988208603090
>>>
>>> code_attempt1 = (9, 5, 0, 3)
>>> hashed_attempt1 = hash(code_attempted)
>>> code_attempt2 = (5, 9, 0, 3)
>>> hashed_attempt2 = hash(code_attempt2)
>>> stored_code == hashed_attempt1
False
>>> stored_code == hashed_attempt2
True
>>> code_picked_list = [5, 9, 0, 3]
>>> hash(code_picked_list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'


2. โ€“



- , - . , . , in , :



>>> integers = [1, 2, 3, 4, 5]
>>> 5 in integers
True
>>> 9 in integers
False


, , (set) . โ€“ Python. , . Python -. - .



, , :



>>> # Import needed modules
>>> from random import randint
>>> from timeit import timeit
>>> 
>>> # Declare a function to measure the time for membership testing
>>> def time_membership_testing(n):
...      integers_list = list(range(n))
...      integers_set = set(range(n))
...      t_list = timeit(lambda : randint(0, 2*n) in integers_list, number=10000)
...      t_set = timeit(lambda : randint(0, 2*n) in integers_set, number=10000)
...      return f"{n: <9} list: {t_list:.4} | set: {t_set:.4}"
... 
>>> numbers = (100, 1000, 10000, 100000)
>>> for number in numbers:
...     print(time_membership_testing(number))
...
100       list: 0.02304 | set: 0.01333
1000      list: 0.1042 | set: 0.01309
10000     list: 0.9028 | set: 0.01713
100000    list: 8.867 | set: 0.01932


, , . , , , , .



3. โ€“



, , โ€“ dict, , .



, , , -. (, Java, Swift, Kotlin), Python -. , , -, -.



, , -, O(1) โ€“ Big-O. O(1) , , , . :



>>> # Import needed modules
>>> from random import randint
>>> from timeit import timeit
>>> 
>>> # Declare a function to measure the time for value retrieval
>>> def time_value_retrieval_testing(n):
...      id_list = list(range(n))
...      score_list = list(range(n))
...      scores_dict = {x: x for x in range(n)}
...      t_list = timeit(lambda : score_list[id_list.index(randint(0, n-1))], number=10000)
...      t_dict = timeit(lambda : scores_dict[randint(0, n-1)], number=10000)
...      return f"{n: <9} list: {t_list:.4} | dict: {t_dict:.4}"
... 
>>> numbers = (100, 1000, 10000, 100000)
>>> for number in numbers:
...     print(time_value_retrieval_testing(number))
... 
100       list: 0.02423 | dict: 0.01309
1000      list: 0.07968 | dict: 0.01322
10000     list: 0.625 | dict: 0.01565
100000    list: 6.223 | dict: 0.01583


, . list, , , โ€“ , . , , . student_id-score, . , , , , .



, -, , . , , .



4. First-In-First-Out โ€“



, , . FIFO (First-in-first-out). , . pop(0). , , O(n).



deque, , , . FIFO, Python . , FIFO . :



>>> # Import needed modules
>>> from collections import deque
>>> from timeit import timeit
>>> 
>>> # Declare a function to measure the time for FIFO
>>> def time_FIFO_testing(n):
...     integer_list = list(range(n))
...     integer_deque = deque(range(n))
...     t_list = timeit(lambda : integer_list.pop(0), number=n)
...     t_deque = timeit(lambda : integer_deque.popleft(), number=n)
...     return f"{n: <9} list: {t_list:.4} | deque: {t_deque:.4}"
... 
>>> numbers = (100, 1000, 10000, 100000)
>>> for number in numbers:
...     print(time_FIFO_testing(number))
... 
100       list: 3.41e-05 | deque: 1.645e-05
1000      list: 0.0004852 | deque: 0.0003466
10000     list: 0.01762 | deque: 0.002618
100000    list: 2.059 | deque: 0.02067


5. โ€“



Python Data Science , . . , . , , .



, , NumPy, , NumPy. , (, , ), DataFrame Pandas, Pandas. , , , TensorFlow PyTorch.



, . , , , . , .





Python.

, , Python , -.



โ€“ , .






ยซ Pythonยป.







All Articles