特性 | 元组 (tuple ) | 列表 (list ) | 字典 (dict ) |
---|---|---|---|
可变性 | 不可变 | 可变 | 可变 |
性能 | 创建和访问速度快 | 创建和访问速度稍慢 | 访问速度超快 |
使用场景 | 存储顺序常量数据 | 存储顺序动态数据 | 存储随机动态数据 |
内存占用 | 较小 | 较大 | 较大 |
操作支持 | 较少 | 较多 | 较多 |
根据具体需求选择合适的数据结构:
-
如果需要存储不需要修改的数据,使用 元组。
-
如果需要存储需要动态修改的数据,使用 列表。
-
如果需要通过键快速查找值,使用 字典。
以下是三种数据结构的示例:
(
{"ticker":"002261","open":27.9500,"pre_close":27.1900,"close":29.9100,"high":29.9100,"low":27.6000,"volume":2430577.0000,"amount":7067431630.0000,"date":"2025-02-12"},
{"ticker":"002261","open":30.6100,"pre_close":30.1900,"close":33.2100,"high":33.2100,"low":29.4800,"volume":4111427.0000,"amount":13064592370.0000,"date":"2025-02-14"},
{"ticker":"002261","open":32.2900,"pre_close":29.9100,"close":30.1900,"high":32.9000,"low":29.9600,"volume":5749920.0000,"amount":18157238421.0000,"date":"2025-02-13"}
)
[
{"ticker":"002261","open":27.9500,"pre_close":27.1900,"close":29.9100,"high":29.9100,"low":27.6000,"volume":2430577.0000,"amount":7067431630.0000,"date":"2025-02-12"},
{"ticker":"002261","open":30.6100,"pre_close":30.1900,"close":33.2100,"high":33.2100,"low":29.4800,"volume":4111427.0000,"amount":13064592370.0000,"date":"2025-02-14"},
{"ticker":"002261","open":32.2900,"pre_close":29.9100,"close":30.1900,"high":32.9000,"low":29.9600,"volume":5749920.0000,"amount":18157238421.0000,"date":"2025-02-13"}
]
{
'2025-02-14': {'code': '002261', 'open': 30.61, 'pre_close': 30.19, 'close': 33.21, 'high': 33.21, 'low': 29.48, 'volume': 4111427, 'amount': 13064592370.0},
'2025-02-12': {'code': '002261', 'open': 27.95, 'pre_close': 27.19, 'close': 29.91, 'high': 29.91, 'low': 27.6, 'volume': 2430577, 'amount': 7067431630.0},
'2025-02-13': {'code': '002261', 'open': 32.29, 'pre_close': 29.91, 'close': 30.19, 'high': 32.9, 'low': 29.96, 'volume': 5749920, 'amount': 18157238421.0}
}