There are many differences, and there are these differences.
>>> bignum = 1000000
>>> a = set(range(bignum))
>>> b = list(range(bignum))
>>> import time
>>> def find_nums_in(c):
... ... s_t = time.time()
... ... for i in range(bignum):
... ... if i in c:
... ... pass
... ... print('took %s sec'%(time.time() - s_t,))
...
>>> find_nums_in(a)
took 0.031252145767211914 sec
>>> find_nums_in(b)
took 4891.12208032608 sec
>>>
The list is the one we know.
In the case of Set, each time you add a new element to the list, you perform a consistency check on all elements and do not add the element in the case of the same element.
This means that all elements are unique.
Set in Python
A set in Python holds a sequence of values. It is sequenced but does not support indexing.
List in Python
A list is a data type in Python that contains an ordered sequence of zero or more elements. Lists in Python are mutable, which means they can be changed. This data type is useful when you want to store many related values, as you can store data with similar values—such as car names, or employee names—in one variable.
You can learn more here: https://theknowshares.com/lists-in-python/
Set:
List
If you use a set, not a list, It's convenient to combine the fixed attributes of a unique value into one and pass on information here and there For example, {member name, unique id, join date} In the same case, it's a unique value, and it's good to tie together fixed information about this member (the content of the element does not change), so you can make it three.
© 2024 OneMinuteCode. All rights reserved.