如何在2个列表间按条件取值?
import itertoolslist01=[12.83, 13.17, 13.09, 13.49, 13.61, 13.68, 13.81, 14.01, 14.08, 14.03,
14.32, 14.27, 14.3, 14.32, 14.36, 14.34, 14.35, 14.38, 14.41, 14.72,
15.01, 15.06, 14.89, 14.5, 14.6, 14.56, 13.96, 13.81, 13.9, 13.82,
13.83, 13.95, 13.91, 13.86, 13.95, 13.91, 14.69, 15.09, 15.04, 15.05,
15.02, 15.01, 14.92, 15.04, 15.15, 15.01, 15.12, 15.06, 15.16, 15.08,
15.13, 15, 14.57, 14.48, 14.54, 14.49, 14.64, 14.61, 14.66, 14.57,
14.71, 14.66, 14.1, 13.88, 14.01]
#按奇偶切开
list11 = list01[::2]
list12 = list01[1::2]
list21 = [0] + [1 if i<j else 0 for i,j in zip(list11, list11[1:])] #后值与前值比较大小
list22 = [0] + [1 if i<j else 0 for i,j in zip(list12, list12[1:])]
list31 = list(itertools.zip_longest(list11, list21, fillvalue=0))
list32 = list(itertools.zip_longest(list12, list22, fillvalue=0))
'''
list31 list32
0 (12.83, 0) (13.17, 0)
1 (13.09, 1) (13.49, 1) list31 13.09>12.83 满足条件,取值13.09 直到list32 11# 14.5< 15.06 结束 取值 14.5
2 (13.61, 1) (13.68, 1)
3 (13.81, 1) (14.01, 1)
4 (14.08, 1) (14.03, 1)
5 (14.32, 1) (14.27, 1)
6 (14.3, 0) (14.32, 1)
7 (14.36, 1) (14.34, 1)
8 (14.35, 0) (14.38, 1)
9 (14.41, 1) (14.72, 1)
10 (15.01, 1) (15.06, 1)
11 (14.89, 0) (14.5, 0) ( 13.09 14.5)
12 (14.6, 0) (14.56, 1)
13 (13.96, 0) (13.81, 0)
14 (13.9, 0) (13.82, 1)
15 (13.83, 0) (13.95, 1)
16 (13.91, 1) (13.86, 0) 再开始取值 13.91
17 (13.95, 1) (13.91, 1)
18 (14.69, 1) (15.09, 1)
19 (15.04, 1) (15.05, 0) 结束 (13.91 15.05)
20 (15.02, 0) (15.01, 0)
21 (14.92, 0) (15.04, 1)
22 (15.15, 1) (15.01, 0) 再取值 15.15
23 (15.12, 0) (15.06, 1)
24 (15.16, 1) (15.08, 1)
25 (15.13, 0) (15, 0) 结束 (15.15 15)
26 (14.57, 0) (14.48, 0)
27 (14.54, 0) (14.49, 1)
28 (14.64, 1) (14.61, 1) 再 取值 14.64
29 (14.66, 1) (14.57, 0) 结束 (14.64 14.57 )
30 (14.71, 1) (14.66, 1) 再 取值 14.71
31 (14.1, 0) (13.88, 0) 结束 (14.71 13.88 )
32 (14.01, 0)
返回结果
( 13.09 14.5)
(13.91 15.05)
(15.15 15)
(14.64 14.57 )
(14.71 13.88 )
[此贴子已经被作者于2021-5-18 16:45编辑过]