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

Python技术求助:使用values_index列表作为位置索引提取converted列表对应元素的指定区间内容

Hey there! Let's work through how to solve your problem of extracting subsets from your converted list using the indices in values_index.

Solution: Extract Subsets Using Corresponding Indices

First, let's recap your core requirement:

  • You have converted: a list where each element is a pandas Series (extracted from Excel columns)
  • You have values_index: a list of integers, where the i-th value is the starting index for the i-th element in converted
  • You need to extract, for each pair, the portion of the Series starting at the given index and going to the end

Implementation Code

Your existing code already correctly generates values_index and converted, so we just need to add the extraction logic. Here are two straightforward approaches:

Approach 1: List Comprehension (Concise & Efficient)

This is the cleanest way to map each index to its corresponding Series subset:

# Extract subsets using paired indices and Series
extracted_results = [series.iloc[idx:] for series, idx in zip(converted, values_index)]

# Print to verify the results
for idx, subset in enumerate(extracted_results):
    print(f"Subset {idx+1}:\n{subset}\n")

Approach 2: For Loop (More Intuitive for Debugging)

If you prefer a step-by-step loop for easier troubleshooting:

extracted_results = []
for i in range(len(converted)):
    current_series = converted[i]
    start_index = values_index[i]
    # Slice from start_index to the end of the Series
    subset = current_series.iloc[start_index:]
    extracted_results.append(subset)

# Print the results
for idx, subset in enumerate(extracted_results):
    print(f"Subset {idx+1}:\n{subset}\n")

Key Notes

  • zip(converted, values_index) pairs each Series in converted with its matching starting index from values_index
  • iloc[idx:] is pandas syntax for slicing a Series: it takes all elements from position idx to the end
  • Based on your output, your values_index values (like 40, 62) are all valid for their corresponding Series lengths (66, 103, etc.), so you won't hit index errors here

Full Integrated Code

Add the extraction logic to the end of your existing code like this:

import os
import pandas as pd
import numpy as np

# Replace with your actual file path
path = "./"  
filelist = os.listdir(path)
filelist = sorted(filelist, key=lambda x: int(os.path.splitext(x)[0]))
print(filelist)

asps = []
for file in filelist:
    if file.endswith('.xlsx'):
        df = pd.read_excel(file)
        asps.append(df)
print(asps)

speed = []
average = []
for table in asps:
    speed.append(table['Reversal Intensities'].iloc[15:25])
print(speed)

for each in speed:
    average.append(np.mean(each))
print(average)

rev_ind = []
for each in asps:
    rev_ind.append(each['Reversal Indices'].iloc[15:25])
print(rev_ind)

c = []
for every in rev_ind:
    b = int(min(every))
    c.append(b)
print(c)

values_index = c
converted = []
for values in asps:
    converted.append(values['conv'].iloc[0:-1])
print(converted)

# --------------------------
# New extraction logic here
# --------------------------
extracted_results = [series.iloc[idx:] for series, idx in zip(converted, values_index)]

print("\n=== Extracted Subsets ===")
for i, res in enumerate(extracted_results):
    print(f"\nSubset {i+1}:")
    print(res)

This will give you exactly the subsets you need from each element in converted.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 00:08:11