内置类型
存储数据集合
Set 是 Python 中用于存储数据集合的四种内置数据类型之一,另外三种是 List(列表)、Tuple(元组)和 Dictionary(字典),每种类型都有不同的特性和用途。
A set is an unordered, unchangeable*, and unindexed collection.
集合是一个无序的、不可变的*、无索引的集合。
*Note: While set items are unchangeable, you can remove items and add new items.
*注意:虽然集合项是不可变的,但你可以删除项或添加新项。
Comparison of Python Collections
Here’s a comparison table showing the main differences between List, Tuple, Set, and Dictionary:
Comparison of Python Data Types
Type | Ordered | Changeable | Indexed | Syntax |
---|---|---|---|---|
List | Yes | Yes | Yes | [ ] |
Tuple | Yes | No | Yes | ( ) |
Set | No | Items No*, Set Yes | No | { } |
Dictionary | Yes | Yes | Yes | {key: value} |
*Items in a set cannot be changed, but you can add or remove items from a set.
理解背后的原理
在 Python 集合中,集合的无序性意味着集合中的元素没有固定的顺序。不可变的特性指的是集合中的元素一旦定义就不能被修改,但你可以增加或删除元素。这种设计允许集合在检查成员、删除重复项时提供更高的效率。
In Python sets, the unordered nature means there is no fixed order to the elements in the set. The unchangeable characteristic refers to the fact that once elements are defined in a set, they cannot be altered, though you can add or remove elements. This design allows sets to be more efficient in membership checks and eliminating duplicates.
Python provides a variety of collection data types that are suitable for different purposes. Here’s a detailed explanation of each type:
List (列表)
- Description: A list is an ordered collection that can be changed or modified. It allows duplicate elements, making it suitable for scenarios where items can repeat and order matters.
- 描述:列表是一种有序的集合,可以被修改或更改。它允许元素重复,适用于项目可能重复且顺序重要的场景。
Tuple (元组)
- Description: A tuple is similar to a list in that it is ordered. However, tuples are immutable, meaning once they are created, their elements cannot be changed. This is suitable for fixed data.
- 描述:元组与列表相似,即它们是有序的。但是,元组是不可变的,一旦创建,其元素就不能更改。这适用于固定数据。
Set (集合)
- Description: A set is an unordered collection of items where each item is unique. Sets are ideal for membership testing and eliminating duplicate entries.
- 描述:集合是一个无序的元素集,每个元素都是唯一的。集合非常适合进行成员测试和消除重复条目。
Dictionary (字典)
- Description: A dictionary is a collection which is ordered (as of Python 3.7) and consists of a key-value pair. Each key-value pair maps the key to its associated value. Dictionaries are optimal for fast lookups and can be changed or updated.
- 描述:字典是一个有序的集合(从 Python 3.7 开始),由键值对组成。每个键值对将键映射到其关联值。字典对于快速查找是最佳选择,并且可以更改或更新。
Common List Methods in Python
When working with lists in Python, various methods are available to manipulate and interact with the list elements. Below is a comparison table of some common list methods, including code examples, tips, and warnings.
Comparison Table of Common List Methods
Method | Description | Code Example | Tips | Warnings |
---|---|---|---|---|
append(elem) |
Adds a single element to the end of the list. | my_list.append(5) |
Use for adding one item. | Does not return the new list, just modifies the original. |
insert(index, elem) |
Inserts the element at the given index, shifting elements to the right. | my_list.insert(2, 'a') |
Useful for adding items at a specific position. | May be slow for large lists. |
extend(list2) |
Adds elements from list2 to the end of the list. |
my_list.extend([1, 2, 3]) |
Equivalent to my_list += list2 . |
Use for combining lists. |
index(elem) |
Searches for the element and returns its index. | index = my_list.index('a') |
Use in to check presence before calling. |
Throws ValueError if the element is not found. |
remove(elem) |
Searches and removes the first instance of the element. | my_list.remove('a') |
Removes first occurrence only. | Throws ValueError if the element is not found. |
sort() |
Sorts the list in place. | my_list.sort() |
Use sorted(my_list) to return a sorted list without modifying the original. |
Modifies the original list. |
reverse() |
Reverses the list in place. | my_list.reverse() |
Reverses the order of elements. | Modifies the original list. |
pop(index) |
Removes and returns the element at the given index. | elem = my_list.pop(2) |
Use to remove and get an element in one step. | Returns the rightmost element if index is omitted. |
Code Examples
# Example list
my_list = [1, 2, 3, 4]
##### # append()
my_list.append(5)
print(my_list) # Output: [1, 2, 3, 4, 5]
##### # insert()
my_list.insert(2, 'a')
print(my_list) # Output: [1, 2, 'a', 3, 4, 5]
##### # extend()
my_list.extend([6, 7])
print(my_list) # Output: [1, 2, 'a', 3, 4, 5, 6, 7]
##### # index()
print(my_list.index('a')) # Output: 2
##### # remove()
my_list.remove('a')
print(my_list) # Output: [1, 2, 3, 4, 5, 6, 7]
##### # sort()
my_list = [4, 2, 1, 3]
my_list.sort()
print(my_list) # Output: [1, 2, 3, 4]
##### # reverse()
my_list.reverse()
print(my_list) # Output: [4, 3, 2, 1]
##### # pop()
print(my_list.pop(1)) # Output: 3
print(my_list) # Output: [4, 2, 1]
Tips
- Choosing Between Methods: Use
append()
for adding single elements,extend()
for adding multiple elements, andinsert()
for adding elements at specific positions. - Checking for Element Presence: Use the
in
keyword before usingindex()
orremove()
to avoidValueError
.
Warnings
- Modifying Original List: Methods like
append()
,insert()
,extend()
,remove()
,sort()
, andreverse()
modify the original list. If you need to keep the original list intact, use methods likesorted()
for sorting. - Performance Considerations: Inserting or removing elements in the middle of large lists can be slow due to the need to shift elements.
sort() vs sorted() in Python
When sorting lists in Python, you can use either the sort()
method or the sorted()
function. Below is a detailed comparison of these two approaches, including code examples, tips, and warnings.
Comparison Table
Feature | sort() Method |
sorted() Function |
---|---|---|
Modifies Original List | Yes | No |
Returns | None (modifies list in place) | New sorted list |
Usage | Only for lists | Works with any iterable |
Syntax | list.sort() |
sorted(iterable) |
Code Examples
Using sort()
The sort()
method sorts a list in place, meaning it modifies the original list and does not return a new list.
# Example list
my_list = [4, 2, 3, 1]
##### # Using sort()
my_list.sort()
print(my_list) # Output: [1, 2, 3, 4]
Using sorted()
The sorted()
function returns a new sorted list from the elements of any iterable.
# Example list
my_list = [4, 2, 3, 1]
##### # Using sorted()
sorted_list = sorted(my_list)
print(sorted_list) # Output: [1, 2, 3, 4]
##### # Original list remains unchanged
print(my_list) # Output: [4, 2, 3, 1]
Tips
- Choosing Between
sort()
andsorted()
:- Use
sort()
if you need to sort a list in place and do not need to retain the original order. - Use
sorted()
if you need a sorted version of the iterable but also need to keep the original order intact.
- Use
- Custom Sorting:
- Both
sort()
andsorted()
accept akey
parameter for custom sorting. For example, sorting by the length of strings:my_list = ['apple', 'banana', 'cherry'] my_list.sort(key=len) print(my_list) # Output: ['apple', 'cherry', 'banana']
- Both
Warnings
- Modifying Original List:
- The
sort()
method modifies the original list. If you need to keep the original list unchanged, usesorted()
.
- The
- Performance Considerations:
- For large datasets, consider the overhead of creating a new list with
sorted()
versus modifying the original list withsort()
.
- For large datasets, consider the overhead of creating a new list with
More
Python: Data Structures – Lists, Tuples, Sets & Dictionaries tutorial – Oggi AI – Artificial Intelligence Today
Leave a Reply