Replacing list comprehensions with pandas and numpy Python

tony selcuk

The function below uses slices to get the maximum value between 2 indexes at once. So it gets the maximum value between 0 and 10 and then 10 and 12 and such. The function is derived from the answer to this post post. Is there a way I could could replace the list comprehensions in the form of a pandas function like pd.Series(). Or if possible do it as a numpy function.

list_ = np.array([9887.89, 9902.99, 9902.99, 9910.23, 9920.79, 9911.34, 9920.01, 9927.51, 9932.3, 9932.33, 9928.87, 9929.22, 9929.22, 9935.24, 9935.24, 9935.26, 9935.26, 9935.68, 9935.68, 9940.5])
indexes = np.array([0,10,12,14])
chunks = np.split(list_, indexes[1:-1])
MAX=([c.max() for c in chunks])
Ehsan

I would recommend this:

MAX = np.maximum.reduceat(list_,indexes[:-1])

output:

array([9932.33, 9929.22, 9940.5 ])

Another way that will NOT increase your performance and is merely a replacement for list comprehension in your answer (in fact, might even be slightly slower):

max = np.vectorize(np.max)
MAX = max(chunks)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

evaluation of python list comprehensions

From Dev

Python list comprehensions nodejs/javascript

From Dev

Python List Comprehensions - Join with For loop

From Dev

Python Reverse Engineer List Comprehensions

From Dev

List comprehensions in python nested loops

From Dev

Python quick question about comprehensions vs list comprehensions

From Dev

How to understand the list comprehensions in making list in python?

From Dev

Python nesting list comprehensions with variable depth

From Dev

Build Dictionary in Python Loop - List and Dictionary Comprehensions

From Dev

Using if, elif, else in List Comprehensions, Python

From Dev

Python list comprehensions to create multiple lists

From Dev

Python Nested List Comprehensions on Multiple Lists

From Dev

python list comprehensions invalid syntax while if statement

From Dev

haskell's "where" in python's list comprehensions

From Dev

Comprehensions in Python to sample tuples from a list

From Dev

Hash function in Python within List comprehensions

From Dev

How list comprehensions are evaluated in python and in what order

From Dev

How do nested list comprehensions work in python?

From Dev

Python convert for/while into list comprehensions using takewhile

From Dev

Python Nested List Comprehensions to create a matrix

From Dev

List comprehensions in Python with mutable state between iterations

From Dev

How does python optimize conditional list comprehensions

From Dev

Clean solution for missing values in python list comprehensions

From Dev

Using if, elif, else in List Comprehensions, Python

From Dev

How to simplify repetitive list comprehensions in python?

From Dev

Python nesting list comprehensions with variable depth

From Dev

How do nested list comprehensions work in python?

From Dev

Python convert for/while into list comprehensions using takewhile

From Dev

Python list comprehensions with regular expressions on a text

Related Related

  1. 1

    evaluation of python list comprehensions

  2. 2

    Python list comprehensions nodejs/javascript

  3. 3

    Python List Comprehensions - Join with For loop

  4. 4

    Python Reverse Engineer List Comprehensions

  5. 5

    List comprehensions in python nested loops

  6. 6

    Python quick question about comprehensions vs list comprehensions

  7. 7

    How to understand the list comprehensions in making list in python?

  8. 8

    Python nesting list comprehensions with variable depth

  9. 9

    Build Dictionary in Python Loop - List and Dictionary Comprehensions

  10. 10

    Using if, elif, else in List Comprehensions, Python

  11. 11

    Python list comprehensions to create multiple lists

  12. 12

    Python Nested List Comprehensions on Multiple Lists

  13. 13

    python list comprehensions invalid syntax while if statement

  14. 14

    haskell's "where" in python's list comprehensions

  15. 15

    Comprehensions in Python to sample tuples from a list

  16. 16

    Hash function in Python within List comprehensions

  17. 17

    How list comprehensions are evaluated in python and in what order

  18. 18

    How do nested list comprehensions work in python?

  19. 19

    Python convert for/while into list comprehensions using takewhile

  20. 20

    Python Nested List Comprehensions to create a matrix

  21. 21

    List comprehensions in Python with mutable state between iterations

  22. 22

    How does python optimize conditional list comprehensions

  23. 23

    Clean solution for missing values in python list comprehensions

  24. 24

    Using if, elif, else in List Comprehensions, Python

  25. 25

    How to simplify repetitive list comprehensions in python?

  26. 26

    Python nesting list comprehensions with variable depth

  27. 27

    How do nested list comprehensions work in python?

  28. 28

    Python convert for/while into list comprehensions using takewhile

  29. 29

    Python list comprehensions with regular expressions on a text

HotTag

Archive