Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14155

List comprehension instead of using append in for loop

$
0
0

I want to use list comprehension instead of using append method because it make the code runs faster however when it is a numpy array it doesn't work.

import numpy as npa = np.array([1, 2 , 4])b = np.array([6, 5 , 2])A = []B = []for i in range(0, 3):    if a[i] > value1 and b[i] > value1:        A.append(a[i])        B.append(b[i])

for example I am trying something like this:

A = [a[i] for i in range(1, 3) if a[i] > value1 and b[i] > value1]

and finally is there a way to define a general function that append elements from an array of numbers in a new list based on if conditions like the mentioned example.


Viewing all articles
Browse latest Browse all 14155

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>