Hướng dẫn difference between collections in python - sự khác biệt giữa các bộ sưu tập trong python

Hướng dẫn difference between collections in python - sự khác biệt giữa các bộ sưu tập trong python

Hướng dẫn difference between collections in python - sự khác biệt giữa các bộ sưu tập trong python

Bộ sưu tập là những cách cơ bản để lưu trữ và tổ chức dữ liệu. Có bốn loại bộ sưu tập cơ bản có sẵn trong Python - tuples, lists, dictionariessets. Để trở nên thành thạo lập trình Python, các nhà phát triển nên biết sự khác biệt cơ bản giữa các cấu trúc dữ liệu này để họ có thể chọn loại bộ sưu tập phù hợp cho một công việc nhất định trong tay.

Bảng dưới đây cho thấy so sánh cạnh nhau của các bộ sưu tập Python khác nhau:

TupleDanh sáchTừ điểnBộ
Eample('Sách 1', 12,99)['Apple', 'Chuối', 'Orange']]{'name': 'joe', 'tuổi': 10}{10, 20, 12}
Mutable?Bất biếnCó thể thay đổiCó thể thay đổiCó thể thay đổi
Ordered?Đặt hàngĐặt hàngBảo tồn đơn đặt hàng kể từ Python 3.7Không đặt hàng
Iterable?Có (mất thời gian tuyến tính)Có (mất thời gian tuyến tính)Có (thời gian không đổi)Có (thời gian không đổi)
Sử dụng trường hợpDữ liệu bất biếnDữ liệu cần thay đổiCác cặp khóa/giá trịCác mặt hàng độc đáo

Phương pháp phổ biến

Nó cũng khá quan trọng để nhớ một số phương pháp phổ biến có sẵn cho mỗi bộ sưu tập. Điều này sẽ giúp trong một số trường hợp như quá trình phỏng vấn.

Bộ sưu tậpPhương pháp phổ biến
TupleNó cũng khá quan trọng để nhớ một số phương pháp phổ biến có sẵn cho mỗi bộ sưu tập. Điều này sẽ giúp trong một số trường hợp như quá trình phỏng vấn.
Danh sáchBộ sưu tập
Từ điểnBộ
BộEample

('Sách 1', 12,99)

t = ('Book 1', 12.99)
# Get the value element for the supplied index, returns IndexError for invalid index  
print(t[1])   # 12.99
# Get length of a tuple
print(len(t))  # 2
# Get index of a given value, if the supplied value is not found,  returns value error
print(t.index('Book 1')) # 0
# Count number of items a given value is present in the collection
print(t.count(12))  # 0

['Apple', 'Chuối', 'Orange']]

{'name': 'joe', 'tuổi': 10}

my_list = ['apple', 'banana', 'orange']
# get element present in the specified index
print(my_list[1])  # banana
# find length of the list
print(len(my_list))  # 3
# get index of specified value
print(my_list.index('orange')) # 2
# count number of items times a specified value is present in the collection
print(my_list.count('banana'))  # 1
# adds item to the end of the list
my_list.append('pear')  
# adds item in the specified index
my_list.insert(1, 'grapes')
# removes and returns item from the end of the list
print(my_list.pop())   # pear
# removes specified value from the list
my_list.remove('banana')
# reverses the list
my_list.reverse()
print(my_list)   # ['orange', 'grapes', 'apple']
# sorts the list
my_list.sort()
print(my_list)   # ['apple', 'grapes', 'orange']

['Apple', 'Chuối', 'Orange']]

{'name': 'joe', 'tuổi': 10}

d = {'name': 'Joe', 'age': 10}
# returns value of specified key
print(d['name'])  # Joe
# returns length of a dictionary
print(len(d))  # 2
# returns iterable list of keys
print(d.keys())  # dict_keys(['name', 'age'])
# returns iterable list of values
print(d.values()) # dict_values(['Joe', 10])
# returns iterable list of key / value pairs
print(d.items()) # dict_items([('name', 'Joe'), ('age', 10)])
# deletes given key from the dictionary
del d['age']
print(d)   # {'name': 'Joe'}

['Apple', 'Chuối', 'Orange']]

{'name': 'joe', 'tuổi': 10}

s1 = {10, 20, 12}
s2 = {100, 21, 12, 35, 40}
# finds length of a given set
print(len(s2))  # 5
# adds an element to the set, ignored if the element already exists
s1.add(21) 
# updates set with elements of another set
s1.update({33, 30})
# removes specified element from the set, throws KeyError if given element is not in the set
s1.remove(33)
# returns a new set containing all elements from both sets 
print(s1.union(s2))  # {33, 35, 100, 40, 10, 12, 20, 21, 30}
# returns a new set containing common elements from both sets
print(s1.intersection(s2))   # {12, 21}

['Apple', 'Chuối', 'Orange']]

Các bộ sưu tập khác nhau trong Python là gì?

Ngôn ngữ lập trình Python có bốn loại dữ liệu thu thập- danh sách, bộ, bộ và từ điển.list, tuple, sets and dictionary.

Bốn loại bộ sưu tập được cung cấp bởi Python là gì?

Python đã có 4 loại dữ liệu thu thập tích hợp.Đây là danh sách, tuple, từ điển và thiết lập.list, tuple, dictionary, and set.

Bộ sưu tập có nghĩa là gì trong Python?

Bộ sưu tập là một mô-đun Python tích hợp, thực hiện các kiểu dữ liệu container chuyên dụng cung cấp các lựa chọn thay thế cho các thùng chứa tích hợp mục đích chung của Python như Dict, Danh sách, Bộ và Tuple.a built-in Python module that implements specialized container datatypes providing alternatives to Python's general purpose built-in containers such as dict , list , set , and tuple .

Nhu cầu của các bộ sưu tập trong Python là gì?

Bộ sưu tập là một mô-đun Python tích hợp cung cấp các kiểu dữ liệu container hữu ích.Các kiểu dữ liệu container cho phép chúng tôi lưu trữ và truy cập các giá trị một cách thuận tiện.Nói chung, bạn sẽ có danh sách, bộ dữ liệu và từ điển.Nhưng, trong khi xử lý dữ liệu có cấu trúc, chúng tôi cần các đối tượng thông minh hơn.provides useful container datatypes. Container datatypes allow us to store and access values in a convenient way. Generally, you would have used lists, tuples, and dictionaries. But, while dealing with structured data we need smarter objects.