我在 python 脚本中使用 psycopg2:
conn = psycopg2.connect(......)
cur = conn.cursor()
cur.execute("select * from table1")
rows = cur.fetchall()
for a1 in rows: # how to shuffle them?
我希望每次检索行时它们的顺序都不同。我该怎么做?
更新:
行数约为 50.000
最佳答案
如果计数不是很大,你可以使用random.shuffle
:
from random import shuffle
...
rows = list(cur.fetchall())
shuffle(rows)
# do what you need with the suffled rows
否则您可以随机选择项目。在 Postgres 中有多种方法可以做到这一点:
关于python - 我怎样才能洗牌我从 Postgresql 得到的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41218369/