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

如何读取列表中的两个元素?相关Python代码技术咨询

Fixing Your Python Code for Reading List Elements & Generating Candlestick Charts

Let’s walk through the problems in your current code and fix them to get the functionality you need:

Key Issues in the Original Code

  • Incorrect indexing with zip: When you use zip(div, pares), each loop iteration returns a tuple of paired elements (one from div, one from pares)—not an index. Trying to use this tuple as an index (like pares[j]) will throw a TypeError because tuples can’t be used to index lists.
  • Variable name typo: You assigned dtm=pares[j] but then referenced dfm in the Candlestick call—this is a typo that would cause a NameError.
  • Unnecessary index lookup: Since zip already pairs elements directly, you don’t need to look up elements by index at all.

Corrected Code

import plotly.graph_objects as go

def ch1(div, pares):
    # Unpack paired elements directly from zip()
    for etiqueta, dfm in zip(div, pares):
        # Create candlestick figure with the paired label and DataFrame
        fig = go.Figure(data=[go.Candlestick(
            x=dfm['Date'],
            open=dfm['Open'],
            high=dfm['High'],
            low=dfm['Low'],
            close=dfm['Price']
        )])
        fig.update_layout(
            title=etiqueta,
        )
        fig.show()

What Changed?

  • We replaced for j in zip(div,pares): with for etiqueta, dfm in zip(div,pares):—this directly unpacks each pair into a label (etiqueta from div) and a DataFrame (dfm from pares), which is exactly what you need for each chart.
  • Removed the incorrect index-based assignments (dtm=pares[j] and etiqueta=div[j]) since we’re already getting these values directly from the zip loop.
  • Fixed the implicit typo by using dfm consistently (assuming pares contains pandas DataFrames with the required columns).

Notes to Ensure It Works

  • Make sure every element in pares is a pandas DataFrame that has the columns Date, Open, High, Low, and Price.
  • Ensure div and pares are the same length—if one is longer than the other, zip will stop at the shorter list’s length.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:50:47