Python: Lists, Tuples, Sets, Dictionaries, 5 Differences Between Data Types

Python: Lists, Tuples, Sets, Dictionaries, 5 Differences Between Data Types

This blog will introduce 5 differences in data types of lists, tuples, sets, and dictionaries (lists, tuples, sets, and dictionaries); including: order sorting, index index, Mutability variability, Duplicates Allowed allows duplication, Types Allowed allows type

1. Similarities

  • Both can store multiple values, the only difference is the special symbols enclosed

2. Differences

Five key differences make each data type helpful in some way. The difference is based on: -order sorting, index index, Mutability variability, Duplicates Allowed allows duplication, Types Allowed allows types.

2.1 Sorting

Lists, tuples and dictionaries are ordered. set is unordered. Note: Dictionaries prior to Python 3.7 were unordered.

2.2 Index

Accessing a single element from a set of elements is called indexing. Indexing can be done using square brackets and mentioning the index value (starting from 0) within them. index specifies whether the data type can be indexed.

Lists and tuples can be indexed. set cannot be indexed. Dictionary keys are similar to indexes.

2.3 Variability

Mutability specifies whether a value of a data type can initially be modified. It divides data types into mutable and immutable.

Lists, sets, and dictionaries are mutable and can be modified. Tuples are immutable and cannot be modified.
Note: The set value cannot be modified. But values ​​can be added or removed. It is still considered mutable due to changes in the same memory location.
The value of the dictionary is variable, and the key is immutable, but the key can be added and deleted;

2.5 Allowed types

Some data types allow both mutable and immutable values, but some types only allow immutable values. Data types can be classified based on this.

2.4 Duplicate allowed

Another key difference is checking whether a type allows repetition of its elements.

Lists and tuples allow duplicate values. set does not allow duplicate values. If you try to enter duplicates, it will only accept one value and ignore duplicates.
A dictionary allows duplicates in its values, but not in its keys. Keys must be unique.
Lists and tuples allow mutable and immutable values. set only allows immutable values. A set cannot even contain another set.
Dictionaries only allow immutable types in keys. Dictionary values ​​can be mutable and immutable.

source code

# lists, tuples, sets, and dictionaries差别
# python python_collections.py

# 1. 都可以存储多个值,唯一差别是括起来的特殊符号
# 2. 主要差异 五个关键差异使每种数据类型都以某种方式有所帮助。差异基于:-order排序、index索引、 Mutability可变性、Duplicates Allowed允许重复、Types Allowed 允许的类型。

def printA(li, tp, st, di):
    print(li)
    print(tp)
    print(st)
    print(di)
    print()


li = [1, 2, 3, 4]  # Lists use []
tp = (1, 2, 3, 4)  # Tuples use ()
st = {
    
    1, 2, 3, 4}  # Sets use {}
di = {
    
    1: 10, 2: 20}  # Dictionaries use {} but contain key:value pairs
printA(li, tp, st, di)

## 2.1 排序:列表、元组和字典是有序的。 set是无序的。 注意:Python 3.7 之前的字典是无序的。
li = ['A', 'B', 'C']
tp = ('A', 'B', 'C')
st = {
    
    'A', 'B', 'C'}
di = {
    
    'A': 10, 'B': 20, 'C': 30}
printA(li, tp, st, di)

## 2.2 索引:从一组元素中访问单个元素称为索引。可以使用方括号并在其中提及索引值(从 0 开始)来完成索引。索引指定数据类型是否可以索引。列表和元组可以建立索引。 set无法建立索引。 字典可以设置键,因为它们有键和值。但它类似于索引。
li = ['A', 'B', 'C']
tp = ('A', 'B', 'C')
di = {
    
    'A': 10, 'B': 20, 'C': 30}

print(li[0])
print(tp[1])
print(di['C'])

## 2.3 可变性 可变性指定数据类型的值最初是否可以修改。它将数据类型分为可变和不可变。 列表、set和字典是可变的并且可以修改。元组是不可变的并且不能被修改。 注意:set值不能修改。但可以添加或删除值。由于同一内存位置发生更改,它仍然被认为是可变的。
# 可变类型
li = ['A', 'B', 'C']
li[0] = 'D'

print(li)

# 不可变类型
tp = ('A', 'B', 'C')
# tp[0] = 'D'
# TypeError: 'tuple' object does not support item assignment

## 2.4 允许重复 另一个关键区别是检查类型是否允许重复其元素。 列表和元组允许重复值。 set不允许重复值。如果您尝试输入重复项,它将只接受一个值并忽略重复项。 字典允许在其值中出现重复项,但不允许在其键中出现重复项。键必须是唯一的。
li = ['A', 'B', 'C', 'A']
tp = ('A', 'B', 'C', 'A')
st = {
    
    'A', 'B', 'C', 'A'}
di = {
    
    'A': 10, 'B': 20, 'C': 10}

printA(li, tp, st, di)

# 使用相同的key取最后一个设置的值
di = {
    
    'A': 10, 'B': 20, 'A': 20}
print(di)

## 2.5 允许的类型 某些数据类型允许可变值和不可变值,但某些类型仅允许其中的不可变值。数据类型可以基于此进行分类。
# 列表和元组允许可变和不可变值。 set只允许不可变的值。这意味着由于其可变性,一个set甚至不能包含另一个set。字典只允许键中存在不可变类型。字典值可以是可变的和不可变的。
li = [(30, 40), [10, 20]]
tp = ((30, 40), [10, 20])

print(li)
print(tp)

# set不可变,但可以插入/移除元素;
# 该错误表明set不可散列unhashable。所有不可散列类型都是可变的,所有可散列类型都是不可变的。
# st = {10, 20, {30, 40}}
# print(st)
# TypeError: unhashable type: 'set'

# Dict同理,值可变,key不可变;
# di = {'A': [10], [2]: 'B'}
# print(di)
# TypeError: unhashable type: 'list'

Guess you like

Origin blog.csdn.net/qq_40985985/article/details/132186834