gpt4 book ai didi

python - 过滤元组列表以包含最大值和最小值

转载 作者:行者123 更新时间:2023-11-28 20:26:16 26 4
gpt4 key购买 nike

我生成了一长串元组(格式如下)。列表中的每个元组的第一个元素是时间,第三个元素是事件。第二个成员总是相同的,并从我将不得不处理的其他类似列表中识别出该列表。元组有许多不同的第三个元素,每个元素在不同的时间值有多个条目,这是第一个元素。

我正在尝试过滤列表以删除每个事件(元组的第三个成员)的时间的最小值和最大值(元组中的第一项)。我尝试使用列表理解,但很快就搞糊涂了。

('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP1_G1.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP1_G1.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP1_G1.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP1_G1.575')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3567', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3600', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3800', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3800', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3800', 'VOLTAGE DEVIATION', 'HORIZ_G .575')
('1.3800', 'VOLTAGE DEVIATION', 'MEDBOWCO 115')
('1.3800', 'VOLTAGE DEVIATION', 'MEDBOWCO 115')
('1.3800', 'VOLTAGE DEVIATION', 'STNDPSVC 230')
('1.3800', 'VOLTAGE DEVIATION', 'STNDPSVC 230')
('1.3800', 'VOLTAGE DEVIATION', 'STNDPSVC 230')
('1.3800', 'VOLTAGE DEVIATION', 'STNDPSVC 230')
('1.3800', 'VOLTAGE DEVIATION', 'STNDPSVC 230')
('1.3800', 'VOLTAGE DEVIATION', 'STNDPSVC 230')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
'1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4267', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4267', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4267', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4267', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4267', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4833', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4833', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4833', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4833', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4833', 'VOLTAGE DEVIATION', 'HIPLN_G .575')

过滤后的结果是

('1.3433', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP2G23.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP1_G1.575')
('1.3467', 'VOLTAGE DEVIATION', 'DNLP1_G1.575')
('1.3533', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3600', 'VOLTAGE DEVIATION', 'DIFICULT 230')
('1.3600', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3800', 'VOLTAGE DEVIATION', '7MIHL G1.575')
('1.3800', 'VOLTAGE DEVIATION', 'HORIZ_G .575')
('1.3800', 'VOLTAGE DEVIATION', 'MEDBOWCO 115')
('1.3800', 'VOLTAGE DEVIATION', 'MEDBOWCO 115')
('1.3800', 'VOLTAGE DEVIATION', 'STNDPSVC 230')
('1.3800', 'VOLTAGE DEVIATION', 'STNDPSVC 230')
('1.3800', 'VOLTAGE DEVIATION', 'STNDPSVC 230')
('1.3867', 'VOLTAGE DEVIATION', 'MINERS 34.5')
('1.3900', 'VOLTAGE DEVIATION', 'MINERS 34.5')
'1.4233', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4267', 'VOLTAGE DEVIATION', 'FT CRK2 34.5')
('1.4800', 'VOLTAGE DEVIATION', 'HIPLN_G .575')
('1.4833', 'VOLTAGE DEVIATION', 'HIPLN_G .575')`

我正在尝试下面的代码,但出现错误。我对此很陌生,所以如果我做错了什么,请告诉我。代码中的 m1 是我从 findall 生成的元组列表。我在代码的顶部导入了 ast。

       m1 = re.findall(pattern1,wholefile)
m1=[ast.literal_eval(t) for t in m1]
m1=[(float(a),b,c) for a,b,c in m1]
keys=sorted({t[2] for t in m1})
for key in keys:
group=filter(lambda t: t[2]==key,m1)
print '{}:\n\tmax: {}\n\tmin: {}'.format(key,max(group),min(group))

最佳答案

将元组重构为字典让生活更轻松。

from collections import defaultdict

d = defaultdict(list)
for t,_,v in your_tuple_list:
d[v].append(t)

之后,d 有每个事件的键,以及该时间段的关联时间列表。

它看起来像这样(有点):

>>> d['DNLP2G23.575']
['1.3433'....]

现在问题变成了找到每个列表的最小值和最大值;使用 min()max()

很容易

一旦你完成了这些,你就会得到按照你想要的顺序排列的数据集;您可以将其转换回元组/列表/等。

如果您热衷于将列表转换为集合,这将消除重复时间并通过加快最小值/最大值来节省您一些时间;假设它是您必须计算的大量元组列表。

您还应该将时间转换为 float - 您可以在主循环中执行此操作:d[v].append(float(t))。这是为了确保最大值和最小值正常工作。

关于python - 过滤元组列表以包含最大值和最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12304587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com