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

Python Generators优质练习方向咨询:已完成斐波那契实现

Python Generator Practice Exercises for Real-World Use Cases

Hey Jeff, great to hear you’ve got the basics of Python generators down and already tackled the Fibonacci sequence—those are solid foundations! Generators really shine when you’re dealing with lazy evaluation, memory efficiency, or processing data that’s too big to fit into memory all at once. Here are some practical, targeted exercises that’ll let you flex those generator muscles:

Core Practice Exercises

  • Large Log File Parser
    Write a generator that reads an oversized log file (think gigabytes) and yields structured entries for each line—for example, extracting timestamp, source IP, and request path from lines like [2024-05-20 14:32:10] 192.168.1.1 GET /api/users.
    Why generators fit: Loading the entire file into a list would crash most systems due to memory limits; generators process one line at a time, keeping memory usage minimal.
    Quick starter snippet:

    def parse_log_file(file_path):
        with open(file_path, 'r') as f:
            for line in f:
                # Parse line into components here
                parts = line.strip().split()
                timestamp = parts[0].strip('[]')
                ip = parts[1]
                method = parts[2]
                path = parts[3]
                yield {'timestamp': timestamp, 'ip': ip, 'method': method, 'path': path}
    
  • Infinite Prime Generator
    Build a generator that produces an infinite sequence of prime numbers. Each call to next() should return the next prime in the sequence.
    Why generators fit: You can’t store an infinite set of primes in a list—generators generate values on-demand, so you can iterate as long as needed without memory bloat.
    Hint: Start with checking each integer starting from 2, and for each number, verify it’s not divisible by any previously generated primes.

  • Streaming Data Pipeline
    Create a chain of generators to process a CSV dataset:

    1. First generator: Reads the CSV line-by-line (skipping the header)
    2. Second generator: Filters out rows where a specific column (e.g., "age") is below a threshold
    3. Third generator: Converts string values in numeric columns to integers/floats
      Why generators fit: Each step processes data lazily—no intermediate lists are stored, making it efficient even for huge datasets.
      Example structure:
    def read_csv(file_path):
        with open(file_path, 'r') as f:
            next(f)  # Skip header
            for line in f:
                yield line.strip().split(',')
    
    def filter_by_age(rows, min_age):
        for row in rows:
            if int(row[2]) >= min_age:
                yield row
    
    def convert_numeric(rows):
        for row in rows:
            yield (row[0], row[1], int(row[2]), float(row[3]))
    
    # Usage:
    for processed_row in convert_numeric(filter_by_age(read_csv('users.csv'), 18)):
        print(processed_row)
    
  • Pagination API Scraper
    Simulate interacting with a paginated API (e.g., an endpoint that returns 10 items per page and a "next page" token). Write a generator that automatically fetches all pages and yields individual items one by one.
    Why generators fit: You don’t need to fetch all pages upfront and store them in a list— the generator fetches the next page only when it’s needed, saving bandwidth and memory.
    Hint: Use a loop inside the generator that checks for a "next page" flag; after fetching each page, iterate over the items and yield them.

  • Custom Iterator Tool Implementations
    Recreate some of Python’s itertools functions using generators:

    • my_range(start, stop, step): Mimic the built-in range() but as a generator
    • my_chain(*iterables): Link multiple iterables together, yielding elements from each in order
    • my_take(n, iterable): Yield the first n elements from an iterable
      Why generators fit: This forces you to understand the iterator protocol and how generators power many of Python’s built-in tools.
      Example for my_range:
    def my_range(start, stop=None, step=1):
        if stop is None:
            start, stop = 0, start
        current = start
        while current < stop:
            yield current
            current += step
    
  • Real-Time Sensor Data Simulator
    Build a generator that simulates real-time sensor readings (e.g., temperature, humidity). It should yield a new reading every second, with values varying randomly within a realistic range. Add an optional stop condition to terminate the generator.
    Why generators fit: Generators can run indefinitely (until stopped) and produce values in a stream, making them perfect for simulating real-time data feeds.
    Snippet to get started:

    import time
    import random
    
    def sensor_simulator(stop_after_seconds=60):
        start_time = time.time()
        while time.time() - start_time < stop_after_seconds:
            temperature = round(random.uniform(20.0, 30.0), 2)
            humidity = round(random.uniform(40.0, 60.0), 2)
            yield {'temperature': temperature, 'humidity': humidity}
            time.sleep(1)
    

内容的提问来源于stack exchange,提问作者Jeff - Mci

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:49:02