开始使用免费开始使用

使用 Redis 存储 Python 字典

除了存储简单的键值对,您还可以存储更复杂的 Python 数据类型,例如 dict(字典)。在这道关于 Redis 的最后一个练习中,您将使用 Python 的 redis 模块练习存储和检索 Python 字典。

与之前相同,已创建连接对象并存入变量 redis_conn。祝您顺利!

本练习是课程的一部分

NoSQL 入门

查看课程

练习说明

  • 使用变量 redis_conn,将字典 london_weather_mapping 存储到键 "london_weather"
  • 使用变量 redis_conn 和相应的方法,检索并打印键 "london_weather" 对应的键值对。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create a dictionary containing weather data
london_weather_mapping = {
	"temperature": 42,
	"humidity": 88,
	"visibility": "low"
}

# Store the london_weather key-value pair
____.____(
    "____",
	mapping=____
)

# Retrieve and print the london_weather key-value pair
print(____.____("____"))
编辑并运行代码