0

I have the following nested list.

 peaks = [[313, 0.7608371709999999], [695, 0.6970320329999999], [996, 0.652950446], [1367, 0.570015382], [1518, 0.516312078], [1726, 0.5599991639999999], [1871, 0.5025837670000001], [2084, 0.563103466], [2240, 0.5229157010000001], [2478, 0.611383482], [2622, 0.59167585], [2987, 0.68685803], [3225, 0.524618916], [3573, 0.738796993], [3938, 0.8191861140000001], [4302, 0.82872904], [4666, 0.814913208], [5038, 0.7919655170000001], [5397, 0.773237498], [5762, 0.78905603]]

I want to extract the first element to a separate list/array and the second to another list/array.

I have done this using a for loop,

max_peaksIdx = []
max_peaksVal = []
for i in max_peaks:
    max_peaksIdx.append(i[0])
    max_peaksVal.append(i[1])

#out : max_peaksIsdx : [313, 695, 996, 1367, 1518, 1726, 1871, 2084, 2240, 2478, 2622, 2987, 3225, 3573, 3938, 4302, 4666, 5038, 5397, 5762]
#out: maxpeaksVal :[0.7608371709999999, 0.6970320329999999, 0.652950446, 0.570015382, 0.516312078, 0.5599991639999999, 0.5025837670000001, 0.563103466, 0.5229157010000001, 0.611383482, 0.59167585, 0.68685803, 0.524618916, 0.738796993, 0.8191861140000001, 0.82872904, 0.814913208, 0.7919655170000001, 0.773237498, 0.78905603]

I am curious to know if the same result can be obtained using numpy slicing? (without using a for loop)

something like,

max_peaksIdx = peaks[:][0]  #this doesn't work, just picks the first element. but get a similar result shown above, but with one line code.

4 Answers 4

4

Yes, it can be done!

import numpy as np

peaks = np.array(peaks)

max_peaksIsdx = peaks[:,0]
max_peaksVal = peaks[:,1]
Sign up to request clarification or add additional context in comments.

Comments

2

You can use zip with unpacking:

max_peaksIdx, max_peaksVal = zip(*peaks)

Output:

max_peaksIdx
# (313, 695, 996, ...)
max_peaksVal
# (0.7608371709999999, 0.6970320329999999, 0.652950446,...)

If you have to construct numpy array:

arr = np.array(peaks)
max_peaksIdx, max_peaksVal = arr[:, 0], arr[:, 1]

But zip is about 9x faster:

%timeit max_peaksIdx, max_peaksVal = zip(*peaks)
# 1.19 µs ± 91.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%%timeit
arr = np.array(peaks)
max_peaksIdx, max_peaksVal = arr[:, 0], arr[:, 1]
# 10 µs ± 120 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

1 Comment

Amazin. Thanks definitely what I was looking for :)
0

There are a number of ways to do so. Firstly, you can try zip and unpacking as

lis = []
for i in range(len(peaks[0])):
    lis.append(list(list(zip(*peaks))[i]) )

This will output lis as

[[313,
  695,
  996,
  1367,
  1518,
  1726,
  1871,
  2084,
  2240,
  2478,
  2622,
  2987,
  3225,
  3573,
  3938,
  4302,
  4666,
  5038,
  5397,
  5762],
 [0.7608371709999999,
  0.6970320329999999,
  0.652950446,
  0.570015382,
  0.516312078,
  0.5599991639999999,
  0.5025837670000001,
  0.563103466,
  0.5229157010000001,
  0.611383482,
  0.59167585,
  0.68685803,
  0.524618916,
  0.738796993,
  0.8191861140000001,
  0.82872904,
  0.814913208,
  0.7919655170000001,
  0.773237498,
  0.78905603]]

Only zip can also be used.

list( map(itemgetter(i), peaks)) 

Comments

-2
   max_peaksIdx = peaks[:][0] 

can also be done as

   max_peaksIdx = peaks[0] #stores the first element in peaks
   max_peaksVal = peaks[1] #stores the second element in peaks

without the ":", all without the for loop.

2 Comments

Sorry this wouldnt work. It just picks the first element. i.e [313, 0.7608371709999999]
it picks the first element to the first list created and the 2nd element to the 2nd list created, just going off your question. this should work without the for loop and is not a slicing approach :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.