在下面的方法定义,什么是*和**为做param2?
def foo(param1, *param2):
def bar(param1, **param2):
Run Code Online (Sandbox Code Playgroud) python syntax parameter-passing variadic-functions argument-unpacking
我在Redis中存储一个列表,如下所示:
redis.lpush('foo', [1,2,3,4,5,6,7,8,9])
Run Code Online (Sandbox Code Playgroud)
然后我得到这样的列表:
redis.lrange('foo', 0, -1)
Run Code Online (Sandbox Code Playgroud)
我得到这样的东西:
[b'[1, 2, 3, 4, 5, 6, 7, 8, 9]']
Run Code Online (Sandbox Code Playgroud)
如何将其转换为实际的Python列表?
另外,我没有看到任何定义RESPONSE_CALLBACKS可以帮助?我错过了什么吗?
一个可能的解决方案(在我看来很糟糕)可以是:
result = redis.lrange('foo',0, -1)[0].decode()
result = result.strip('[]')
result = result.split(', ')
# lastly, if you know all your items in the list are integers
result = [int(x) for x in result]
Run Code Online (Sandbox Code Playgroud)
UPDATE
好的,所以我得到了解决方案.
实际上,该lpush函数希望所有列表项都作为参数传递,而不是作为单个列表传递.来自redis-py源的函数签名清楚地表明......
def lpush(self, name, *values):
"Push ``values`` onto the head of the list ``name``"
return self.execute_command('LPUSH', name, *values)
Run Code Online (Sandbox Code Playgroud)
我上面做的是发送一个列表作为参数,然后作为SINGLE项发送到redis.
我应该按照答案中的建议解压缩列表:
redis.lpush('foo', *[1,2,3,4,5,6,7,8,9])
Run Code Online (Sandbox Code Playgroud)
返回我期望的结果...... …