Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

python - Identifying the highest peak, and return the values that is low to the right and left of highest peak, by keeping the highest peak as reference

I have data where I need to return the values that are low by keeping the highest value or peak as a reference, there are many peaks, I need to return the lows of each peak and store them to a variable.

Sample data:

A=['6452', '6444', '6452', '6456', '6455', '6465', '6456', '6442', '6449', '6452', '6455', '6465', '6463', '6452', '6459', '6470', '6456', '6435', '6438', '6441', '6438', '6448', '6451', '6439', '6449', '6472', '6483', '6503', '6531', '6524', '6523', '6538', '6550', '6534', '6537', '6554', '6540', '6538', '6553', '6558', '6568', '6588', '6609', '6615', '6616', '6625', '6628', '6633', '6628', '6612', '6605', '6605', '6599', '6587', '6588', '6589', '6584', '6592', '6592', '6571', '6555', '6551', '6533', '6521', '6517', '6502', '6493', '6476', '6473', '6469', '6455', '6463', '6462', '6438', '6424', '6428', '6417', '6400', '6404', '6398', '6398', '6417', '6425', '6422', '6429', '6448', '6435', '6424', '6434', '6439', '6431', '6429', '6418', '6407', '6412', '6415', '6407', '6412', '6424', '6415', '6403', '6417', '6425', '6424', '6425', '6428', '6427', '6432', '6455', '6442', '6420', '6446', '6503', '6558', '6654', '6748', '6841', '6945', '7053', '7160', '7229', '7237', '7150', '7062', '7025', '6882', '6657', '6493', '6391', '6278', '6156', '6083', '6076', '6081', '6091', '6115', '6145', '6189', '6234', '6282', '6298', '6323', '6370', '6393', '6408', '6429', '6427', '6431', '6452', '6454', '6456', '6465', '6461', '6456', '6472', '6487', '6493', '6493', '6509', '6500', '6496', '6497', '6487', '6472', '6480', '6496', '6489', '6490', '6506', '6506', '6492', '6507', '6517', '6507', '6485', '6495', '6496', '6500', '6509', '6502', '6492', '6510', '6530', '6533', '6545', '6545', '6530', '6526', '6533', '6536', '6527', '6533', '6531', '6527', '6529', '6536', '6538', '6554', '6561', '6555', '6553', '6567', '6574', '6570', '6572', '6581', '6578', '6582', '6608', '6613', '6616', '6625', '6643', '6643', '6652', '6662', '6662', '6653', '6670', '6684', '6686', '6700', '6714', '6714', '6737', '6759', '6761', '6768', '6778', '6786', '6785', '6785', '6792', '6786', '6779', '6790', '6807', '6813', '6823', '6819', '6812', '6826', '6828', '6830', '6833', '6836', '6816', '6799', '6806', '6821', '6813', '6793', '6800', '6793', '6796', '6807', '6787', '6770', '6773', '6753', '6737', '6728', '6722', '6703', '6686', '6670', '6639', '6618', '6618', '6609', '6594', '6585', '6571', '6547', '6543', '6543', '6543', '6514', '6512', '6523', '6514', '6492', '6485', '6482', '6487', '6482', '6473', '6492', '6506', '6489', '6473', '6476', '6478', '6475', '6480', '6483', '6461', '6452', '6465', '6463', '6463', '6463', '6466', '6461', '6459', '6473', '6463', '6454', '6462', '6462', '6445', '6449', '6469', '6470', '6475', '6473', '6469', '6475', '6486', '6483', '6486', '6497', '6499', '6495', '6502', '6495', '6487', '6483', '6490', '6493', '6486', '6497', '6489', '6473', '6483', '6514', '6510', '6497', '6503', '6510', '6521', '6503', '6496', '6489', '6490', '6497', '6492', '6485', '6495', '6490', '6490', '6497', '6504', '6493', '6487', '6506', '6507', '6496', '6499', '6497', '6492', '6506', '6502', '6493', '6502', '6510', '6506', '6502', '6512', '6519', '6502', '6504', '6526', '6507', '6504', '6517', '6519', '6506', '6513', '6514', '6502', '6509', '6516', '6503', '6506', '6516', '6514', '6500', '6503', '6513', '6521', '6524', '6526', '6509', '6523', '6530', '6524', '6524', '6534', '6524', '6517', '6536', '6544', '6540', '6529', '6547', '6553', '6536', '6543', '6540', '6527', '6540', '6540', '6526', '6529', '6533', '6537', '6523', '6533', '6541', '6531', '6540', '6550', '6543', '6537', '6548', '6548', '6548', '6564', '6561', '6560', '6557', '6553', '6547', '6548', '6553', '6554', '6554', '6555', '6558', '6551', '6557', '6574', '6565', '6561', '6564', '6558', '6551', '6550', '6554', '6554', '6547', '6555', '6544', '6555', '6562', '6548', '6545', '6558', '6554', '6545', '6545', '6545', '6541', '6560', '6579', '6575', '6577', '6587', '6589', '6599', '6628', '6636', '6623', '6623', '6628', '6623', '6628', '6646', '6654', '6653', '6660', '6667', '6669', '6676', '6680', '6677', '6679', '6684', '6676', '6664', '6656', '6657', '6646', '6659', '6669', '6663', '6645', '6640', '6625', '6606', '6606', '6599', '6572', '6553', '6545', '6543', '6533', '6533', '6534', '6516', '6516', '6541', '6521', '6492', '6493', '6490', '6482', '6479', '6478', '6489', '6483', '6487', '6495', '6500', '6507', '6510', '6493', '6495', '6495', '6482', '6476', '6485', '6478', '6480', '6500', '6499', '6478', '6500', '6510', '6513', '6512', '6521', '6504', '6492', '6495', '6503', '6493', '6509', '6495', '6475', '6485', '6529', '6577', '6664', '6759', '6845', '6950', '7062', '7171', '7264', '7288', '7215', '7128', '7114', '7032', '6814', '6618', '6512', '6410', '6278', '6190', '6183', '6183', '6180', '6186', '6212', '6236', '6294', '6340', '6354', '6387', '6434', '6456', '6492', '6509', '6509', '6516', '6540', '6560', '6567', '6560', '6553', '6541', '6564', '6582', '6577', '6568', '6579', '6591', '6574', '6558', '6561', '6551', '6541', '6547', '6554', '6558', '6574', '6575', '6577', '6578', '6594', '6589', '6594', '6601', '6596', '6579', '6601', '6606', '6599', '6611', '6635', '6625', '6611', '6623', '6635', '6622', '6632', '6628', '6601', '6605', '6622', '6623', '6628', '6628', '6632', '6636', '6649', '6652', '6654', '6656', '6663', '6654', '6652', '6653', '6646', '6652', '6671', '6679', '6684', '6703', '6704', '6712', '6720', '6746', '6770', '6749', '6752', '6753', '6744', '6746', '6775', '6783', '6787', '6803', '6817', '6826', '6834', '6844', '6843', '6853', '6874', '6867', '6860', '6847', '6853', '6861', '6872', '6875', '6878', '6874', '6889', '6878', '6875', '6879', '6888', '6885', '6895', '6894', '6881', '6878', '6891', '6877', '6860', '6854', '6840', '6824', '6820', '6812', '6793', '6763', '6758', '6748', '6742', '6738', '6720', '6669', '6649', '6646', '6632', '6619', '6619', '6608', '6588', '6567', '6577', '6574', '6564', '6561', '6554', '6536', '6534', '6519', '6517', '6526', '6510', '6497', '6516', '6521', '6516', '6513', '6514', '6497', '6504', '6531', '6533', '6516', '6510', '6523', '6521', '6504', '6495', '6487', '6499', '6504', '6480', '6475', '6493', '6503', '6490', '6487', '6487', '6493', '6495', '6512', '6503', '6476', '6482', '6496', '6502', '6502', '6504', '6500', '6503', '6496', '6506', '6487', '6490', '6500', '6486', '6486', '6510', '6509', '6499', '6504', '6510', '6490', '6495', '6504', '6504', '6500', '6507', '6506', '6497', '6503', '6512', '6495', '6514', '6533', '6517', '6502', '6509', '6493', '6496', '6509', '6504', '6500', '6519', '6510', '6482', '6468', '6472', '6465', '6459', '6473', '6479', '6452', '6456', '6463', '6462', '6472', '6478', '6485', '6476', '6492', '6497', '6478', '6475', '6493', '6490', '6495', '6507', '6507', '6503', '6492', '6469', '6452', '6451', '6465', '6472', '6456', '6448', '6441', '6438', '6462', '6476', '6459', '6472', '6493', '6480', '6473', '6478', '6469', '6463', '6465', '6469', '6466', '6456', '6449', '6451', '6434', '6438', '6451', '6444', '6449', '6449', '6437', '6442', '6452', '6439', '6442', '6445', '6429', '6425', '6431', '6437', '6432', '6418', '6421', '6422', '6418', '6428', '6445', '6439', '6441', '6441', '6427', '6432', '6441', '6444', '6441', '6438', '6422', '6418', '6415', '6418', '6404', '6415', '6434', '6429', '6414', '6405', '6403', '6407', '6421', '6431', '6415', '6429', '6432', '6428', '6446', '6469', '6469', '6466', '6489', '6497', '6479', '6497', '6500', '6483', '6489', '6489', '6513', '6530', '6536', '6530', '6530', '6541', '6531', '6537', '6544', '6543', '6547', '6541', '6540', '6533', '6520', '6547', '6577', '6574', '6557', '6533', '6520', '6521', '6502', '6476', '6441', '6425', '6414', '6393', '6374', '6364', '6349', '6352', '6360', '6356', '6343', '6357', '6362', '6343', '6349', '6369', '6337', '6333', '6346', '6350', '6345', '6356', '6349', '6321', '6309', '6336', '6325', '6319', '6332', '6336', '6335', '6340', '6340', '6349', '6369', '6376', '6356', '6339', '6337', '6340', '6328', '6318', '6313', '6308', '6304', '6326', '6308', '6289', '6304', '6340', '6369', '6410', '6495', '6582', '6666', '6770', '6881', '6981', '7059', '7024', '6920', '6913', '6919', '6756', '6550', '6437', '6340', '6237', '6146', '6105', '6087', '6079', '6079', '6071', '6091', '6161', '6190', '6197', '6221', '6246', '6284', '6339', '6367', '6377', '6388', '6408', '6412', '6418', '6432', '6444', '6424', '6429', '6459', '6441', '6452', '6463', '6452', '6461', '6485', '6499', '6473', '6455', '6462', '6485', '6493', '6496', '6492', '6473', '6480', '6496', '6479', '6476', '6482', '6468', '6472', '6489', '6502', '6493', '6514', '6533', '6534', '6543', '6547', '6547', '6553', '6557', '6536', '6516', '6523', '6513', '6500', '6527', '6558', '6544', '6550', '6554', '6568', '6579', '6604', '6594', '6560', '6571', '6579', '6555', '6537', '6553', '6571', '6578', '6578', '6587', '6582', '6599', '6622', '6622', '6611', '6619', '6637', '6640', '6646', '6662', '6662', '6681', '6698', '6704', '6704', '6728', '6725', '6718', '6717', '6729', '6738', '6745', '6734', '6742', '6745', '6755', '6761', '6763', '6772', '6770', '6768', '6768', '6758', '6755', '6759', '6766', '6758', '6756', '6755', '6751', '6749', '6739', '6732', '6745', '6737', '6729', '6708', '6697', '6695', '6686', '6670', '6653', '6646', '6637', '6623', '6601', '6584', '6567', '6575', '6558', '6530', '6524', '6533', '6513', '6486', '6470', '6468', '6442', '6439', '6446', '6427', '6418', '6415', '6397', '6414', '6427', '6428', '6417', '6424', '6438', '6435', '6438', '6449', '6438', '6434', '6444', '6448', '6431', '6425', '6424', '6421', '6429', '6439', '6429', '6432', '6444', '6449', '6442', '6448', '6456', '6439', '6431', '6432', '6421', '6431', '6432', '6427', '6455', '6456', '6455', '6444', '6452', '6455', '6445', '6452', '6458', '6448', '6428', '6415', '6434', '6432', '6432', '6449', '6435', '6435', '6434', '6425', '6414', '6432', '6445', '6445', '6448', '6438', '6434', '6435', '6448', '6439', '6428', '6448', '6445', '6429', '6432', '6434', '6429', '6435', '6446', '6444', '6424', '6427', '6431', '6429', '6420', '6418', '6417', '6425', '6431', '6431', '6421', '6424', '6441', '6437', '6444', '6442', '6442', '6432', '6446', '6445', '6438', '6461', '6472', '6451', '6437', '6448', '6459', '6459', '6465', '6456', '6441', '6437', '6452', '6441', '6445', '6445', '6435', '6437', '6463', '6485', '6465', '6454', '6469', '6475', '6465', '6478', '6466', '6461', '6463', '6449', '6442', '6451', '6455', '6458', '6461', '6462', '6465', '6461', '6470', '6479', '6468', '6470', '6476', '6475', '6470', '6473', '6475', '6452', '6444', '6452', '6438', '6446', '6448', '6439', '6435', '6456', '6463', '6458', '6461', '6459', '6463', '6472', '6472', '6466', '6461', '6468', '6469', '6466', '6480', '6504', '6521', '6519', '6524', '6533', '6545', '6570', '6570', '6555', '6558', '6558', '6570', '6565', '6562', '6565', '6567', '6581', '6589', '6591', '6608', '6606', '6602', '6605', '6616', '6620', '6623', '6625', '6622', '6601', '6599', '6620', '6622', '6616', '6584', '6565', '6557', '6555', '6529', '6496', '6496', '6485', '6478', '6470', '6452', '6446', '6448', '6446', '6425', '6412', '6405', '6408', '6390', '6384', '6376', '6363', '6370', '6398', '6410', '6420', '6427', '6422', '6410', '6400', '6405', '6400', '6404', '6407', '6398', '6379', '6383', '6390', '6393', '6412', '6412', '6415', '6410', '6424', '6429', '6415', '6404', '6411', '6417', '6424', '6417', '6394', '6388', '6408', '6448', '6517', '6613', '6705', '6790', '6916', '7036', '7140', '7215', '7198', '7095', '7042', '7024', '6881', '6645', '6509', '6410', '6279', '6168', '6132', '6129', '6121', '6121', '6129', '6162', '6209', '6250', '6274', '6295', '6339', '6373', '6398', '6425', '6452', '6444', '6463', '6490', '6495', '6495', '6499', '6500', '6486', '6502', '6517', '6510', '6526', '6531', '6520', '6512', '6512', '6520', '6519', '6519', '6523', '6514', '6533', '6550'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The zip() function is very useful when you are looking for patterns of relative values within a list. It allows you to obtain pairings (or triples) of values that are next to each other.

def findHighPeaks(A,count):
    # positions of values that are top of hills or bottom of valleys
    pivots   = [i for a,(i,b),c in zip(A,enumerate(A[1:],1),A[2:])
                                if a>b and b<c or a<b and b>c]

    # merge pivot positions into triples
    # representing the low-high-low or high-low-high indexes
    iWaves = [(left,mid,right) for left,mid,right
                               in  zip([0]+pivots,pivots,pivots[1:]+[len(A)-1])]
    
    # extract only peak values from patterns of hills and valleys
    peaks  = [(A[left],A[mid],A[right]) for left,mid,right in iWaves
                                        if A[left]<A[mid]]

    # find the highest peaks, and put them in a set
    highPeaks = set(sorted(peaks,key=lambda p:p[1])[-count:])

    # Return high peaks, in original order
    return [p for p in peaks if p in highPeaks]

# A = big list
    
Q,R,S = zip(*findHighPeaks(A,6)) # large example
print(R) # (7237, 7288, 7215, 7242, 7368, 7469) peaks
print(Q) # (6420, 6475, 6388, 6381, 6502, 6659) Low on the left
print(S) # (6076, 6180, 6452, 6018, 6176, 6337) Low on the right

A = ['22','21','22','23','24','25','26','27','28','27','24','15','23',
   '22','32','30','28','20','27','17','22','28','42','40','35','22','16']

Q,R,S = zip(*findHighPeaks(A,3)) # small example
print(R) # ('28', '32', '42') peaks
print(Q) # ('21', '22', '17') Low on the left
print(S) # ('15', '20', '16') Low on the right

Here is a visual representation of the selection process, which is based on relative variations (increasing/decreasing).

                                  HI                                        
                                 '28'                                   
                              '27'  '27'      HI    HI                     HI   
                           '26'        '24'  '23'  '32'                   '42'
                       '25'               '15'  '22'  '30'     HI      '28'  '40'
                    '24'                   LO    LO     '28'  '27'  '22'        '35'  
                 '23'                                      '20'  '17'              '22' 
        '22'  '22'                                          LO    LO                  '16'
           '21'
            LO

peaks:                           28           23    32         27          42       
            ||-------------------^^-------||--^^-||-^^------||-^^-||-------^^----------||
left:       21                            15     22         20    17
right:                                    15     22         20    17                   16

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...