如何使用 Python API 在 Redis 中使用带有多个元素的 sadd? [英] How to use sadd with multiple elements in Redis using Python API?
问题描述
请考虑以下示例
>>import redis
>>redis_db_url = '127.0.0.1'
>>r = redis.StrictRedis(host = redis_db_url,port = 6379,db = 0)
>>r.sadd('a',1)
>>r.sadd('a',2)
>>r.sadd('a',3)
>>r.smembers('a')
[+] 输出:set(['1', '3', '2'])
[+] output: set(['1', '3', '2'])
>>r.sadd('a',set([3,4]))
>>r.smembers('a')
[+] 输出:set(['1', '3', '2', 'set([3, 4])'])
[+] output: set(['1', '3', '2', 'set([3, 4])'])
>>r.sadd('a',[3,4])
>>r.smember('a')
[+] set(['1', '[3, 4]', '3', '2', 'set([3, 4])'])
[+] set(['1', '[3, 4]', '3', '2', 'set([3, 4])'])
根据https://redis-py.readthedocs.org/en中的官方文档/最新/悲伤(名称,*值)添加值以设置名称
According to the official documentation in https://redis-py.readthedocs.org/en/latest/ sadd(name, *values) Add value(s) to set name
所以这是一个错误还是我遗漏了什么?
So is it a bug or I am missing something ?
推荐答案
当您在参数列表中看到语法 *values
时,这意味着该函数采用可变数量的参数.
When you see the syntax *values
in an argument list, it means the function takes a variable number of arguments.
因此,称之为
r.sadd('a', 1, 2, 3)
您可以通过使用 splat 运算符来解包它来传递一个可迭代对象:
You can pass an iterable by using the splat operator to unpack it:
r.sadd('a', *set([3, 4]))
或
r.sadd('a', *[3, 4])
这篇关于如何使用 Python API 在 Redis 中使用带有多个元素的 sadd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!