You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python技术问题:如何从电影列表每日输出唯一不同影片?

Fixing Duplicate Daily Movie Picks

Hey Corey, let's break down why your current random.shuffle setup is spitting out the same movies two days in a row, then walk through two solid fixes to get unique daily picks.

The Root of the Problem

When you run your script fresh each day, random.shuffle starts with a new random sequence—but if you don't anchor that sequence to something unique per day (like the date) or track which movies you've already picked, you'll end up with repeat results. The default random seed in Python is based on system time, but if your script runs at similar times each day, or you're testing it back-to-back, you can get overlapping sequences that lead to duplicate picks.


Solution 1: Date-Based Sequential Picks (Simple & Predictable)

This approach uses the current date to pick a movie from your list, ensuring each day gets a unique entry (and tomorrow's pick is guaranteed different). It's great if you want a rotating schedule without repeats until you cycle through all movies.

import random
from datetime import date

# Your full movie list
movie_list = ["Howl's Moving Castle", "Akira", "Spirited Away", "Ghost in the Shell"]

# Convert today's date to a unique integer (ordinal date)
today = date.today().toordinal()
total_movies = len(movie_list)

# Calculate which index to use for today and tomorrow (cycles through the list)
today_index = today % total_movies
tomorrow_index = (today + 1) % total_movies

# Grab the movies
today_movie = movie_list[today_index]
tomorrow_movie = movie_list[tomorrow_index]

# Output results
print(f"Movie of the Day: {today_movie}")
print(f"Movie of the Day Tomorrow: {tomorrow_movie}")

How it works:

  • date.today().toordinal() turns the date into a unique number (e.g., January 1, 2024 is 738920)
  • Using modulo % total_movies wraps that number to fit within your list's length, so you cycle through movies in order
  • Tomorrow's index is just today's index + 1 (wrapping back to 0 if you hit the end of the list)

Solution 2: Random Non-Repeating Picks (With History Tracking)

If you want fully random picks that never repeat until you've used every movie, you can save your selection history to a file. This way, the script remembers which movies have already been chosen.

import random
import os

# Your full movie list
movie_list = ["Howl's Moving Castle", "Akira", "Spirited Away", "Ghost in the Shell"]
# File to track which movies we've already picked
history_file = "movie_selection_history.txt"

# Load existing selection history
if os.path.exists(history_file):
    with open(history_file, "r") as f:
        selected_movies = [line.strip() for line in f if line.strip()]
else:
    selected_movies = []

# Get movies that haven't been picked yet
available_movies = [movie for movie in movie_list if movie not in selected_movies]

# Reset history if all movies have been used
if not available_movies:
    selected_movies = []
    available_movies = movie_list.copy()

# Pick today's movie and add it to history
today_movie = random.choice(available_movies)
selected_movies.append(today_movie)

# Pick tomorrow's movie from the remaining available options
remaining_for_tomorrow = [m for m in available_movies if m != today_movie]
tomorrow_movie = random.choice(remaining_for_tomorrow)

# Save updated history to file
with open(history_file, "w") as f:
    for movie in selected_movies:
        f.write(f"{movie}\n")

# Output results
print(f"Movie of the Day: {today_movie}")
print(f"Movie of the Day Tomorrow: {tomorrow_movie}")

How it works:

  • The script reads a text file to see which movies have already been selected
  • It picks today's movie from the unused pool, then adds it to the history
  • Tomorrow's pick is pulled from the remaining unused movies, so it's guaranteed unique
  • Once all movies are used, the history resets and you start over

内容的提问来源于stack exchange,提问作者Corey Bryant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:27:29