Note that there are some explanatory texts on larger screens.

plurals
  1. POslicing two lists to have same size using for loop in python
    text
    copied!<p>Hello I want to create two new lists from existing lists using for loop.I know by slicing the lists I can get that result but I have a big list with more than a 1000 entries so slicing by counting the index is not very reliable. I am learning python so there may be other ways I am not aware of so would appreciate if anyone can solve my problem.</p> <p>So I have two lists say :</p> <pre><code> a=np.linspace(1,10,30) b=np.linspcae(0,6,30) a array([ 1. , 1.31034483, 1.62068966, 1.93103448, 2.24137931, 2.55172414, 2.86206897, 3.17241379, 3.48275862, 3.79310345, 4.10344828, 4.4137931 , 4.72413793, 5.03448276, 5.34482759, 5.65517241, 5.96551724, 6.27586207, 6.5862069 , 6.89655172, 7.20689655, 7.51724138, 7.82758621, 8.13793103, 8.44827586, 8.75862069, 9.06896552, 9.37931034, 9.68965517, 10. ]) b array([ 0. , 0.20689655, 0.4137931 , 0.62068966, 0.82758621, 1.03448276, 1.24137931, 1.44827586, 1.65517241, 1.86206897, 2.06896552, 2.27586207, 2.48275862, 2.68965517, 2.89655172, 3.10344828, 3.31034483, 3.51724138, 3.72413793, 3.93103448, 4.13793103, 4.34482759, 4.55172414, 4.75862069, 4.96551724, 5.17241379, 5.37931034, 5.5862069 , 5.79310345, 6. ]) </code></pre> <p>so my code is:</p> <pre><code>for i in a: if i &gt;2.27-1. and i&lt;2.27+1: print i </code></pre> <p>which gives me the elements I want from a.</p> <pre><code>1.31034482759 1.62068965517 1.93103448276 2.24137931034 2.55172413793 2.86206896552 3.1724137931 </code></pre> <p>But I also want the corresponding elements in b. i.e.</p> <pre><code>0.20689655 0.4137931 0.62068966 0.82758621 1.03448276 1.24137931 1.44827586 </code></pre> <p>How do I go from here to get the desired result. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload