Redis-Pyredis在SET上迭代烧结结果

2024-09-28 19:35:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有2套结构,添加了以下值:

r.sadd("clONE", 'abc')
r.sadd("clONE", 'def')
r.sadd("clONE", 'ghi')

r.sadd("TWO", 'abc')
r.sadd("TWO", 'def')

print(r.sinter("clONE", "TWO"))
 OUTPUT: set(['abc', 'def'])

如何使用pyredis从SET()中获取值“abc”和“def”?我试图通过指定数组索引[0]来使用数组语法,但得到以下错误

^{pr2}$

Tags: outputclonedef语法数组结构abcprint
1条回答
网友
1楼 · 发布于 2024-09-28 19:35:09

集合没有索引。顺序取决于内部哈希。 不要依赖集合中的顺序,即使它看起来合乎逻辑。在

您可以执行以下操作:

s = r.sinter("clONE", "TWO")

# iterate through the set, unsorted
for i in s:
    print(i)

或者

^{pr2}$

相关问题 更多 >