【发布时间】:2022-01-10 21:50:54
【问题描述】:
我正在尝试通过几个嵌套列表进行迭代和分析。通常,我开始的列表包含 200 多个子列表:
[
[
1499040000000, // Open time
"0.01634790", // Open
"0.80000000", // High
"0.01575800", // Low
"0.01577100", // Close
"148976.11427815", // Volume
1499644799999, // Close time
"2434.19055334", // Quote asset volume
308, // Number of trades
"1756.87402397", // Taker buy base asset volume
"28.46694368", // Taker buy quote asset volume
"17928899.62484339" // Ignore.
]
]
我想遍历该嵌套列表的几个不同小节。例如。我只想迭代和分析列表的最后一个季度或后半部分。
从这些小节中,我想确定值“高”的最大值,即索引 2。
这是我尝试过的:
import itertools
twentyfour_hour_klines = initial list of sublists
#last 6 hours:
lookback_period = int('6')
six_hour_highest_high = get_highest_high(klines=twentyfour_hour_klines, lookback_period=lookback_period)
print(six_hour_highest_high, flush=True)
def get_highest_high(klines, lookback_period):
start = int(len(klines) / 24 * (24 - lookback_period) + 1)
stop = int(len(klines) + 1)
highest_high = None
for line in itertools.islice(klines , start, stop):
if highest_high == None:
highest_high = float(line[2])
elif float(line[2]) > highest_high:
highest_high = float(line[2])
return highest_high
它有效,但它似乎是一个相当笨重的解决方案。还有比这更瘦的吗?还请记住,我需要多次执行计算,速度是一个问题。
【问题讨论】:
-
此数据看起来可以在
pandas.DataFrame中很好地工作,其中pandas还提供了根据您的请求查看特定数据片段的方法。 -
谢谢,@Kraigolas。 panda 似乎确实是满足我要求的合适工具。只需查看文档,即可: # Import pandas library import pandas as pd # 初始化列表列表 data = [['DS', 'Linked_list', 10], ['DS', 'Stack', 9], [ 'DS', 'Queue', 7], ['Algo', 'Greedy', 8], ['Algo', 'DP', 6], ['Algo', 'BackTrack', 5], ] # 创建pandas DataFrame df = pd.DataFrame(data, columns = ['Category', 'Name', 'Marks']) 连同 this: titanic.iloc[9:25, 2:5] 应该可以解决问题。当我找到它并更新我的问题时,我会检查它。
标签: python loops max nested-lists