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

如何在Python列表中使用变量求解连续数字最大乘积问题

Solving the "Largest Product in a Series" Problem with Python Lists

Let's walk through how to tackle this problem using Python's list functionality—super straightforward once you break it down.

Step 1: Convert the big number string to a list of integers

First, we need to turn that giant 1000-digit string into a list of individual integers. Lists let us easily access elements by index, which is perfect for grabbing adjacent digits later. Here's how:

# Your full 1000-digit number string (truncated here for brevity)
num_str = "73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960..."
# Convert each character to an integer, store in a list
digits = [int(char) for char in num_str]

This list comprehension takes every character in the string, converts it to an integer, and builds our digits list—simple and efficient.

Step 2: Initialize variables to track the maximum product

We'll need two key variables here:

  • max_product: To keep track of the largest product we've found so far. Start it at 0 (since all digits are non-negative, this works fine).
  • A loop variable (like i) to iterate through the list's indices.

Step 3: Iterate through the list and calculate products

We need to check every set of 4 adjacent digits. The last valid starting index for a 4-digit group is len(digits) - 4 (since starting at len(digits)-3 would only give us 3 digits). For each starting index i, we grab the slice digits[i:i+4] (this gives us the 4 adjacent digits starting at i), calculate their product, and update max_product if the current product is larger.

Here's the code for this part:

import math

max_product = 0
# Loop through every valid starting index
for i in range(len(digits) - 3):
    # Get the 4 adjacent digits using list slicing
    current_group = digits[i:i+4]
    # Calculate the product of the group
    current_product = math.prod(current_group)
    # Update max_product if current is bigger
    if current_product > max_product:
        max_product = current_product

print(f"The largest product of 4 adjacent digits is: {max_product}")

If you're using a Python version older than 3.8 (where math.prod was introduced), you can calculate the product manually with a loop:

current_product = 1
for num in current_group:
    current_product *= num

Why lists work here

  • Index access & slicing: Lists let us easily grab adjacent elements with slicing (digits[i:i+4]), which is way cleaner than dealing with string indices directly (since we'd have to convert each character to int every time).
  • Readability: Using a list of integers makes the code intuitive—anyone reading it can immediately tell we're working with numeric values, not string characters.
  • Flexibility: Even though we don't modify the list here, lists are mutable if we ever needed to adjust digits later for edge cases.

Quick note on edge cases

If the number string contains zeros, that's okay—our code will just calculate the product as zero, and since we're tracking the maximum, it'll ignore those if there's a larger product elsewhere. If all groups include a zero, the max product will correctly be zero.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:18:59