I want to sum elements in an array. For example I have an array
[183948, 218520, 243141, 224539, 205322, 203855, 233281, 244830, 281245,
280579, 235384, 183596, 106072, 88773, 63297, 38769, 28343]
I want to sum it in three different parts which are the first three elements, the next 10 elements and the rest.
My only idea is to separate the array and use sum method. Is there better way to do it?
Thanks in advance!
try this:
arr=[183948, 218520, 243141, 224539, 205322, 203855, 233281, 244830, 281245,
280579, 235384, 183596, 106072, 88773, 63297, 38769, 28343]
first=arr[0:3]
second=arr[3:13]
last=arr[13:]
print(sum(first))
print(sum(second))
print(sum(last))
the alternative more extensible version is as follows
arr=[183948, 218520, 243141, 224539, 205322, 203855, 233281, 244830, 281245,
280579, 235384, 183596, 106072, 88773, 63297, 38769, 28343]
indices=[3,13]
results=[]
prev=0
for i in indices:
results.append(sum(arr[prev:i]))
prev=i
results.append(sum(arr[prev:]))
for res in results:
print(res)
note: set prev = to the index you want to start from, in this case 0
You can use the reduceat method of np.add:
data = [183948, 218520, 243141, 224539, 205322, 203855, 233281, 244830, 281245,
280579, 235384, 183596, 106072, 88773, 63297, 38769, 28343]
sizes = 3, 10
np.add.reduceat(data, np.cumsum([0, *sizes]))
# array([ 645609, 2198703, 219182])
Related
the input to my function is a list of lists like the one below, and I want to divide each item in the list after the 3rd one by the third one for each list.. How do I do that?
So far I tried this:
def convert_data_percentages(data):
lst = []
for x in data:
lst.append(int(x[2:]) // int(x[2]))
return lst
convert_data_percentages(data, col_id=2)
[["'s-Gravenhage",
'GM0518',
'537833',
'266778',
'271055',
'92532',
'66099',
'162025',
'139157',
'78020',
'304766',
'162020',
'51430',
'19617'],
['Rotterdam',
'GM0599',
'644618',
'317935',
'326683',
'103680',
'86037',
'197424',
'159008',
'98469',
'367279',
'187835',
'62703',
'26801']]
Do you mean something like this?
data = [["'s-Gravenhage",
'GM0518',
'537833',
'266778',
'271055',
'92532',
'66099',
'162025',
'139157',
'78020',
'304766',
'162020',
'51430',
'19617'],
['Rotterdam',
'GM0599',
'644618',
'317935',
'326683',
'103680',
'86037',
'197424',
'159008',
'98469',
'367279',
'187835',
'62703',
'26801']]
# reformat original data, dropping first two items and converting to ints
# then extract third item and remaining subset into tuple
subsets = [(lst[:2] + to_int[:1], to_int[0], to_int[1:]) for lst in data if (to_int := [int(i) for i in lst[2:]])]
# walk through tuples generating a new list by dividing all items in the
# remaining subset by the third item
print([first_three + [i / third for i in subset] for first_three, third, subset in subsets])
returns:
[["'s-Gravenhage", 'GM0518', 537833, 0.4960238587070708, 0.5039761412929292, 0.1720459696597271, 0.1228987436620661, 0.30125522234596985, 0.2587364479308633, 0.14506361640137366, 0.5666554488103185, 0.3012459257799354, 0.09562447823023132, 0.03647414717951483],
['Rotterdam', 'GM0599', 644618, 0.49321458600287305, 0.506785413997127, 0.16083944289486177, 0.13346974487215685, 0.3062651058456326, 0.24667012090881732, 0.15275558547853146, 0.5697622467880202, 0.291389629206755, 0.09727156238268246, 0.04157656162254234]]
I am new to python and I am attempting to pull specific values from an array based on their index value not the value of the element. How can I do this?
I know what array I want to pull the data from and the indexes I want pull but I am unsure as to the setup as a function.
I want to pull data from my hip angles file and pull specific indexes.
def hpst(demands):
hp = demands['HP'][:,0]
# fk = demands['FK'][:,0]
# aa = demands['AA'][:,0]
# val = (demands['StridePeaks'][:,0])
# hpst = {}
for A in range(len(hp)):
return A[[15,33,51,73,88,109,125,145,161,182,200,222,238]]
I am unsure if this is the proper setup.
However, this is the hip angle data from a pandas dictionary. I only want to pull data from the 0 column and at the indexes of 15, 33, etc.
142.444 94.3356
132.121 92.7187
122.649 92.2527
112.534 103.177
100.118 102.765
92.0133 93.1956
86.4117 89.6813
77.5857 90.1729
70.9326 95.0486
67.0381 100.924
66.5811 110.449
69.5347 125.725
71.2853 139.804
73.4733 138.542
74.1935 127.868
73.7101 128.108
75.4367 128.032
77.7155 127.292
81.593 135.946
82.7716 128.468
79.9823 107.518
80.7256 100.981
84.3518 96.2074
85.6058 86.0201
83.7879 87.4381
85.4597 93.5943
89.8181 92.0666
97.5656 90.6827
116.002 89.9328
135.595 88.8136
150.94 91.4732
157.737 88.6928
155.49 84.379
156.55 87.471
155.458 89.4682
153.036 93.9603
153.757 98.5537
145.86 96.0115
134.803 100.779
126.987 107.122
111.637 102.514
95.01 97.033
89.0428 93.2397
84.2956 88.3167
73.1231 85.827
65.6818 85.3847
66.7661 88.2002
66.9876 102.567
65.6378 123.309
67.0576 138.525
72.153 142.375
77.9615 134.473
78.7077 129.01
79.3094 132.146
79.6939 133.914
77.8669 132.105
79.4663 125.982
80.7738 113.921
81.9972 105.029
86.7038 96.5438
86.8284 84.9008
83.2474 87.0168
86.8545 95.0353
89.5397 88.5338
90.7755 87.7473
105.112 99.8809
122.347 98.6513
137.134 95.2858
152.183 101.437
158.137 100.553
156.484 96.2948
153.716 96.7974
154.801 102.498
152.89 104.698
143.468 98.8762
138.304 95.86
128.719 94.8656
114.043 95.6951
107.558 96.4558
101.166 93.4559
93.7206 93.5208
85.3982 90.5242
75.906 87.6068
72.278 90.3149
70.2718 91.2148
69.3056 103.069
70.011 120.223
71.3477 124.454
76.9937 126.978
80.8508 131.451
80.0341 130.221
80.4728 122.764
81.5849 116.894
81.7331 114.3
80.66 104.565
80.688 95.5776
83.5644 91.9178
84.0912 87.5472
83.9085 88.8345
87.5639 88.372
92.5143 87.5451
97.9724 95.4833
108.694 98.5047
125.099 93.46
140.345 92.2416
157.075 98.5277
169.54 99.8388
168.204 99.911
162.676 103.14
155.131 97.1475
150.646 98.1131
152.127 106.245
149.424 111.988
141.379 113.049
124.835 95.792
107.939 88.8298
98.8121 96.6449
88.9631 92.1304
81.1267 90.0516
73.6213 90.3448
67.3936 86.9272
69.2013 94.6061
68.7782 113.364
67.1497 128.624
72.419 133.438
77.1528 132.73
76.2917 126.883
75.8093 123.265
77.8584 124.75
77.9565 122.36
78.0597 118.024
80.5439 108.822
81.6859 99.0825
81.1073 91.7428
80.6491 85.872
82.9748 92.8926
84.5156 89.72
82.8995 79.0692
93.4305 87.1985
115.421 91.5762
134.818 91.9185
148.396 92.6058
155.7 91.8545
157.929 91.8012
156.029 89.0408
155.537 103.305
152.636 106.649
143.85 91.4026
139.15 95.9343
134.295 101.185
120.378 101.393
109.157 104.284
100.904 103.878
92.7664 101.406
85.2396 91.9995
76.9565 80.6445
74.7453 86.9655
60.4233 105.397
76.727 89.4368
106.752 80.918
76.3472 122.442
63.0862 136.702
75.857 124.376
72.2408 127.796
74.3019 128.157
73.7003 126.903
75.6378 126.893
80.1898 119.195
79.818 107.194
78.0927 95.9515
78.6968 90.6118
81.6341 92.9499
81.8974 94.8118
85.8516 94.3597
91.5325 94.9903
100.875 94.3432
122.649 92.9595
143.269 90.4793
153.609 88.9303
159.759 93.486
162.477 102.913
158.822 107.012
154.776 105.631
155.569 108.909
152.73 103.638
146.952 93.7555
139.01 101.398
123.76 107.7
112.718 102.206
106.651 101.55
96.8904 97.5473
89.2883 91.0048
82.4354 86.8519
73.3603 85.9089
67.0828 98.7009
63.467 84.9501
64.4223 76.5582
67.4855 122.781
69.9852 140.948
73.8453 130.218
72.9275 130.016
72.6492 121.706
75.903 120.577
76.0476 122.544
78.4037 119.37
80.7675 112.94
82.5817 99.982
86.3209 93.482
85.0229 94.9573
84.1585 99.5623
86.933 98.3248
86.9423 87.0122
90.3343 85.6848
103.944 93.6842
123.422 91.7796
140.788 89.9716
154.369 92.1987
165.511 89.7282
167.283 90.7866
162.989 93.755
162.456 97.8036
156.689 97.0894
149.863 92.3949
148.294 99.2785
136.549 96.2237
123.03 92.4155
111.187 100.863
94.6514 96.2559
88.9702 95.7135
86.7612 92.5555
78.0158 85.3122
68.8636 90.824
62.4144 82.137
64.6031 86.899
65.628 117.267
62.1972 132.161
65.9919 137.014
70.3412 137.643
70.6389 133.078
71.8256 130.303
72.4533 126.325
74.2835 124.771
77.3096 123.649
77.6268 116.093
77.708 105.539
78.4 93.3733
78.7506 86.8869
79.7636 91.1909
82.4384 94.0581
85.8938 90.8869
87.6994 85.0496
94.2127 84.5761
111.206 91.2272
132.669 91.6249
150.309 89.9407
160.821 90.4688
164.157 89.0467
161.775 92.7846
158.439 99.413
156.53 100.486
151.654 94.4403
141.89 92.9437
133.617 93.4393
123.511 83.0772
107.083 84.3018
95.5675 96.8183
91.9259 95.9605
87.6419 90.4093
78.7722 87.534
Basically I want to pull the angles from those indexes in the array of hip angle data.
I don't think there's a good way to do this in one statement, but you can do it in two using list comprehension:
indices = [0,15,33,51,73,88,109,125,145,161,182,200,222,238]
return [A[i] for i in indices]
As for slicing syntax in general:
arr[start:end:step]
So, doing A[3:10:2] would start at index 3, go until reaching index 10, stepping by two elements each time (you can also use negative numbers for these indices; those count from the back of the list instead of from the front). So, you'd get
A[3:10:2] --> [A[3], A[5], A[7], A[9]]
If you leave start blank, it goes from the beginning of the list. If you leave end blank, it goes until the end of the list. And if you leave step blank, it defaults to 1, or taking every element from start to end.
I have to generate multiple reactions with different variables. They have 3 elements. Let's call them B, S and H. And they all start with B1. S can be appended to the element if there is at least one B. So it can be B1S1 or B2S2 or B2S1 etc... but not B1S2. The same goes for H. B1S1H1 or B2S2H1 or B4S1H1 but never B2S2H3. The final variation would be B5S5H5. I tried with itertools.product. But I don't know how to get rid of the elements that don't match my condition and how to add the next element. Here is my code:
import itertools
a = list(itertools.product([1, 2, 3, 4], repeat=4))
#print (a)
met = open('random_dat.dat', 'w')
met.write('Reactions')
met.write('\n')
for i in range(1,256):
met.write('\n')
met.write('%s: B%sS%sH%s -> B%sS%sH%s' %(i, a[i][3], a[i][2], a[i][1], a[i][3], a[i][2], a[i][1]))
met.write('\n')
met.close()
Simple for loops will do what you want:
bsh = []
for b in range(1,6):
for s in range(1,b+1):
for h in range(1,b+1):
bsh.append( f"B{b}S{s}H{h}" )
print(bsh)
Output:
['B1S1H1', 'B2S1H1', 'B2S1H2', 'B2S2H1', 'B2S2H2', 'B3S1H1', 'B3S1H2', 'B3S1H3',
'B3S2H1', 'B3S2H2', 'B3S2H3', 'B3S3H1', 'B3S3H2', 'B3S3H3', 'B4S1H1', 'B4S1H2',
'B4S1H3', 'B4S1H4', 'B4S2H1', 'B4S2H2', 'B4S2H3', 'B4S2H4', 'B4S3H1', 'B4S3H2',
'B4S3H3', 'B4S3H4', 'B4S4H1', 'B4S4H2', 'B4S4H3', 'B4S4H4', 'B5S1H1', 'B5S1H2',
'B5S1H3', 'B5S1H4', 'B5S1H5', 'B5S2H1', 'B5S2H2', 'B5S2H3', 'B5S2H4', 'B5S2H5',
'B5S3H1', 'B5S3H2', 'B5S3H3', 'B5S3H4', 'B5S3H5', 'B5S4H1', 'B5S4H2', 'B5S4H3',
'B5S4H4', 'B5S4H5', 'B5S5H1', 'B5S5H2', 'B5S5H3', 'B5S5H4', 'B5S5H5']
Thanks to #mikuszefski for pointing out improvements.
Patrick his answer in list comprehension style
bsh = [f"B{b}S{s}H{h}" for b in range(1,5) for s in range(1,b+1) for h in range(1,b+1)]
Gives
['B1S1H1',
'B2S1H1',
'B2S1H2',
'B2S2H1',
'B2S2H2',
'B3S1H1',
'B3S1H2',
'B3S1H3',
'B3S2H1',
'B3S2H2',
'B3S2H3',
'B3S3H1',
'B3S3H2',
'B3S3H3',
'B4S1H1',
'B4S1H2',
'B4S1H3',
'B4S1H4',
'B4S2H1',
'B4S2H2',
'B4S2H3',
'B4S2H4',
'B4S3H1',
'B4S3H2',
'B4S3H3',
'B4S3H4',
'B4S4H1',
'B4S4H2',
'B4S4H3',
'B4S4H4']
I would implement your "use itertools.product and get rid off unnecessary elements" solution following way:
import itertools
a = list(itertools.product([1,2,3,4,5],repeat=3))
a = [i for i in a if (i[1]<=i[0] and i[2]<=i[1] and i[2]<=i[0])]
Note that I assumed last elements needs to be smaller or equal than any other. Note that a is now list of 35 tuples each holding 3 ints. So you need to made strs of them for example using so-called f-string:
a = [f"B{i[0]}S{i[1]}H{i[2]}" for i in a]
print(a)
output:
['B1S1H1', 'B2S1H1', 'B2S2H1', 'B2S2H2', 'B3S1H1', 'B3S2H1', 'B3S2H2', 'B3S3H1', 'B3S3H2', 'B3S3H3', 'B4S1H1', 'B4S2H1', 'B4S2H2', 'B4S3H1', 'B4S3H2', 'B4S3H3', 'B4S4H1', 'B4S4H2', 'B4S4H3', 'B4S4H4', 'B5S1H1', 'B5S2H1', 'B5S2H2', 'B5S3H1', 'B5S3H2', 'B5S3H3', 'B5S4H1', 'B5S4H2', 'B5S4H3', 'B5S4H4', 'B5S5H1', 'B5S5H2', 'B5S5H3', 'B5S5H4', 'B5S5H5']
However you might also use another methods of formatting instead of f-string if you wish.
tests= ['test-2017-09-19-12-06',
'test-2017-09-19-12-05',
'test-2017-09-12-12-06',
'test-2017-09-12-12-05',
'test-2017-09-07-12-05',
'test-2017-09-06-12-07']
So I have the above list, how could I group by the list such that I can get a list which looks like the one below:
[['test-2017-09-19-12-06','test-2017-09-19-12-05'],
['test-2017-09-12-12-06','test-2017-09-12-12-05'],
['test-2017-09-07-12-05'],
['test-2017-09-06-12-07']]
I did try the following code but I get different results, where each string value becomes its own list rather than group by.
from itertools import groupby
print([list(j) for i, j in groupby(tests)])
See that range 0:15, you can use that to determine which segment is used for the grouping.
tests= ['test-2017-09-19-12-06',
'test-2017-09-19-12-05',
'test-2017-09-12-12-06',
'test-2017-09-12-12-05',
'test-2017-09-07-12-05',
'test-2017-09-06-12-07']
pprint.pprint([list(j[1]) for j in groupby(tests,lambda i:i[0:15])])
[['test-2017-09-19-12-06', 'test-2017-09-19-12-05'],
['test-2017-09-12-12-05', 'test-2017-09-12-12-05'],
['test-2017-09-07-12-05'],
['test-2017-09-06-12-07']]
You can split it with by size
tests = ['test-2017-09-19-12-06',
'test-2017-09-19-12-05',
'test-2017-09-12-12-05',
'test-2017-09-12-12-05',
'test-2017-09-07-12-05',
'test-2017-09-06-12-07']
size = 2
[tests[i:i+size] for i in range(0, len(tests), size)]
I have an array of array's named pdf of len(pdf) = 300
pdf[0] is an array of len(pdf[0]) = 300
What I would like to do is, take the sum of pdf[0][50:100] and repeat the same for pdf[1][50:100] and so on upto pdf[300][50:100].
What I tried is:
for i,a in enumerate(pdf):
result.append(a[i][50:100].sum())
But I get the error:
IndexError: invalid index to scalar variable
If you just need to build a simple list, then a List Comprehension is your friend:
result = [i[50:100].sum() for i in pdf]
Should do it.
In your for loop,
a is pdf[0], pdf[1]...
i is 0,1...
so you are referencing pdf[0][0][50:100], pdf[1][1][50:100]... hence the tracebck.
This means you could also have changed your loop to:
result = []
for a in (pdf):
result.append(a[50:100].sum())
...but that's what list comprehensions are for :)
In
for i,a in enumerate(pdf):
result.append(a[i][50:100].sum())
a is actually pdf[0], pdf[1], etc. So you calculate sums of pdf[0][0], pdf[1][1], etc.
If you want: take the sum of pdf[0][50:100] and repeat the same for pdf[1][50:100] and so on upto pdf[300][50:100]
you can do
result = [a[50:100].sum() for a in pdf]