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

如何在嵌套列表(list of list)中替换与移动元素?附示例数据

Got it, let's tackle those nested list modifications you need. I'll walk you through common replacement and reordering operations using your sample movie data, with clear Python code snippets you can adapt.

First, let's lay out your raw nested list for reference:

movie_data = [
    ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'Studio Movie Grill - Downey', '11:15 am', '12:05 pm', '1:40', '2:30', '4:15', '6:40', '7:30', '9:05', '10:15'],
    ['90 min', 'Drama', 'Horror', 'Thriller', 'User Rating: 8.2/10 (28,588 user ratings)', '82', 'Metascore', 'Rank:', '2', 'Showtimes:', 'AMC Norwalk 20', '11:45 am', '12:45 pm', '2:15', '3:15', '4:45', '5:45', '7:15', '...']
]

1. Replace Specific Elements

Let's start with straightforward replacements, like updating labels or filling in missing details:

# Replace "Rank:" with a more descriptive label in all sublists
for movie in movie_data:
    if 'Rank:' in movie:
        rank_pos = movie.index('Rank:')
        movie[rank_pos] = 'Box Office Rank:'

# Replace the "..." placeholder in the second movie's showtimes
second_movie = movie_data[1]
if '...' in second_movie:
    ellipsis_pos = second_movie.index('...')
    second_movie[ellipsis_pos] = 'More showtimes available'

2. Reorder/Move Elements

Sometimes you need to rearrange elements for better readability. For example, let's fix the Metascore formatting (right now the score comes before the label) and clean up the showtimes section:

for movie in movie_data:
    # Combine Metascore label and score into one element
    if 'Metascore' in movie:
        meta_label_pos = movie.index('Metascore')
        meta_score = movie[meta_label_pos - 1]
        # Replace the two separate elements with a single formatted string
        movie[meta_label_pos - 1:meta_label_pos + 1] = [f'Metascore: {meta_score}']

    # Move theater name into the "Showtimes:" label for clarity
    if 'Showtimes:' in movie:
        showtimes_pos = movie.index('Showtimes:')
        theater_name = movie[showtimes_pos + 1]
        # Update the label and remove the standalone theater name
        movie[showtimes_pos] = f'Showtimes at {theater_name}'
        del movie[showtimes_pos + 1]

3. Batch Replace Inconsistent Elements

Your showtimes have some entries missing "am/pm" — let's fix that in bulk:

for movie in movie_data:
    # Find where showtimes start (adjust based on whether we modified the label)
    showtimes_start = None
    for idx, item in enumerate(movie):
        if item.startswith('Showtimes'):
            showtimes_start = idx + 1
            break
    
    if showtimes_start:
        # Add "pm" to times without am/pm (assuming these are afternoon/evening slots)
        for time_idx in range(showtimes_start, len(movie)):
            time = movie[time_idx]
            if not ('am' in time or 'pm' in time):
                movie[time_idx] = f'{time} pm'

View the Modified Data

Run this to see your updated nested list:

for idx, movie in enumerate(movie_data, 1):
    print(f"Movie {idx} Details:")
    print(movie)
    print("---")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:31:45