import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import find_peaks
import pdb
file = open("Data1r2.txt", 'r')
lines = file.readlines()
file.close()
t = []
z = []
y = []
x = []
with open("Data1r2.txt", 'r') as f:
for line in f:
parts = line.split(", ")
x.append(float(parts[0][2:]))
y.append(float(parts[1][2:]))
z.append(float(parts[2][2:]))
t.append(float(parts[3][2:]))
This part im mainly annotating the highest peak value of the graph, but how can i annotate all peak values at a fixed distance? say distance = 10,000
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot(t, z)
ymax = max(z)
xpos = z.index(ymax)
xmax = t[xpos]
text= "x={:.1f}, y={:.1f}".format(xmax, ymax) #Annotation(correct)
ax.annotate(text, xy=(xmax, ymax), xytext=(xmax, ymax),
arrowprops=dict(facecolor='black', shrink=0.05),
)
plt.legend()
plt.show()
This is what i currently have annotating only the peak value:
Data
X:-241, Y:-31, Z:17, T:73823
X:-241, Y:-31, Z:17, T:73952
X:-240, Y:-30, Z:26, T:74073
X:-240, Y:-30, Z:26, T:74191
X:-240, Y:-30, Z:26, T:74312
X:-240, Y:-32, Z:39, T:74432
X:-240, Y:-32, Z:39, T:74549
X:-240, Y:-32, Z:39, T:74668
X:-239, Y:-21, Z:12, T:74785
X:-239, Y:-21, Z:12, T:74904
X:-239, Y:-21, Z:12, T:75022
X:-246, Y:15, Z:18, T:75142
X:-246, Y:15, Z:18, T:75260
X:-246, Y:15, Z:18, T:75378
X:-250, Y:19, Z:14, T:75498
X:-250, Y:19, Z:14, T:75615
X:-250, Y:19, Z:14, T:75732
X:-239, Y:-5, Z:27, T:75854
X:-239, Y:-5, Z:27, T:75972
X:-239, Y:-5, Z:27, T:76102
X:-236, Y:-19, Z:46, T:76240
X:-236, Y:-19, Z:46, T:76369
X:-236, Y:-19, Z:46, T:76489
X:-235, Y:-14, Z:32, T:76610
X:-235, Y:-14, Z:32, T:76727
X:-235, Y:-14, Z:32, T:76845
X:-244, Y:-16, Z:22, T:76963
X:-244, Y:-16, Z:22, T:77081
X:-244, Y:-16, Z:22, T:77201
X:-220, Y:-25, Z:-3, T:77346
X:-220, Y:-25, Z:-3, T:77464
X:-220, Y:-25, Z:-3, T:77580
X:-229, Y:24, Z:2, T:77699
X:-229, Y:24, Z:2, T:77814
X:-229, Y:24, Z:2, T:77934
X:-248, Y:-20, Z:0, T:78052
X:-248, Y:-20, Z:0, T:78171
X:-248, Y:-20, Z:0, T:78288
X:-242, Y:-15, Z:-35, T:78515
X:-242, Y:-15, Z:-35, T:78630
X:-242, Y:-15, Z:-35, T:78747
X:-235, Y:-12, Z:-63, T:78865
X:-235, Y:-12, Z:-63, T:78982
X:-235, Y:-12, Z:-63, T:79102
X:-226, Y:-35, Z:-145, T:79221
X:-226, Y:-35, Z:-145, T:79340
X:-226, Y:-35, Z:-145, T:79461
X:-205, Y:-47, Z:-156, T:79582
X:-205, Y:-47, Z:-156, T:79702
X:-205, Y:-47, Z:-156, T:79821
X:-208, Y:-39, Z:-149, T:79940
X:-208, Y:-39, Z:-149, T:80061
X:-208, Y:-39, Z:-149, T:80181
X:-235, Y:-16, Z:-99, T:80304
X:-235, Y:-16, Z:-99, T:80432
X:-235, Y:-16, Z:-99, T:80657
X:-247, Y:-10, Z:12, T:80774
X:-247, Y:-10, Z:12, T:80890
X:-247, Y:-10, Z:12, T:81008
X:-242, Y:-1, Z:2, T:81127
X:-242, Y:-1, Z:2, T:81246
X:-242, Y:-1, Z:2, T:81363
X:-239, Y:-8, Z:15, T:81483
X:-239, Y:-8, Z:15, T:81600
X:-239, Y:-8, Z:15, T:81720
X:-241, Y:-13, Z:-11, T:81841
X:-241, Y:-13, Z:-11, T:81958
X:-241, Y:-13, Z:-11, T:82076
X:-242, Y:-5, Z:-37, T:82198
X:-242, Y:-5, Z:-37, T:82315
X:-242, Y:-5, Z:-37, T:82435
X:-215, Y:-43, Z:-128, T:82554
X:-215, Y:-43, Z:-128, T:82699
X:-215, Y:-43, Z:-128, T:82829
X:-207, Y:-48, Z:-153, T:82952
X:-207, Y:-48, Z:-153, T:83072
X:-207, Y:-48, Z:-153, T:83191
X:-198, Y:-37, Z:-166, T:83315
X:-198, Y:-37, Z:-166, T:83453
X:-198, Y:-37, Z:-166, T:83572
X:-218, Y:-33, Z:-134, T:83694
X:-218, Y:-33, Z:-134, T:83812
X:-218, Y:-33, Z:-134, T:83932
X:-228, Y:-15, Z:-80, T:84047
X:-228, Y:-15, Z:-80, T:84166
X:-228, Y:-15, Z:-80, T:84288
X:-243, Y:-8, Z:-4, T:84407
X:-243, Y:-8, Z:-4, T:84524
X:-243, Y:-8, Z:-4, T:84640
X:-238, Y:-4, Z:2, T:84756
X:-238, Y:-4, Z:2, T:84872
X:-238, Y:-4, Z:2, T:84994
X:-252, Y:-7, Z:-16, T:85136
X:-252, Y:-7, Z:-16, T:85265
X:-252, Y:-7, Z:-16, T:85385
X:-243, Y:-3, Z:-28, T:85504
X:-243, Y:-3, Z:-28, T:85618
X:-243, Y:-3, Z:-28, T:85739
X:-241, Y:-3, Z:-48, T:85858
X:-241, Y:-3, Z:-48, T:85975
X:-241, Y:-3, Z:-48, T:86094
X:-231, Y:-15, Z:-112, T:86216
X:-231, Y:-15, Z:-112, T:86334
X:-231, Y:-15, Z:-112, T:86453
X:-210, Y:-43, Z:-150, T:86573
X:-210, Y:-43, Z:-150, T:86691
X:-210, Y:-43, Z:-150, T:86811
X:-193, Y:-58, Z:-169, T:86933
X:-193, Y:-58, Z:-169, T:87051
X:-193, Y:-58, Z:-169, T:87171
X:-182, Y:-27, Z:-179, T:87305
X:-182, Y:-27, Z:-179, T:87435
X:-182, Y:-27, Z:-179, T:87566
X:-212, Y:-19, Z:-136, T:87686
X:-212, Y:-19, Z:-136, T:87803
X:-212, Y:-19, Z:-136, T:87920
X:-233, Y:-25, Z:-83, T:88040
X:-233, Y:-25, Z:-83, T:88160
X:-233, Y:-25, Z:-83, T:88278
X:-243, Y:-16, Z:-31, T:88396
X:-243, Y:-16, Z:-31, T:88510
X:-243, Y:-16, Z:-31, T:88625
X:-244, Y:-13, Z:-27, T:88744
X:-244, Y:-13, Z:-27, T:88860
X:-244, Y:-13, Z:-27, T:88978
X:-243, Y:-15, Z:-51, T:89099
X:-243, Y:-15, Z:-51, T:89218
X:-243, Y:-15, Z:-51, T:89338
X:-228, Y:-27, Z:-78, T:89472
X:-228, Y:-27, Z:-78, T:89601
X:-228, Y:-27, Z:-78, T:89746
X:-223, Y:-24, Z:-114, T:89876
X:-223, Y:-24, Z:-114, T:89995
X:-223, Y:-24, Z:-114, T:90115
X:-205, Y:-42, Z:-141, T:90236
X:-205, Y:-42, Z:-141, T:90354
X:-205, Y:-42, Z:-141, T:90474
X:-199, Y:-67, Z:-153, T:90595
X:-199, Y:-67, Z:-153, T:90713
X:-199, Y:-67, Z:-153, T:90833
X:-202, Y:-53, Z:-152, T:90951
X:-202, Y:-53, Z:-152, T:91069
X:-202, Y:-53, Z:-152, T:91191
X:-224, Y:-41, Z:-135, T:91312
X:-224, Y:-41, Z:-135, T:91431
X:-224, Y:-41, Z:-135, T:91549
X:-229, Y:-29, Z:-91, T:91669
X:-229, Y:-29, Z:-91, T:91789
X:-229, Y:-29, Z:-91, T:91923
X:-242, Y:-8, Z:-2, T:92066
X:-242, Y:-8, Z:-2, T:92184
X:-242, Y:-8, Z:-2, T:92302
X:-233, Y:-12, Z:-5, T:92420
X:-233, Y:-12, Z:-5, T:92534
X:-233, Y:-12, Z:-5, T:92654
X:-246, Y:-1, Z:-4, T:92773
X:-246, Y:-1, Z:-4, T:92892
X:-246, Y:-1, Z:-4, T:93010
X:-242, Y:-9, Z:-23, T:93130
X:-242, Y:-9, Z:-23, T:93251
X:-242, Y:-9, Z:-23, T:93370
X:-237, Y:-19, Z:-46, T:93491
X:-237, Y:-19, Z:-46, T:93608
X:-237, Y:-19, Z:-46, T:93727
X:-213, Y:-23, Z:-95, T:93849
X:-213, Y:-23, Z:-95, T:93966
X:-213, Y:-23, Z:-95, T:94112
X:-207, Y:-36, Z:-151, T:94241
X:-207, Y:-36, Z:-151, T:94359
X:-207, Y:-36, Z:-151, T:94480
X:-199, Y:-49, Z:-162, T:94600
X:-199, Y:-49, Z:-162, T:94721
X:-199, Y:-49, Z:-162, T:94840
X:-203, Y:-36, Z:-146, T:94961
X:-203, Y:-36, Z:-146, T:95082
X:-203, Y:-36, Z:-146, T:95202
X:-222, Y:-28, Z:-124, T:95324
X:-222, Y:-28, Z:-124, T:95439
X:-222, Y:-28, Z:-124, T:95583
X:-244, Y:2, Z:-53, T:95700
X:-244, Y:2, Z:-53, T:95817
X:-244, Y:2, Z:-53, T:95935
X:-237, Y:-5, Z:-9, T:96055
X:-237, Y:-5, Z:-9, T:96171
X:-237, Y:-5, Z:-9, T:96301
X:-239, Y:-2, Z:1, T:96439
X:-239, Y:-2, Z:1, T:96568
X:-239, Y:-2, Z:1, T:96685
X:-243, Y:-4, Z:2, T:96805
X:-243, Y:-4, Z:2, T:96919
X:-243, Y:-4, Z:2, T:97037
X:-246, Y:-3, Z:-16, T:97159
X:-246, Y:-3, Z:-16, T:97276
X:-246, Y:-3, Z:-16, T:97395
X:-239, Y:-8, Z:-42, T:97513
X:-239, Y:-8, Z:-42, T:97631
X:-239, Y:-8, Z:-42, T:97752
X:-221, Y:-10, Z:-115, T:97871
X:-221, Y:-10, Z:-115, T:97990
X:-221, Y:-10, Z:-115, T:98109
X:-219, Y:-25, Z:-145, T:98230
X:-219, Y:-25, Z:-145, T:98350
X:-219, Y:-25, Z:-145, T:98468
X:-202, Y:-31, Z:-172, T:98589
X:-202, Y:-31, Z:-172, T:98736
X:-202, Y:-31, Z:-172, T:98865
X:-214, Y:-34, Z:-144, T:98985
X:-214, Y:-34, Z:-144, T:99101
X:-214, Y:-34, Z:-144, T:99223
X:-224, Y:-24, Z:-116, T:99342
X:-224, Y:-24, Z:-116, T:99460
X:-224, Y:-24, Z:-116, T:99579
X:-232, Y:2, Z:-50, T:99699
X:-232, Y:2, Z:-50, T:99818
X:-232, Y:2, Z:-50, T:99936
X:-241, Y:-4, Z:-22, T:100056
X:-241, Y:-4, Z:-22, T:100175
X:-241, Y:-4, Z:-22, T:100293
X:-240, Y:4, Z:-2, T:100414
X:-240, Y:4, Z:-2, T:100532
X:-240, Y:4, Z:-2, T:100648
X:-241, Y:3, Z:1, T:100768
X:-241, Y:3, Z:1, T:100895
X:-241, Y:3, Z:1, T:101029
X:-243, Y:1, Z:-16, T:101160
X:-243, Y:1, Z:-16, T:101278
X:-243, Y:1, Z:-16, T:101399
X:-239, Y:-2, Z:-36, T:101518
X:-239, Y:-2, Z:-36, T:101661
X:-239, Y:-2, Z:-36, T:101780
X:-228, Y:-12, Z:-71, T:101901
X:-228, Y:-12, Z:-71, T:102019
X:-228, Y:-12, Z:-71, T:102138
X:-224, Y:-23, Z:-118, T:102260
X:-224, Y:-23, Z:-118, T:102378
X:-224, Y:-23, Z:-118, T:102498
X:-209, Y:-2, Z:-161, T:102617
X:-209, Y:-2, Z:-161, T:102735
X:-209, Y:-2, Z:-161, T:102855
X:-206, Y:-3, Z:-150, T:102974
X:-206, Y:-3, Z:-150, T:103088
X:-206, Y:-3, Z:-150, T:103216
X:-218, Y:0, Z:-142, T:103355
X:-218, Y:0, Z:-142, T:103469
X:-218, Y:0, Z:-142, T:103581
X:-226, Y:-17, Z:-118, T:103700
X:-226, Y:-17, Z:-118, T:103814
X:-226, Y:-17, Z:-118, T:103931
X:-242, Y:4, Z:-40, T:104054
X:-242, Y:4, Z:-40, T:104171
X:-242, Y:4, Z:-40, T:104292
X:-242, Y:4, Z:-22, T:104410
X:-242, Y:4, Z:-22, T:104523
X:-242, Y:4, Z:-22, T:104642
X:-240, Y:5, Z:-3, T:104762
X:-240, Y:5, Z:-3, T:104879
X:-240, Y:5, Z:-3, T:104993
X:-244, Y:-2, Z:-6, T:105111
X:-244, Y:-2, Z:-6, T:105231
X:-244, Y:-2, Z:-6, T:105361
X:-244, Y:1, Z:-10, T:105497
X:-244, Y:1, Z:-10, T:105623
X:-244, Y:1, Z:-10, T:105744
X:-244, Y:-4, Z:-34, T:105865
X:-244, Y:-4, Z:-34, T:105981
X:-244, Y:-4, Z:-34, T:106101
X:-231, Y:-1, Z:-63, T:106222
X:-231, Y:-1, Z:-63, T:106341
X:-231, Y:-1, Z:-63, T:106462
X:-222, Y:-11, Z:-116, T:106580
X:-222, Y:-11, Z:-116, T:106698
X:-222, Y:-11, Z:-116, T:106818
X:-219, Y:-15, Z:-144, T:106938
X:-219, Y:-15, Z:-144, T:107058
X:-219, Y:-15, Z:-144, T:107174
X:-204, Y:-6, Z:-150, T:107297
X:-204, Y:-6, Z:-150, T:107410
X:-204, Y:-6, Z:-150, T:107528
X:-196, Y:-5, Z:-163, T:107665
X:-196, Y:-5, Z:-163, T:107802
X:-196, Y:-5, Z:-163, T:107935
X:-214, Y:-2, Z:-153, T:108066
X:-214, Y:-2, Z:-153, T:108186
X:-214, Y:-2, Z:-153, T:108306
X:-223, Y:-12, Z:-123, T:108422
X:-223, Y:-12, Z:-123, T:108544
X:-223, Y:-12, Z:-123, T:108661
X:-230, Y:7, Z:-52, T:108783
X:-230, Y:7, Z:-52, T:108900
X:-230, Y:7, Z:-52, T:109019
X:-241, Y:9, Z:-25, T:109139
X:-241, Y:9, Z:-25, T:109258
X:-241, Y:9, Z:-25, T:109375
X:-245, Y:4, Z:-12, T:109496
X:-245, Y:4, Z:-12, T:109612
X:-245, Y:4, Z:-12, T:109732
X:-242, Y:3, Z:-6, T:109852
X:-242, Y:3, Z:-6, T:109968
X:-242, Y:3, Z:-6, T:110098
X:-239, Y:-4, Z:-35, T:110243
X:-239, Y:-4, Z:-35, T:110362
X:-239, Y:-4, Z:-35, T:110484
X:-235, Y:6, Z:-65, T:110606
X:-235, Y:6, Z:-65, T:110722
X:-235, Y:6, Z:-65, T:110840
X:-215, Y:-14, Z:-117, T:110962
X:-215, Y:-14, Z:-117, T:111081
X:-215, Y:-14, Z:-117, T:111204
X:-224, Y:7, Z:-146, T:111324
X:-224, Y:7, Z:-146, T:111441
X:-224, Y:7, Z:-146, T:111561
X:-209, Y:-6, Z:-149, T:111679
X:-209, Y:-6, Z:-149, T:111799
X:-209, Y:-6, Z:-149, T:111919
X:-219, Y:-8, Z:-140, T:112038
X:-219, Y:-8, Z:-140, T:112157
X:-219, Y:-8, Z:-140, T:112274
X:-226, Y:-3, Z:-116, T:112405
X:-226, Y:-3, Z:-116, T:112540
X:-226, Y:-3, Z:-116, T:112669
X:-233, Y:2, Z:-76, T:112792
X:-233, Y:2, Z:-76, T:112909
X:-233, Y:2, Z:-76, T:113028
X:-237, Y:7, Z:-35, T:113148
X:-237, Y:7, Z:-35, T:113266
X:-237, Y:7, Z:-35, T:113386
X:-242, Y:5, Z:-15, T:113504
X:-242, Y:5, Z:-15, T:113624
X:-242, Y:5, Z:-15, T:113764
X:-244, Y:5, Z:-3, T:113884
X:-244, Y:5, Z:-3, T:113999
X:-244, Y:5, Z:-3, T:114118
X:-242, Y:3, Z:-7, T:114239
X:-242, Y:3, Z:-7, T:114357
X:-242, Y:3, Z:-7, T:114473
X:-241, Y:0, Z:-30, T:114595
X:-241, Y:0, Z:-30, T:114720
X:-241, Y:0, Z:-30, T:114867
X:-227, Y:-13, Z:-95, T:114989
X:-227, Y:-13, Z:-95, T:115104
X:-227, Y:-13, Z:-95, T:115224
X:-212, Y:-5, Z:-114, T:115343
X:-212, Y:-5, Z:-114, T:115462
X:-212, Y:-5, Z:-114, T:115579
X:-215, Y:-6, Z:-145, T:115701
X:-215, Y:-6, Z:-145, T:115819
X:-215, Y:-6, Z:-145, T:115937
X:-210, Y:5, Z:-142, T:116059
X:-210, Y:5, Z:-142, T:116176
X:-210, Y:5, Z:-142, T:116296
X:-222, Y:-19, Z:-145, T:116415
X:-222, Y:-19, Z:-145, T:116534
X:-222, Y:-19, Z:-145, T:116655
X:-231, Y:6, Z:-119, T:116775
X:-231, Y:6, Z:-119, T:116894
X:-231, Y:6, Z:-119, T:117023
I don't think there's something ready in matplotlib - the task of peak detection is hard and the notion of peak can vary greatly from application to application.
Since your data is relatively simple, you can try an approach inspired by a Schmitt trigger: look for recent high values but discard small oscillations. The (pseudo) code would be:
y_max = None
for y in data:
if y_max is None: # start tracking
y_max = y
if y > y_max: # update max value
y_max = y
if y < y_max * 0.9: # signal is too different from
add_label(y=y_max) # the peak - save the peak and
y_max = None # start looking for another one
Related
I'm trying to flatten a list that is the result of a zip between two lists. For the code I take 19 data values from 99 different web pages. When I zip the two codes, I put the header of each web page in front of the nested data lists of 19. Now I want to flatten the entire list, keeping the same order. I've tried many different flatten codes and nothing gets rid of the square brackets around each individual data list. Help! :)
I've tried itertools, chain, for loops removing subsets - everything that I found the last two days with google searches.
population_data = [('Billbrook', ['2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1357', '1476', '1452', '1398', '1428', '2176', '2198', '2097', '1992']), ('Billstedt', ['68256', '68064', '68461', '68512', '68119', '68115', '68573', '68936', '68982', '69085', '69306', '69261', '69110', '69570', '69806', '70100', '70234', '70539', '70355']), ('Borgfelde', ['6526', '6469', '6457', '6504', '6460', '6654', '6591', '6565', '6649', '6611', '6556', '6688', '6857', '7085', '6773', '6845', '7461', '7635', '7696']), ('HafenCity', ['-', '-', '-', '-', '-', '-', '-', '-', '760', '930', '1097', '1582', '1834', '1914', '2093', '2319', '3275', '3627', '4592']), ('Hamburg-Altstadt', ['1993', '1965', '1908', '1843', '1774', '1816', '1949', '2247', '1701', '1673', '1661', '1665', '1660', '1675', '1793', '1879', '2257', '2305', '2272']), ('Hamm', ['35737', '35854', '35793', '35536', '35195', '35443', '35985', '36228', '36662', '36810', '37243', '37607', '37763', '37520', '37948', '38515', '38136', '38716', '38773']), ('Hammerbrook', ['565', '572', '584', '494', '432', '418', '450', '435', '1697', '1673', '1703', '1896', '2119', '2170', '2244', '2454', '4067', '3939', '4323']), ('Horn', ['35522', '35350', '35910', '35971', '36291', '36302', '36490', '36890', '37351', '37677', '37635', '37882', '37599', '37614', '38296', '38253', '38549', '38569', '38799']), ('Kleiner Grasbrook und Steinwerder', ['1376', '1391', '1410', '1445', '1425', '1361', '1351', '1290', '1279', '1190', '1244', '1305', '1431', '1259', '1326', '1414', '1256', '1243', '1239']), ('Neustadt', ['11912', '12106', '11989', '12052', '11856', '11850', '11747', '11706', '11838', '11626', '11856', '12026', '12331', '12301', '12372', '12586', '12657', '12719', '12920']), ('Rothenburgsort', ['8006', '8047', '8063', '8092', '8181', '8180', '8241', '8660', '8938', '8650', '8637', '8751', '8876', '8992', '9092', '9138', '9137', '9225', '9268']), ('St.Georg', ['10191', '10239', '9940', '10298', '10245', '10388', '10551', '10586', '10666', '9980', '10279', '10443', '10554', '10466', '10682', '10736', '10814', '11055', '11384']), ('St.Pauli', ['26660', '26094', '26776', '26923', '26829', '27035', '27612', '27706', '21703', '21099', '21469', '21704', '22058', '22392', '22433', '22535', '22595', '22501', '22436']), ('Veddel', ['4693', '4603', '4592', '4708', '4776', '4875', '4927', '4944', '4909', '4852', '4847', '4856', '4836', '4709', '4707', '4704', '4632', '4667', '4632']), ('Waltershof und Finkenwerder', ['11833', '11685', '11689', '11637', '11578', '11600', '11634', '11633', '11709', '11666', '11690', '11709', '11718', '11662', '11721', '11733', '11729', '11765', '11808']), ('Wilhelmsburg', ['46125', '47180', '47857', '47847', '48322', '48957', '49132', '49851', '49803', '50091', '50472', '50731', '51171', '52372', '52892', '53764', '55074', '54169', '54068'])]
If I used itertools, for example, it cuts out the parentheses, but the square brackets always remain. I printed the length of the list and it's the number of neighborhood headers plus 1 for each data list.
You can start by unzipping population_data into a tuple of names and a list containing tuples of numbers.
We then iterate over both names and numbers and join them together in their own sublist
Then we finally we flatten all the sublists into one big list
population_data = [('Billbrook', ['2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1357', '1476', '1452', '1398', '1428', '2176', '2198', '2097', '1992']), ('Billstedt', ['68256', '68064', '68461', '68512', '68119', '68115', '68573', '68936', '68982', '69085', '69306', '69261', '69110', '69570', '69806', '70100', '70234', '70539', '70355']), ('Borgfelde', ['6526', '6469', '6457', '6504', '6460', '6654', '6591', '6565', '6649', '6611', '6556', '6688', '6857', '7085', '6773', '6845', '7461', '7635', '7696']), ('HafenCity', ['-', '-', '-', '-', '-', '-', '-', '-', '760', '930', '1097', '1582', '1834', '1914', '2093', '2319', '3275', '3627', '4592']), ('Hamburg-Altstadt', ['1993', '1965', '1908', '1843', '1774', '1816', '1949', '2247', '1701', '1673', '1661', '1665', '1660', '1675', '1793', '1879', '2257', '2305', '2272']), ('Hamm', ['35737', '35854', '35793', '35536', '35195', '35443', '35985', '36228', '36662', '36810', '37243', '37607', '37763', '37520', '37948', '38515', '38136', '38716', '38773']), ('Hammerbrook', ['565', '572', '584', '494', '432', '418', '450', '435', '1697', '1673', '1703', '1896', '2119', '2170', '2244', '2454', '4067', '3939', '4323']), ('Horn', ['35522', '35350', '35910', '35971', '36291', '36302', '36490', '36890', '37351', '37677', '37635', '37882', '37599', '37614', '38296', '38253', '38549', '38569', '38799']), ('Kleiner Grasbrook und Steinwerder', ['1376', '1391', '1410', '1445', '1425', '1361', '1351', '1290', '1279', '1190', '1244', '1305', '1431', '1259', '1326', '1414', '1256', '1243', '1239']), ('Neustadt', ['11912', '12106', '11989', '12052', '11856', '11850', '11747', '11706', '11838', '11626', '11856', '12026', '12331', '12301', '12372', '12586', '12657', '12719', '12920']), ('Rothenburgsort', ['8006', '8047', '8063', '8092', '8181', '8180', '8241', '8660', '8938', '8650', '8637', '8751', '8876', '8992', '9092', '9138', '9137', '9225', '9268']), ('St.Georg', ['10191', '10239', '9940', '10298', '10245', '10388', '10551', '10586', '10666', '9980', '10279', '10443', '10554', '10466', '10682', '10736', '10814', '11055', '11384']), ('St.Pauli', ['26660', '26094', '26776', '26923', '26829', '27035', '27612', '27706', '21703', '21099', '21469', '21704', '22058', '22392', '22433', '22535', '22595', '22501', '22436']), ('Veddel', ['4693', '4603', '4592', '4708', '4776', '4875', '4927', '4944', '4909', '4852', '4847', '4856', '4836', '4709', '4707', '4704', '4632', '4667', '4632']), ('Waltershof und Finkenwerder', ['11833', '11685', '11689', '11637', '11578', '11600', '11634', '11633', '11709', '11666', '11690', '11709', '11718', '11662', '11721', '11733', '11729', '11765', '11808']), ('Wilhelmsburg', ['46125', '47180', '47857', '47847', '48322', '48957', '49132', '49851', '49803', '50091', '50472', '50731', '51171', '52372', '52892', '53764', '55074', '54169', '54068'])]
#Unzip population data into list of names and numbers
name, *li = zip(*population_data)
#name = ('Billbrook', 'Billstedt', 'Borgfelde', 'HafenCity', 'Hamburg-Altstadt', '...
#li = [(['2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1l......
#Join the name and numbers in a list
res = [[name[idx]]+li[0][idx] for idx in range(len(name))]
#res = [['Billbrook', '2350', '2354', '2032', '1783', '1526', '1....
#Join all sublists together
concat_list = [j for i in res for j in i]
print(concat_list)
The output will be
['Billbrook', '2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1357', '1476', '1452', '1398', '1428', '2176', '2198', '2097', '1992', 'Billstedt', '68256', '68064', '68461', '68512', '68119', '68115', '68573', '68936', '68982', '69085', '69306', '69261', '69110', '69570', '69806', '70100', '70234', '70539', '70355', 'Borgfelde', '6526', '6469', '6457', '6504', '6460', '6654', '6591', '6565', '6649', '6611', '6556', '6688', '6857', '7085', '6773', '6845', '7461', '7635', '7696', 'HafenCity', '-', '-', '-', '-', '-', '-', '-', '-', '760', '930', '1097', '1582', '1834', '1914', '2093', '2319', '3275', '3627', '4592', 'Hamburg-Altstadt', '1993', '1965', '1908', '1843', '1774', '1816', '1949', '2247', '1701', '1673', '1661', '1665', '1660', '1675', '1793', '1879', '2257', '2305', '2272', 'Hamm', '35737', '35854', '35793', '35536', '35195', '35443', '35985', '36228', '36662', '36810', '37243', '37607', '37763', '37520', '37948', '38515', '38136', '38716', '38773', 'Hammerbrook', '565', '572', '584', '494', '432', '418', '450', '435', '1697', '1673', '1703', '1896', '2119', '2170', '2244', '2454', '4067', '3939', '4323', 'Horn', '35522', '35350', '35910', '35971', '36291', '36302', '36490', '36890', '37351', '37677', '37635', '37882', '37599', '37614', '38296', '38253', '38549', '38569', '38799', 'Kleiner Grasbrook und Steinwerder', '1376', '1391', '1410', '1445', '1425', '1361', '1351', '1290', '1279', '1190', '1244', '1305', '1431', '1259', '1326', '1414', '1256', '1243', '1239', 'Neustadt', '11912', '12106', '11989', '12052', '11856', '11850', '11747', '11706', '11838', '11626', '11856', '12026', '12331', '12301', '12372', '12586', '12657', '12719', '12920', 'Rothenburgsort', '8006', '8047', '8063', '8092', '8181', '8180', '8241', '8660', '8938', '8650', '8637', '8751', '8876', '8992', '9092', '9138', '9137', '9225', '9268', 'St.Georg', '10191', '10239', '9940', '10298', '10245', '10388', '10551', '10586', '10666', '9980', '10279', '10443', '10554', '10466', '10682', '10736', '10814', '11055', '11384', 'St.Pauli', '26660', '26094', '26776', '26923', '26829', '27035', '27612', '27706', '21703', '21099', '21469', '21704', '22058', '22392', '22433', '22535', '22595', '22501', '22436', 'Veddel', '4693', '4603', '4592', '4708', '4776', '4875', '4927', '4944', '4909', '4852', '4847', '4856', '4836', '4709', '4707', '4704', '4632', '4667', '4632', 'Waltershof und Finkenwerder', '11833', '11685', '11689', '11637', '11578', '11600', '11634', '11633', '11709', '11666', '11690', '11709', '11718', '11662', '11721', '11733', '11729', '11765', '11808', 'Wilhelmsburg', '46125', '47180', '47857', '47847', '48322', '48957', '49132', '49851', '49803', '50091', '50472', '50731', '51171', '52372', '52892', '53764', '55074', '54169', '54068']
Use:
from itertools import chain
population_data = [('Billbrook', ['2350', '2354', '2032', '1783', '1526', '1528', '1235', '1117', '1133', '1210', '1357', '1476', '1452', '1398', '1428', '2176', '2198', '2097', '1992']), ('Billstedt', ['68256', '68064', '68461', '68512', '68119', '68115', '68573', '68936', '68982', '69085', '69306', '69261', '69110', '69570', '69806', '70100', '70234', '70539', '70355']), ('Borgfelde', ['6526', '6469', '6457', '6504', '6460', '6654', '6591', '6565', '6649', '6611', '6556', '6688', '6857', '7085', '6773', '6845', '7461', '7635', '7696']), ('HafenCity', ['-', '-', '-', '-', '-', '-', '-', '-', '760', '930', '1097', '1582', '1834', '1914', '2093', '2319', '3275', '3627', '4592']), ('Hamburg-Altstadt', ['1993', '1965', '1908', '1843', '1774', '1816', '1949', '2247', '1701', '1673', '1661', '1665', '1660', '1675', '1793', '1879', '2257', '2305', '2272']), ('Hamm', ['35737', '35854', '35793', '35536', '35195', '35443', '35985', '36228', '36662', '36810', '37243', '37607', '37763', '37520', '37948', '38515', '38136', '38716', '38773']), ('Hammerbrook', ['565', '572', '584', '494', '432', '418', '450', '435', '1697', '1673', '1703', '1896', '2119', '2170', '2244', '2454', '4067', '3939', '4323']), ('Horn', ['35522', '35350', '35910', '35971', '36291', '36302', '36490', '36890', '37351', '37677', '37635', '37882', '37599', '37614', '38296', '38253', '38549', '38569', '38799']), ('Kleiner Grasbrook und Steinwerder', ['1376', '1391', '1410', '1445', '1425', '1361', '1351', '1290', '1279', '1190', '1244', '1305', '1431', '1259', '1326', '1414', '1256', '1243', '1239']), ('Neustadt', ['11912', '12106', '11989', '12052', '11856', '11850', '11747', '11706', '11838', '11626', '11856', '12026', '12331', '12301', '12372', '12586', '12657', '12719', '12920']), ('Rothenburgsort', ['8006', '8047', '8063', '8092', '8181', '8180', '8241', '8660', '8938', '8650', '8637', '8751', '8876', '8992', '9092', '9138', '9137', '9225', '9268']), ('St.Georg', ['10191', '10239', '9940', '10298', '10245', '10388', '10551', '10586', '10666', '9980', '10279', '10443', '10554', '10466', '10682', '10736', '10814', '11055', '11384']), ('St.Pauli', ['26660', '26094', '26776', '26923', '26829', '27035', '27612', '27706', '21703', '21099', '21469', '21704', '22058', '22392', '22433', '22535', '22595', '22501', '22436']), ('Veddel', ['4693', '4603', '4592', '4708', '4776', '4875', '4927', '4944', '4909', '4852', '4847', '4856', '4836', '4709', '4707', '4704', '4632', '4667', '4632']), ('Waltershof und Finkenwerder', ['11833', '11685', '11689', '11637', '11578', '11600', '11634', '11633', '11709', '11666', '11690', '11709', '11718', '11662', '11721', '11733', '11729', '11765', '11808']), ('Wilhelmsburg', ['46125', '47180', '47857', '47847', '48322', '48957', '49132', '49851', '49803', '50091', '50472', '50731', '51171', '52372', '52892', '53764', '55074', '54169', '54068'])]
result = []
for elem in chain.from_iterable(population_data):
if isinstance(elem, list): #Check if element is list object
result.extend(elem) #Extend result
else:
result.append(elem)
print(result)
I exactly do not know how do you want to flatten it, the below code will do row wise and column wise flattening.
# Flattening
flattened = list(map(lambda t : [t[0]] + t[1], population_data))
# Flattened data printing
for i in flattened:
print(i)
print('-' * 40)
# Transposed data printing
for k in zip(*flattened):
print(k)
try this:
flattened_list = []
x = [[name] + prices for name,prices in population_data]
for inner_list in x:
flattened_list.extend(inner_list)
print(flattened_list)
flat list can be made through reduce easily.
All you need to use initializer - third argument in the reduce function.
reduce(
lambda result, _list: result.append(_list[0]) or result.extend(_list[1]) or result,
population_data,
[])
Above code works for both python2 and python3, but you need to import reduce module as from functools import reduce. Refer below link for details.
for python2
for python3
I have issues getting my data to save properly into my csv. I have a few sets of arrays in x[zpeaks] however when i save data it only saves the last array and not all of them.
Like say for example my x[zpeaks] contains [1,2,1 ],[1,4,1],[1,3,5]. but when i wanna save all the arrays in the csv file it will only save the last array being [1,3,5].
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import find_peaks
import pdb
import pandas as pd
t = []
z = []
y = []
x = []
with open("Data1r2.txt", 'r') as f:
for line in f:
parts = line.split(", ")
x.append(float(parts[0][2:]))
y.append(float(parts[1][2:]))
z.append(float(parts[2][2:]))
t.append(float(parts[3][2:]))
zz = np.array(z)
tt = np.array(t)
zminvalue = np.min(zz)
zzz = zz - zminvalue
zpeaks, _ = find_peaks(zzz)
for i in range(len(zpeaks)-1):
print(z[zpeaks[i]:zpeaks[i+1]])
a = (x[zpeaks[i]:zpeaks[i+1]])
b = (y[zpeaks[i]:zpeaks[i+1]])
c = (z[zpeaks[i]:zpeaks[i+1]])
pd.concat([pd.DataFrame(a),pd.DataFrame(b), pd.DataFrame(c)], axis=1).to_csv('Diff.csv', mode='w')
My data.txt
X:-241, Y:-31, Z:17, T:73823
X:-241, Y:-31, Z:17, T:73952
X:-240, Y:-30, Z:26, T:74073
X:-240, Y:-30, Z:26, T:74191
X:-240, Y:-30, Z:26, T:74312
X:-240, Y:-32, Z:39, T:74432
X:-240, Y:-32, Z:39, T:74549
X:-240, Y:-32, Z:39, T:74668
X:-239, Y:-21, Z:12, T:74785
X:-239, Y:-21, Z:12, T:74904
X:-239, Y:-21, Z:12, T:75022
X:-246, Y:15, Z:18, T:75142
X:-246, Y:15, Z:18, T:75260
X:-246, Y:15, Z:18, T:75378
X:-250, Y:19, Z:14, T:75498
X:-250, Y:19, Z:14, T:75615
X:-250, Y:19, Z:14, T:75732
X:-239, Y:-5, Z:27, T:75854
X:-239, Y:-5, Z:27, T:75972
X:-239, Y:-5, Z:27, T:76102
X:-236, Y:-19, Z:46, T:76240
X:-236, Y:-19, Z:46, T:76369
X:-236, Y:-19, Z:46, T:76489
X:-235, Y:-14, Z:32, T:76610
X:-235, Y:-14, Z:32, T:76727
X:-235, Y:-14, Z:32, T:76845
X:-244, Y:-16, Z:22, T:76963
X:-244, Y:-16, Z:22, T:77081
X:-244, Y:-16, Z:22, T:77201
X:-220, Y:-25, Z:-3, T:77346
X:-220, Y:-25, Z:-3, T:77464
X:-220, Y:-25, Z:-3, T:77580
X:-229, Y:24, Z:2, T:77699
X:-229, Y:24, Z:2, T:77814
X:-229, Y:24, Z:2, T:77934
X:-248, Y:-20, Z:0, T:78052
X:-248, Y:-20, Z:0, T:78171
X:-248, Y:-20, Z:0, T:78288
X:-242, Y:-15, Z:-35, T:78515
X:-242, Y:-15, Z:-35, T:78630
X:-242, Y:-15, Z:-35, T:78747
X:-235, Y:-12, Z:-63, T:78865
X:-235, Y:-12, Z:-63, T:78982
X:-235, Y:-12, Z:-63, T:79102
X:-226, Y:-35, Z:-145, T:79221
X:-226, Y:-35, Z:-145, T:79340
X:-226, Y:-35, Z:-145, T:79461
X:-205, Y:-47, Z:-156, T:79582
X:-205, Y:-47, Z:-156, T:79702
X:-205, Y:-47, Z:-156, T:79821
X:-208, Y:-39, Z:-149, T:79940
X:-208, Y:-39, Z:-149, T:80061
X:-208, Y:-39, Z:-149, T:80181
X:-235, Y:-16, Z:-99, T:80304
X:-235, Y:-16, Z:-99, T:80432
X:-235, Y:-16, Z:-99, T:80657
X:-247, Y:-10, Z:12, T:80774
X:-247, Y:-10, Z:12, T:80890
X:-247, Y:-10, Z:12, T:81008
X:-242, Y:-1, Z:2, T:81127
X:-242, Y:-1, Z:2, T:81246
X:-242, Y:-1, Z:2, T:81363
X:-239, Y:-8, Z:15, T:81483
X:-239, Y:-8, Z:15, T:81600
X:-239, Y:-8, Z:15, T:81720
X:-241, Y:-13, Z:-11, T:81841
X:-241, Y:-13, Z:-11, T:81958
X:-241, Y:-13, Z:-11, T:82076
X:-242, Y:-5, Z:-37, T:82198
X:-242, Y:-5, Z:-37, T:82315
X:-242, Y:-5, Z:-37, T:82435
X:-215, Y:-43, Z:-128, T:82554
X:-215, Y:-43, Z:-128, T:82699
X:-215, Y:-43, Z:-128, T:82829
X:-207, Y:-48, Z:-153, T:82952
X:-207, Y:-48, Z:-153, T:83072
X:-207, Y:-48, Z:-153, T:83191
X:-198, Y:-37, Z:-166, T:83315
X:-198, Y:-37, Z:-166, T:83453
X:-198, Y:-37, Z:-166, T:83572
X:-218, Y:-33, Z:-134, T:83694
X:-218, Y:-33, Z:-134, T:83812
X:-218, Y:-33, Z:-134, T:83932
X:-228, Y:-15, Z:-80, T:84047
X:-228, Y:-15, Z:-80, T:84166
X:-228, Y:-15, Z:-80, T:84288
X:-243, Y:-8, Z:-4, T:84407
X:-243, Y:-8, Z:-4, T:84524
X:-243, Y:-8, Z:-4, T:84640
X:-238, Y:-4, Z:2, T:84756
X:-238, Y:-4, Z:2, T:84872
X:-238, Y:-4, Z:2, T:84994
X:-252, Y:-7, Z:-16, T:85136
X:-252, Y:-7, Z:-16, T:85265
X:-252, Y:-7, Z:-16, T:85385
X:-243, Y:-3, Z:-28, T:85504
X:-243, Y:-3, Z:-28, T:85618
X:-243, Y:-3, Z:-28, T:85739
X:-241, Y:-3, Z:-48, T:85858
X:-241, Y:-3, Z:-48, T:85975
X:-241, Y:-3, Z:-48, T:86094
X:-231, Y:-15, Z:-112, T:86216
X:-231, Y:-15, Z:-112, T:86334
X:-231, Y:-15, Z:-112, T:86453
X:-210, Y:-43, Z:-150, T:86573
X:-210, Y:-43, Z:-150, T:86691
X:-210, Y:-43, Z:-150, T:86811
X:-193, Y:-58, Z:-169, T:86933
X:-193, Y:-58, Z:-169, T:87051
X:-193, Y:-58, Z:-169, T:87171
X:-182, Y:-27, Z:-179, T:87305
X:-182, Y:-27, Z:-179, T:87435
X:-182, Y:-27, Z:-179, T:87566
X:-212, Y:-19, Z:-136, T:87686
X:-212, Y:-19, Z:-136, T:87803
X:-212, Y:-19, Z:-136, T:87920
X:-233, Y:-25, Z:-83, T:88040
X:-233, Y:-25, Z:-83, T:88160
X:-233, Y:-25, Z:-83, T:88278
X:-243, Y:-16, Z:-31, T:88396
X:-243, Y:-16, Z:-31, T:88510
X:-243, Y:-16, Z:-31, T:88625
X:-244, Y:-13, Z:-27, T:88744
X:-244, Y:-13, Z:-27, T:88860
X:-244, Y:-13, Z:-27, T:88978
X:-243, Y:-15, Z:-51, T:89099
X:-243, Y:-15, Z:-51, T:89218
X:-243, Y:-15, Z:-51, T:89338
X:-228, Y:-27, Z:-78, T:89472
X:-228, Y:-27, Z:-78, T:89601
X:-228, Y:-27, Z:-78, T:89746
X:-223, Y:-24, Z:-114, T:89876
X:-223, Y:-24, Z:-114, T:89995
X:-223, Y:-24, Z:-114, T:90115
X:-205, Y:-42, Z:-141, T:90236
X:-205, Y:-42, Z:-141, T:90354
X:-205, Y:-42, Z:-141, T:90474
X:-199, Y:-67, Z:-153, T:90595
X:-199, Y:-67, Z:-153, T:90713
X:-199, Y:-67, Z:-153, T:90833
X:-202, Y:-53, Z:-152, T:90951
X:-202, Y:-53, Z:-152, T:91069
X:-202, Y:-53, Z:-152, T:91191
X:-224, Y:-41, Z:-135, T:91312
X:-224, Y:-41, Z:-135, T:91431
X:-224, Y:-41, Z:-135, T:91549
X:-229, Y:-29, Z:-91, T:91669
X:-229, Y:-29, Z:-91, T:91789
X:-229, Y:-29, Z:-91, T:91923
X:-242, Y:-8, Z:-2, T:92066
X:-242, Y:-8, Z:-2, T:92184
X:-242, Y:-8, Z:-2, T:92302
X:-233, Y:-12, Z:-5, T:92420
X:-233, Y:-12, Z:-5, T:92534
X:-233, Y:-12, Z:-5, T:92654
X:-246, Y:-1, Z:-4, T:92773
X:-246, Y:-1, Z:-4, T:92892
X:-246, Y:-1, Z:-4, T:93010
X:-242, Y:-9, Z:-23, T:93130
X:-242, Y:-9, Z:-23, T:93251
X:-242, Y:-9, Z:-23, T:93370
X:-237, Y:-19, Z:-46, T:93491
X:-237, Y:-19, Z:-46, T:93608
X:-237, Y:-19, Z:-46, T:93727
X:-213, Y:-23, Z:-95, T:93849
X:-213, Y:-23, Z:-95, T:93966
X:-213, Y:-23, Z:-95, T:94112
X:-207, Y:-36, Z:-151, T:94241
X:-207, Y:-36, Z:-151, T:94359
X:-207, Y:-36, Z:-151, T:94480
X:-199, Y:-49, Z:-162, T:94600
X:-199, Y:-49, Z:-162, T:94721
X:-199, Y:-49, Z:-162, T:94840
X:-203, Y:-36, Z:-146, T:94961
X:-203, Y:-36, Z:-146, T:95082
X:-203, Y:-36, Z:-146, T:95202
X:-222, Y:-28, Z:-124, T:95324
X:-222, Y:-28, Z:-124, T:95439
X:-222, Y:-28, Z:-124, T:95583
X:-244, Y:2, Z:-53, T:95700
X:-244, Y:2, Z:-53, T:95817
X:-244, Y:2, Z:-53, T:95935
X:-237, Y:-5, Z:-9, T:96055
X:-237, Y:-5, Z:-9, T:96171
X:-237, Y:-5, Z:-9, T:96301
X:-239, Y:-2, Z:1, T:96439
X:-239, Y:-2, Z:1, T:96568
X:-239, Y:-2, Z:1, T:96685
X:-243, Y:-4, Z:2, T:96805
X:-243, Y:-4, Z:2, T:96919
X:-243, Y:-4, Z:2, T:97037
X:-246, Y:-3, Z:-16, T:97159
X:-246, Y:-3, Z:-16, T:97276
X:-246, Y:-3, Z:-16, T:97395
X:-239, Y:-8, Z:-42, T:97513
X:-239, Y:-8, Z:-42, T:97631
X:-239, Y:-8, Z:-42, T:97752
X:-221, Y:-10, Z:-115, T:97871
X:-221, Y:-10, Z:-115, T:97990
X:-221, Y:-10, Z:-115, T:98109
X:-219, Y:-25, Z:-145, T:98230
X:-219, Y:-25, Z:-145, T:98350
X:-219, Y:-25, Z:-145, T:98468
X:-202, Y:-31, Z:-172, T:98589
X:-202, Y:-31, Z:-172, T:98736
X:-202, Y:-31, Z:-172, T:98865
X:-214, Y:-34, Z:-144, T:98985
X:-214, Y:-34, Z:-144, T:99101
X:-214, Y:-34, Z:-144, T:99223
X:-224, Y:-24, Z:-116, T:99342
X:-224, Y:-24, Z:-116, T:99460
X:-224, Y:-24, Z:-116, T:99579
X:-232, Y:2, Z:-50, T:99699
X:-232, Y:2, Z:-50, T:99818
X:-232, Y:2, Z:-50, T:99936
X:-241, Y:-4, Z:-22, T:100056
X:-241, Y:-4, Z:-22, T:100175
X:-241, Y:-4, Z:-22, T:100293
X:-240, Y:4, Z:-2, T:100414
X:-240, Y:4, Z:-2, T:100532
X:-240, Y:4, Z:-2, T:100648
X:-241, Y:3, Z:1, T:100768
X:-241, Y:3, Z:1, T:100895
X:-241, Y:3, Z:1, T:101029
X:-243, Y:1, Z:-16, T:101160
X:-243, Y:1, Z:-16, T:101278
X:-243, Y:1, Z:-16, T:101399
X:-239, Y:-2, Z:-36, T:101518
X:-239, Y:-2, Z:-36, T:101661
X:-239, Y:-2, Z:-36, T:101780
X:-228, Y:-12, Z:-71, T:101901
X:-228, Y:-12, Z:-71, T:102019
X:-228, Y:-12, Z:-71, T:102138
X:-224, Y:-23, Z:-118, T:102260
X:-224, Y:-23, Z:-118, T:102378
X:-224, Y:-23, Z:-118, T:102498
X:-209, Y:-2, Z:-161, T:102617
X:-209, Y:-2, Z:-161, T:102735
X:-209, Y:-2, Z:-161, T:102855
X:-206, Y:-3, Z:-150, T:102974
X:-206, Y:-3, Z:-150, T:103088
X:-206, Y:-3, Z:-150, T:103216
X:-218, Y:0, Z:-142, T:103355
X:-218, Y:0, Z:-142, T:103469
X:-218, Y:0, Z:-142, T:103581
X:-226, Y:-17, Z:-118, T:103700
X:-226, Y:-17, Z:-118, T:103814
X:-226, Y:-17, Z:-118, T:103931
X:-242, Y:4, Z:-40, T:104054
X:-242, Y:4, Z:-40, T:104171
X:-242, Y:4, Z:-40, T:104292
X:-242, Y:4, Z:-22, T:104410
X:-242, Y:4, Z:-22, T:104523
X:-242, Y:4, Z:-22, T:104642
X:-240, Y:5, Z:-3, T:104762
X:-240, Y:5, Z:-3, T:104879
X:-240, Y:5, Z:-3, T:104993
X:-244, Y:-2, Z:-6, T:105111
X:-244, Y:-2, Z:-6, T:105231
X:-244, Y:-2, Z:-6, T:105361
X:-244, Y:1, Z:-10, T:105497
X:-244, Y:1, Z:-10, T:105623
X:-244, Y:1, Z:-10, T:105744
X:-244, Y:-4, Z:-34, T:105865
X:-244, Y:-4, Z:-34, T:105981
X:-244, Y:-4, Z:-34, T:106101
X:-231, Y:-1, Z:-63, T:106222
X:-231, Y:-1, Z:-63, T:106341
X:-231, Y:-1, Z:-63, T:106462
X:-222, Y:-11, Z:-116, T:106580
X:-222, Y:-11, Z:-116, T:106698
X:-222, Y:-11, Z:-116, T:106818
X:-219, Y:-15, Z:-144, T:106938
X:-219, Y:-15, Z:-144, T:107058
X:-219, Y:-15, Z:-144, T:107174
X:-204, Y:-6, Z:-150, T:107297
X:-204, Y:-6, Z:-150, T:107410
X:-204, Y:-6, Z:-150, T:107528
X:-196, Y:-5, Z:-163, T:107665
X:-196, Y:-5, Z:-163, T:107802
X:-196, Y:-5, Z:-163, T:107935
X:-214, Y:-2, Z:-153, T:108066
X:-214, Y:-2, Z:-153, T:108186
X:-214, Y:-2, Z:-153, T:108306
X:-223, Y:-12, Z:-123, T:108422
X:-223, Y:-12, Z:-123, T:108544
X:-223, Y:-12, Z:-123, T:108661
X:-230, Y:7, Z:-52, T:108783
X:-230, Y:7, Z:-52, T:108900
X:-230, Y:7, Z:-52, T:109019
X:-241, Y:9, Z:-25, T:109139
X:-241, Y:9, Z:-25, T:109258
X:-241, Y:9, Z:-25, T:109375
X:-245, Y:4, Z:-12, T:109496
X:-245, Y:4, Z:-12, T:109612
X:-245, Y:4, Z:-12, T:109732
X:-242, Y:3, Z:-6, T:109852
X:-242, Y:3, Z:-6, T:109968
X:-242, Y:3, Z:-6, T:110098
X:-239, Y:-4, Z:-35, T:110243
X:-239, Y:-4, Z:-35, T:110362
X:-239, Y:-4, Z:-35, T:110484
X:-235, Y:6, Z:-65, T:110606
X:-235, Y:6, Z:-65, T:110722
X:-235, Y:6, Z:-65, T:110840
X:-215, Y:-14, Z:-117, T:110962
X:-215, Y:-14, Z:-117, T:111081
X:-215, Y:-14, Z:-117, T:111204
X:-224, Y:7, Z:-146, T:111324
X:-224, Y:7, Z:-146, T:111441
X:-224, Y:7, Z:-146, T:111561
X:-209, Y:-6, Z:-149, T:111679
X:-209, Y:-6, Z:-149, T:111799
X:-209, Y:-6, Z:-149, T:111919
X:-219, Y:-8, Z:-140, T:112038
X:-219, Y:-8, Z:-140, T:112157
X:-219, Y:-8, Z:-140, T:112274
X:-226, Y:-3, Z:-116, T:112405
X:-226, Y:-3, Z:-116, T:112540
X:-226, Y:-3, Z:-116, T:112669
X:-233, Y:2, Z:-76, T:112792
X:-233, Y:2, Z:-76, T:112909
X:-233, Y:2, Z:-76, T:113028
X:-237, Y:7, Z:-35, T:113148
X:-237, Y:7, Z:-35, T:113266
X:-237, Y:7, Z:-35, T:113386
X:-242, Y:5, Z:-15, T:113504
X:-242, Y:5, Z:-15, T:113624
X:-242, Y:5, Z:-15, T:113764
X:-244, Y:5, Z:-3, T:113884
X:-244, Y:5, Z:-3, T:113999
X:-244, Y:5, Z:-3, T:114118
X:-242, Y:3, Z:-7, T:114239
X:-242, Y:3, Z:-7, T:114357
X:-242, Y:3, Z:-7, T:114473
X:-241, Y:0, Z:-30, T:114595
X:-241, Y:0, Z:-30, T:114720
X:-241, Y:0, Z:-30, T:114867
X:-227, Y:-13, Z:-95, T:114989
X:-227, Y:-13, Z:-95, T:115104
X:-227, Y:-13, Z:-95, T:115224
X:-212, Y:-5, Z:-114, T:115343
X:-212, Y:-5, Z:-114, T:115462
X:-212, Y:-5, Z:-114, T:115579
X:-215, Y:-6, Z:-145, T:115701
X:-215, Y:-6, Z:-145, T:115819
X:-215, Y:-6, Z:-145, T:115937
X:-210, Y:5, Z:-142, T:116059
X:-210, Y:5, Z:-142, T:116176
X:-210, Y:5, Z:-142, T:116296
X:-222, Y:-19, Z:-145, T:116415
X:-222, Y:-19, Z:-145, T:116534
X:-222, Y:-19, Z:-145, T:116655
X:-231, Y:6, Z:-119, T:116775
X:-231, Y:6, Z:-119, T:116894
X:-231, Y:6, Z:-119, T:117023
The issue is that since your .to_csv call is within the loop, 'Diff.csv' is being overwritten every time. Only the last time it's written is what you end up seeing.
There are a few solutions.
.to_csv(mode='a')
This uses append mode, so it will not overwrite the entire file. You will also want to specify header=None so that it doesn't constantly write the header column in the middle of the file. If you want you can add the header once before the loop.
for i in range(len(zpeaks)-1):
a = (x[zpeaks[i]:zpeaks[i+1]])
b = (y[zpeaks[i]:zpeaks[i+1]])
c = (z[zpeaks[i]:zpeaks[i+1]])
pd.concat([pd.DataFrame(a),
pd.DataFrame(b),
pd.DataFrame(c)], axis=1).to_csv('Diff.csv', mode='a', header=None)
Create a list, concat after the loop
Add your DataFrames to a list within the loop, then concatenate when the loop finishes and save the full DataFrame to a file at once.
l = []
for i in range(len(zpeaks)-1):
a = (x[zpeaks[i]:zpeaks[i+1]])
b = (y[zpeaks[i]:zpeaks[i+1]])
c = (z[zpeaks[i]:zpeaks[i+1]])
l.append(pd.concat([pd.DataFrame(a),pd.DataFrame(b), pd.DataFrame(c)], axis=1))
#pd.concat(l).to_csv('Diff.csv') # No column names
pd.concat(l).rename(columns=lambda x, y=iter(['x', 'y', 'z']): next(y)).to_csv('Diff.csv')
I'm not entirely sure, since I myself am still learning Python, but it looks like it may be due to your indentation at the end.
It should look like this:
for i in range(len(zpeaks)-1):
print(z[zpeaks[i]:zpeaks[i+1]])
a = (x[zpeaks[i]:zpeaks[i+1]])
b = (y[zpeaks[i]:zpeaks[i+1]])
c = (z[zpeaks[i]:zpeaks[i+1]])
pd.concat([pd.DataFrame(a),pd.DataFrame(b), pd.DataFrame(c)], axis=1).to_csv('Diff.csv', mode='w')
The last line should be within the for loop in order to add each row to the csv.
I have AssertionError using TA-Lib wrapper in python. Could you take a look at my code? I really appreciate your help.
import numpy as np
import talib
#This works
test_data = np.random.random(5)
np_out = talib.SMA(test_data,3)
print np_out
#How come this does not work? I need to fix
real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]
np_real_data = np.array(real_data,dtype=np.object)
np_out = talib.SMA(np_real_data,3)
print np_out
error message:
File "func.pyx", line 9200, in talib.func.SMA (talib/func.c:85610)
AssertionError: real is not double
I suspet the solution might be to convert double to real. I want to test that idea. How do I convert the real_data from double to real?
Thanks.
I suspect the solution might be to convert double to real.
No. You have real data. TA-lib doesn't like "real data". You want to convert it to double float data.
re: qcc's unexplained answer:
f8 is a 64 bit "double precision" floating point number.
http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
TA-lib wants numpy arrays of "double" floats as inputs.
http://en.wikipedia.org/wiki/Double-precision_floating-point_format
There are several methods you can use to get where you're going, probably the best for your purposes is:
real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]
float_data = [float(x) for x in real_data]
np_float_data = np.array(float_data)
np_out = talib.SMA(np_float_data,3)
Here are several others:
1)
real_data = [float(x) for x in [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]]
np_real_data = np.array(real_data)
np_out = talib.SMA(np_real_data,3)
2)
real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]
np_real_data = np.array(real_data, dtype='f8')
np_out = talib.SMA(np_real_data,3)
3)
real_data = [135.01, 133.0, 134.0, 131.0, 133.0, 131.0]
np_real_data = np.array(real_data, dtype=float)
np_out = talib.SMA(np_real_data,3)
4)
real_data = map(float, [135.01, 133.0, 134.0, 131.0, 133.0, 131.0])
np_real_data = np.array(real_data)
np_out = talib.SMA(np_real_data,3)
5)
real_data = [float(135.01), float(133.0), float(134.0), float(131.0),
float(133.0), float(131.0)]
np_real_data = np.array(real_data)
np_out = talib.SMA(np_real_data,3)
try this
np_real_data = np.array(real_data,dtype='f8')
Hi I've exported an outlook contacts csv file and loaded it into a python shell.
I have a number of European names in the list and the following for example
tmp = 'Fern\xc3\x9fndez'
tmp.encode("latin-1")
results in an error
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)
while
tmp.decode('latin-1')
gives me
u'Fern\xc3\x9fndez'
How do I get the text to read as Fernandez? (not too worried about the accents - but happy to have them)
You must be using Python 2.x. Here is one way to print out the character (depending on which encoding you are working with):
>>> tmp = 'Fern\xc3\x9fndez'
>>> print tmp.decode('utf-8') # print formats the string for stdout
Fernßndez
>>> print tmp.decode('latin1')
FernÃndez
Are you sure you have the right character? Is it utf-8? And another way:
>>> print unicode(tmp, 'latin1')
FernÃndez
>>> print unicode(tmp, 'utf-8')
Fernßndez
Interesting. So none of these options worked for you? Incidentally, I ran the string through a few other encodings just to see if any of them had a character more in line with what I would expect. Unfortunately, I don't see any that look quite right:
>>> for encoding in ['ascii', 'big5', 'big5hkscs', 'cp037', 'cp424', 'cp437', 'cp500', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856', 'cp857', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', 'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006', 'cp1026', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', 'cp1256', 'cp1257', 'cp1258', 'euc_jp', 'euc_jis_2004', 'euc_jisx0213', 'euc_kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022_jp', 'iso2022_jp_1', 'iso2022_jp_2', 'iso2022_jp_2004', 'iso2022_jp_3', 'iso2022_jp_ext', 'iso2022_kr', 'latin_1', 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6', 'iso8859_7', 'iso8859_8', 'iso8859_9', 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', 'johab', 'koi8_r', 'koi8_u', 'mac_cyrillic', 'mac_greek', 'mac_iceland', 'mac_latin2', 'mac_roman', 'mac_turkish', 'ptcp154', 'shift_jis', 'shift_jis_2004', 'shift_jisx0213', 'utf_16', 'utf_16_be', 'utf_16_le', 'utf_7', 'utf_8']:
try:
print encoding + ': ' + tmp.decode(encoding)
except:
pass
cp037: ãÁÊ>C¤>ÀÁ:
cp437: Fernßndez
cp500: ãÁÊ>C¤>ÀÁ:
cp737: Fern├θndez
cp775: Fern├¤ndez
cp850: Fernßndez
cp852: Fern├čndez
cp855: Fern├Ъndez
cp857: Fern├şndez
cp860: Fern├Óndez
cp861: Fernßndez
cp862: Fernßndez
cp863: Fernßndez
cp865: Fernßndez
cp866: Fern├Яndez
cp869: Fern├ίndez
cp875: ΖΧΈ>Cμ>ΦΧ:
cp932: Fernテ殤dez
cp949: Fern횩ndez
cp1006: Fernﺣndez
cp1026: ãÁÊ>C¤>ÀÁ:
cp1140: ãÁÊ>C€>ÀÁ:
cp1250: FernĂźndez
cp1251: FernГџndez
cp1252: Fernßndez
cp1254: Fernßndez
cp1256: Fernأںndez
cp1258: FernĂŸndez
gbk: Fern脽ndez
gb18030: Fern脽ndez
latin_1: FernÃndez
iso8859_2: FernĂndez
iso8859_4: FernÃndez
iso8859_5: FernУndez
iso8859_6: Fernأndez
iso8859_7: FernΓndez
iso8859_9: FernÃndez
iso8859_10: FernÃndez
iso8859_13: FernĆndez
iso8859_14: FernÃndez
iso8859_15: FernÃndez
koi8_r: Fernц÷ndez
koi8_u: Fernц÷ndez
mac_cyrillic: Fern√Яndez
mac_greek: FernΟündez
mac_iceland: Fernßndez
mac_latin2: Fernßndez
mac_roman: Fernßndez
mac_turkish: Fernßndez
ptcp154: FernГҹndez
shift_jis: Fernテ殤dez
shift_jis_2004: Fernテ殤dez
shift_jisx0213: Fernテ殤dez
utf_16: 敆湲鿃摮穥
utf_16_be: 䙥牮쎟湤敺
utf_16_le: 敆湲鿃摮穥
utf_8: Fernßndez
Or, practically, how can I sort a list of dictionaries by multiple keys?
I have a list of dicts:
b = [{u'TOT_PTS_Misc': u'Utley, Alex', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Russo, Brandon', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Chappell, Justin', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Foster, Toney', u'Total_Points': 80.0},
{u'TOT_PTS_Misc': u'Lawson, Roman', u'Total_Points': 80.0},
{u'TOT_PTS_Misc': u'Lempke, Sam', u'Total_Points': 80.0},
{u'TOT_PTS_Misc': u'Gnezda, Alex', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Kirks, Damien', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Worden, Tom', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Korecz, Mike', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Swartz, Brian', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Burgess, Randy', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Smugala, Ryan', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Harmon, Gary', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Blasinsky, Scott', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Carter III, Laymon', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Coleman, Johnathan', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Venditti, Nick', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Blackwell, Devon', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Kovach, Alex', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Bolden, Antonio', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Smith, Ryan', u'Total_Points': 60.0}]
and I need to use a multi key sort reversed by Total_Points, then not reversed by TOT_PTS_Misc.
This can be done at the command prompt like so:
a = sorted(b, key=lambda d: (-d['Total_Points'], d['TOT_PTS_Misc']))
But I have to run this through a function, where I pass in the list and the sort keys. For example, def multikeysort(dict_list, sortkeys):.
How can the lambda line be used which will sort the list, for an arbitrary number of keys that are passed in to the multikeysort function, and take into consideration that the sortkeys may have any number of keys and those that need reversed sorts will be identified with a '-' before it?
This article has a nice rundown on various techniques for doing this. If your requirements are simpler than "full bidirectional multikey", take a look. It's clear the accepted answer and the blog post I just
referenced influenced each other in some way, though I don't know which order.
In case the link dies here's a very quick synopsis of examples not covered above:
mylist = sorted(mylist, key=itemgetter('name', 'age'))
mylist = sorted(mylist, key=lambda k: (k['name'].lower(), k['age']))
mylist = sorted(mylist, key=lambda k: (k['name'].lower(), -k['age']))
This answer works for any kind of column in the dictionary -- the negated column need not be a number.
def multikeysort(items, columns):
from operator import itemgetter
comparers = [((itemgetter(col[1:].strip()), -1) if col.startswith('-') else
(itemgetter(col.strip()), 1)) for col in columns]
def comparer(left, right):
for fn, mult in comparers:
result = cmp(fn(left), fn(right))
if result:
return mult * result
else:
return 0
return sorted(items, cmp=comparer)
You can call it like this:
b = [{u'TOT_PTS_Misc': u'Utley, Alex', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Russo, Brandon', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Chappell, Justin', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Foster, Toney', u'Total_Points': 80.0},
{u'TOT_PTS_Misc': u'Lawson, Roman', u'Total_Points': 80.0},
{u'TOT_PTS_Misc': u'Lempke, Sam', u'Total_Points': 80.0},
{u'TOT_PTS_Misc': u'Gnezda, Alex', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Kirks, Damien', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Worden, Tom', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Korecz, Mike', u'Total_Points': 78.0},
{u'TOT_PTS_Misc': u'Swartz, Brian', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Burgess, Randy', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Smugala, Ryan', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Harmon, Gary', u'Total_Points': 66.0},
{u'TOT_PTS_Misc': u'Blasinsky, Scott', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Carter III, Laymon', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Coleman, Johnathan', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Venditti, Nick', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Blackwell, Devon', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Kovach, Alex', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Bolden, Antonio', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Smith, Ryan', u'Total_Points': 60.0}]
a = multikeysort(b, ['-Total_Points', 'TOT_PTS_Misc'])
for item in a:
print item
Try it with either column negated. You will see the sort order reverse.
Next: change it so it does not use extra class....
2016-01-17
Taking my inspiration from this answer What is the best way to get the first item from an iterable matching a condition?, I shortened the code:
from operator import itemgetter as i
def multikeysort(items, columns):
comparers = [
((i(col[1:].strip()), -1) if col.startswith('-') else (i(col.strip()), 1))
for col in columns
]
def comparer(left, right):
comparer_iter = (
cmp(fn(left), fn(right)) * mult
for fn, mult in comparers
)
return next((result for result in comparer_iter if result), 0)
return sorted(items, cmp=comparer)
In case you like your code terse.
Later 2016-01-17
This works with python3 (which eliminated the cmp argument to sort):
from operator import itemgetter as i
from functools import cmp_to_key
def cmp(x, y):
"""
Replacement for built-in function cmp that was removed in Python 3
Compare the two objects x and y and return an integer according to
the outcome. The return value is negative if x < y, zero if x == y
and strictly positive if x > y.
https://portingguide.readthedocs.io/en/latest/comparisons.html#the-cmp-function
"""
return (x > y) - (x < y)
def multikeysort(items, columns):
comparers = [
((i(col[1:].strip()), -1) if col.startswith('-') else (i(col.strip()), 1))
for col in columns
]
def comparer(left, right):
comparer_iter = (
cmp(fn(left), fn(right)) * mult
for fn, mult in comparers
)
return next((result for result in comparer_iter if result), 0)
return sorted(items, key=cmp_to_key(comparer))
Inspired by this answer How should I do custom sort in Python 3?
I know this is a rather old question, but none of the answers mention that Python guarantees a stable sort order for its sorting routines such as list.sort() and sorted(), which means items that compare equal retain their original order.
This means that the equivalent of ORDER BY name ASC, age DESC (using SQL notation) for a list of dictionaries can be done like this:
items.sort(key=operator.itemgetter('age'), reverse=True)
items.sort(key=operator.itemgetter('name'))
Note how the items are first sorted by the "lesser" attribute age (descending), then by the "major" attribute name, leading to the correct final order.
The reversing/inverting works for all orderable types, not just numbers which you can negate by putting a minus sign in front.
And because of the Timsort algorithm used in (at least) CPython, this is actually rather fast in practice.
def sortkeypicker(keynames):
negate = set()
for i, k in enumerate(keynames):
if k[:1] == '-':
keynames[i] = k[1:]
negate.add(k[1:])
def getit(adict):
composite = [adict[k] for k in keynames]
for i, (k, v) in enumerate(zip(keynames, composite)):
if k in negate:
composite[i] = -v
return composite
return getit
a = sorted(b, key=sortkeypicker(['-Total_Points', 'TOT_PTS_Misc']))
I had a similar issue today - I had to sort dictionary items by descending numeric values and by ascending string values. To solve the issue of conflicting directions, I negated the integer values.
Here's a variant of my solution - as applicable to OP
sorted(b, key=lambda e: (-e['Total_Points'], e['TOT_PTS_Misc']))
Very simple - and works like a charm
[{'TOT_PTS_Misc': 'Chappell, Justin', 'Total_Points': 96.0},
{'TOT_PTS_Misc': 'Russo, Brandon', 'Total_Points': 96.0},
{'TOT_PTS_Misc': 'Utley, Alex', 'Total_Points': 96.0},
{'TOT_PTS_Misc': 'Foster, Toney', 'Total_Points': 80.0},
{'TOT_PTS_Misc': 'Lawson, Roman', 'Total_Points': 80.0},
{'TOT_PTS_Misc': 'Lempke, Sam', 'Total_Points': 80.0},
{'TOT_PTS_Misc': 'Gnezda, Alex', 'Total_Points': 78.0},
{'TOT_PTS_Misc': 'Kirks, Damien', 'Total_Points': 78.0},
{'TOT_PTS_Misc': 'Korecz, Mike', 'Total_Points': 78.0},
{'TOT_PTS_Misc': 'Worden, Tom', 'Total_Points': 78.0},
{'TOT_PTS_Misc': 'Burgess, Randy', 'Total_Points': 66.0},
{'TOT_PTS_Misc': 'Harmon, Gary', 'Total_Points': 66.0},
{'TOT_PTS_Misc': 'Smugala, Ryan', 'Total_Points': 66.0},
{'TOT_PTS_Misc': 'Swartz, Brian', 'Total_Points': 66.0},
{'TOT_PTS_Misc': 'Blackwell, Devon', 'Total_Points': 60.0},
{'TOT_PTS_Misc': 'Blasinsky, Scott', 'Total_Points': 60.0},
{'TOT_PTS_Misc': 'Bolden, Antonio', 'Total_Points': 60.0},
{'TOT_PTS_Misc': 'Carter III, Laymon', 'Total_Points': 60.0},
{'TOT_PTS_Misc': 'Coleman, Johnathan', 'Total_Points': 60.0},
{'TOT_PTS_Misc': 'Kovach, Alex', 'Total_Points': 60.0},
{'TOT_PTS_Misc': 'Smith, Ryan', 'Total_Points': 60.0},
{'TOT_PTS_Misc': 'Venditti, Nick', 'Total_Points': 60.0}]
I use the following for sorting a 2d array on a number of columns
def k(a,b):
def _k(item):
return (item[a],item[b])
return _k
This could be extended to work on an arbitrary number of items. I tend to think finding a better access pattern to your sortable keys is better than writing a fancy comparator.
>>> data = [[0,1,2,3,4],[0,2,3,4,5],[1,0,2,3,4]]
>>> sorted(data, key=k(0,1))
[[0, 1, 2, 3, 4], [0, 2, 3, 4, 5], [1, 0, 2, 3, 4]]
>>> sorted(data, key=k(1,0))
[[1, 0, 2, 3, 4], [0, 1, 2, 3, 4], [0, 2, 3, 4, 5]]
>>> sorted(a, key=k(2,0))
[[0, 1, 2, 3, 4], [1, 0, 2, 3, 4], [0, 2, 3, 4, 5]]
from operator import itemgetter
from functools import partial
def _neg_itemgetter(key, d):
return -d[key]
def key_getter(key_expr):
keys = key_expr.split(",")
getters = []
for k in keys:
k = k.strip()
if k.startswith("-"):
getters.append(partial(_neg_itemgetter, k[1:]))
else:
getters.append(itemgetter(k))
def keyfunc(dct):
return [kg(dct) for kg in getters]
return keyfunc
def multikeysort(dict_list, sortkeys):
return sorted(dict_list, key = key_getter(sortkeys)
Demonstration:
>>> multikeysort([{u'TOT_PTS_Misc': u'Utley, Alex', u'Total_Points': 60.0},
{u'TOT_PTS_Misc': u'Russo, Brandon', u'Total_Points': 96.0},
{u'TOT_PTS_Misc': u'Chappell, Justin', u'Total_Points': 96.0}],
"-Total_Points,TOT_PTS_Misc")
[{u'Total_Points': 96.0, u'TOT_PTS_Misc': u'Chappell, Justin'},
{u'Total_Points': 96.0, u'TOT_PTS_Misc': u'Russo, Brandon'},
{u'Total_Points': 60.0, u'TOT_PTS_Misc': u'Utley, Alex'}]
The parsing is a bit fragile, but at least it allows for variable number of spaces between the keys.
Since you're already comfortable with lambda, here's a less verbose solution.
>>> def itemgetter(*names):
return lambda mapping: tuple(-mapping[name[1:]] if name.startswith('-') else mapping[name] for name in names)
>>> itemgetter('a', '-b')({'a': 1, 'b': 2})
(1, -2)