gpt4 book ai didi

python - 我怎样才能洗牌我从 Postgresql 得到的结果?

转载 作者:行者123 更新时间:2023-11-29 14:18:01 25 4
gpt4 key购买 nike

我在 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 中有多种方法可以做到这一点:

1) Best way to select random rows PostgreSQL

2) quick random row selection in Postgres

关于python - 我怎样才能洗牌我从 Postgresql 得到的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41218369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com