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

在Python2中生成特定序列及0,1,3,4,6,7…列表的最简/最快方法?

Generating the sequence 0,1,3,4,6,7,… in Python 2: Simplest & Fastest Approaches

Hey there! Let's start by clarifying the pattern of your target sequence: it's made by interleaving two arithmetic sequences. One starts at 0 and increments by 3 (0, 3, 6, ...), the other starts at 1 and also increments by 3 (1, 4, 7, ...), with elements alternating between the two.

Simplest Implementation

If code brevity and readability are your top priorities, a list comprehension is hard to beat. Here's a one-liner that generates the first k elements:

k = 6  # Example: generate first 6 elements
sequence = [i//2 * 3 + i%2 for i in range(k)]

Let me break that down: i//2 groups the indices into pairs (0,0,1,1,2,2...), multiplying by 3 gives the starting value of each pair (0,0,3,3,6,6...), then adding i%2 (0,1,0,1...) fills in the second element of each pair.

Alternatively, using itertools makes the logic super explicit—great if you want someone reading your code to immediately grasp what's happening:

import itertools

num_pairs = 3  # Generate 3 pairs (6 total elements)
seq_a = range(0, 3*num_pairs, 3)
seq_b = range(1, 3*num_pairs, 3)
sequence = list(itertools.chain.from_iterable(zip(seq_a, seq_b)))

This zips the two sequences into pairs (like (0,1), (3,4)) then flattens them into a single list.

Fastest Implementation

When you need to generate very long sequences, speed becomes critical. Let's cover pure Python and optimized library approaches:

Pure Python Optimal

Avoid unnecessary overhead by leveraging range (which is efficient in Python 2) and generator expressions to build the list without extra intermediate objects:

def generate_fast(length):
    half = (length + 1) // 2
    part1 = range(0, 3*half, 3)
    part2 = range(1, 3*(length//2) + 1, 3)
    
    sequence = []
    # Interleave with a generator to minimize memory overhead
    sequence.extend(x for pair in zip(part1, part2) for x in pair)
    # Handle odd lengths by adding the last element from part1
    if length % 2 != 0:
        sequence.append(part1[-1])
    return sequence

This is faster than list comprehensions for large length because it avoids creating temporary sublists.

Even Faster with NumPy

If you're allowed to use third-party libraries, NumPy's vectorized operations blow pure Python out of the water for large sequences:

import numpy as np

def generate_numpy_fast(length):
    indices = np.arange(length)
    return (indices // 2 * 3 + indices % 2).tolist()

NumPy handles the entire sequence calculation in C-level operations, which is drastically faster than looping in Python for big datasets.

Quick Check

No matter which method you use, generating the first 6 elements will give you [0, 1, 3, 4, 6, 7]—exactly what you're after.

To recap:

  • Go for the list comprehension if you want the shortest, most readable code.
  • Use the pure Python generate_fast function for speed without external dependencies.
  • Use the NumPy version if you need maximum performance for very long sequences.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:33:25