Python 不知怎么的,你在“整个数据库的循环”中把我弄丢了。这听起来效率极低。前两步相当于:从某个用户标准所在的某个表中选择*。你必须以某种方式确定你的人口。 result = [] for n, row in enumerate(cursor.execute(us

Python 不知怎么的,你在“整个数据库的循环”中把我弄丢了。这听起来效率极低。前两步相当于:从某个用户标准所在的某个表中选择*。你必须以某种方式确定你的人口。 result = [] for n, row in enumerate(cursor.execute(us,python,algorithm,multiple-choice,Python,Algorithm,Multiple Choice,不知怎么的,你在“整个数据库的循环”中把我弄丢了。这听起来效率极低。前两步相当于:从某个用户标准所在的某个表中选择*。你必须以某种方式确定你的人口。 result = [] for n, row in enumerate(cursor.execute(user_query), 1): if n <= 10: result.append(row) elif random() < 10.0 / n: i = randrange(10)


不知怎么的,你在“整个数据库的循环”中把我弄丢了。这听起来效率极低。前两步相当于:从某个用户标准所在的某个表中选择*。你必须以某种方式确定你的人口。
result = []
for n, row in enumerate(cursor.execute(user_query), 1):
    if n <= 10:
        result.append(row)
    elif random() < 10.0 / n:
        i = randrange(10)
        result[i] = row
result = set()
while len(result) < 10:
     record = random.choice(entire_db)
     if record not in result and matches(record, user_criteria):
          result.add(record)
random.sample(list(cursor.execute(user_query)), 10)
SELECT item
FROM items
WHERE type = ...
ORDER BY RANDOM()
LIMIT 10
SELECT item
FROM items
WHERE type = type1
ORDER BY RANDOM()
LIMIT 5
UNION ALL
SELECT item
FROM items
WHERE type = type2
ORDER BY RANDOM()
LIMIT 5
types = ("type1", "type2", "type3")
limit = 10 // len(types) # be careful, for 11 or more types, this will set the limit to 0
sql = """
    SELECT item
    FROM items
    WHERE type = ?
    ORDER BY RANDOM()
    LIMIT %s
""" % (limit)
unioned_sql = " UNION ALL ".join([sql for i in range(len(types))])
result = cursor.execute(unioned_sql, types)
Return a random element from the non-empty sequence seq.
Return a k length list of unique elements chosen from the population
sequence. Used for random sampling without replacement.
>>> import random
>>> random.choice([1, 2, 3, 4, 5])  # Choose a random element
3
>>> random.sample([1, 2, 3, 4, 5],  3)  # Choose 3 elements
[4, 1, 5]
SELECT * FROM Items
WHERE Type=1