您的位置:首页 > 汽车 > 新车 > 哪里有免费网站可以看_建站开发工具_360站长平台链接提交_电商推广和网络推广的区别

哪里有免费网站可以看_建站开发工具_360站长平台链接提交_电商推广和网络推广的区别

2025/8/21 16:19:58 来源:https://blog.csdn.net/xingxiliang/article/details/145780542  浏览:    关键词:哪里有免费网站可以看_建站开发工具_360站长平台链接提交_电商推广和网络推广的区别
哪里有免费网站可以看_建站开发工具_360站长平台链接提交_电商推广和网络推广的区别

Basic Data stucture

list

image-20250221112725995

image-20250221112829809

list code

# Create a list
my_list = [1, 2, 3, 'a', 'b', 'c']
my_list

Out[ ]:

[1, 2, 3, 'a', 'b', 'c']

In [ ]:

# Get length for list
len(my_list)

Out[ ]:

6

In [ ]:

#Accessing Elements -- O(1)
first_element = my_list[0]  # Access the first element
last_element = my_list[-1]  # Access the last element
first_element, last_element

Out[ ]:

(1, 'c')

In [ ]:

#Slicing a List -- O(k), where k is the size of the slice
sub_list = my_list[1:4]  # Get elements from index 1 to 3
sub_list

Out[ ]:

[2, 3, 'a']

In [ ]:

# Appending Elements -- O(1)
my_list.append('d') # Add 'd' to the end of the list
my_list

Out[ ]:

[1, 2, 3, 'a', 'b', 'c', 'd']

In [ ]:

#Inserting Elements -- O(n), where n is the number of elements.
my_list.insert(2, 'x') # Insert 'x' at index 2
my_list

Out[ ]:

[1, 2, 'x', 3, 'a', 'b', 'c', 'd']

In [ ]:

#Removing Elements -- O(n)
my_list.remove('a')  # Remove the first occurrence of 'a'
my_list

Out[ ]:

[1, 2, 'x', 3, 'b', 'c', 'd']

In [ ]:

# Popping Elements
popped_element = my_list.pop()  # Remove and return the last element -- O(1)
popped_element_at_index = my_list.pop(1)  # Remove and return the element at index 1 -- O(n), where n is the number of elements after the popped index
popped_element, popped_element_at_index

Out[ ]:

('d', 2)

In [ ]:

#Finding the Index of an Element -- O(n)
index_of_b = my_list.index('b')
my_list, index_of_b

Out[ ]:

([1, 'x', 3, 'b', 'c'], 3)

In [ ]:

#Sorting a List -- O(n log n).       n: 8.    logn = 3 -->  8/2 = 4 / 2 == 2 / 2 == 1
my_list = [1,3,2,4,6]
my_list.sort()
my_list

Out[ ]:

[1, 2, 3, 4, 6]

In [ ]:

# List Comprehension
squared_numbers = [x**2 for x in range(10)]  # O(n), where n is the length of the list
squared_numbersa = []
for i in range(10):a.append(i ** 2)
a, squared_numbers

Out[ ]:

([0, 1, 4, 9, 16, 25, 36, 49,

Tuple

image-20250221113003665

my_tuple = (1, 2, 3, 3, 'a', 'b', 'c')
my_tuple

Out[ ]:

(1, 2, 3, 3, 'a', 'b', 'c')

In [ ]:

# Get Length of a Tuple
len(my_tuple) # O(1)

Out[ ]:

6

In [ ]:

# Accessing Elements
first_element = my_tuple[0]  # O(1)
last_element = my_tuple[-1]  # O(1)
first_element, last_element

Out[ ]:

(1, 'c')

In [ ]:

# Slicing a Tuple
sub_tuple = my_tuple[1:4]  # O(k), where k is the size of the slice
sub_tuple

Out[ ]:

(2, 3, 'a')

In [ ]:

# Converting a List to a Tuple
my_list = [1, 2, 3]
my_tuple_from_list = tuple(my_list)  # O(n), where n is the number of elements in the list
my_tuple_from_list

Out[ ]:

(1, 2, 3)

In [ ]:

# Finding the Index of an Element
index_of_b = my_tuple.index('b')  # O(n), where n is the number of elements
my_tuple, index_of_b

Out[ ]:

((1, 2, 3, 'a', 'b', 'c'), 4)

In [ ]:

# Counting Elements
count_of_2 = my_tuple.count(2)  # O(n), where n is the number of elements
count_of_2

Out[ ]:

1

Tuple 与list的区别

image-20250221113112289

Dict

image-20250221113204868

dict的遍历:

image-20250221113948831

Set

set可以并集

image-20250221114034881

几个容器的比较

What is difference between dictionary and sets, when to use which?

Answer:

  • Structure

    • Dictionaries: Key-value pairs enclosed in curly braces {}.

    • Sets: Unique elements enclosed in curly braces {}.

  • Orders

    • Dictionaries: Maintain order of insertion (from Python 3.7 onwards).

    • Sets: Do not maintain any specific order.

  • Mutability:

    • Dictionaries: Both keys and values can be changed.

    • Sets: The set itself is mutable, but elements must be immutable.

  • Use Cases:

    • Dictionaries: Storing associated data, fast lookup by key.

    • Sets: Storing unique items, membership testing (if customerID present)

High level comparsion for 4 different basic data structures: List, Tuple, Dictionary, Sets

  • Lists:

    • Ordered + mutable

    • When you need a collection with a specific order and the ability to change its elements.

  • Tuples:

    • Ordered + immutable

    • When you need a collection with a specific order and the assurance that its elements won't change.

  • Dictionaries:

    • Unorded (Python 3.7 before) + mutable

    • When you need to map unique keys to values and access them efficiently via keys

  • Sets:

    • Unordered + mutable (but immutable for elements, cannot update)

    • When you need to store unique items and perform membership testing or set operations.

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com