Shortest way of counting?

Superbest

Let's say I have a list of MyClass. I want to count the number of elements which have MyClass.SomeProperty set to True (assuming SomeProperty is a boolean which is always True or False). My concerns are:

  • It should run fast
  • The code should not be confusing to read
  • It should work for any kind of condition (not just booleans)

I know I can do:

count = len([e for e in MyList if e.SomeProperty])  # For non booleans, something like e.SomeProperty == MyValue

But it seems inefficient.

  • Why type a meaningless thing like e for e?
  • Why create a whole list when you just want to count them?

Is it possible to do better?

shx2

You can use sum with a generator expression.

count = sum( e.SomeProperty for e in MyList )

Or for a general predicate p:

count = sum( p(e) for e in MyList )

This makes use of the fact True and False can be used as the integers 1 and 0, and the fact a generator is used will prevent a new list from being created.

If you insist on avoiding the for e in part, you can use map and attrgetter:

import opertor
count = sum(map(operator.attrgetter('SomeProperty'), MyList))

Or for a general predicate p:

count = sum(map(p, MyList))

However, this is less pythonic. I'd recommend the first approach.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Shortest way to deserialize XmlDocument

From Dev

Shortest way to initialize a dictionary

From Dev

Shortest way to create a date

From Dev

Shortest way to create a date

From Dev

Shortest way to create a DIV

From Dev

Shortest way to read this file line?

From Dev

How to sort JTable in shortest way?

From Dev

Shortest way to linearize a list in Python

From Dev

Shortest way of checking if Double is "NaN"

From Dev

Shortest way to access json key

From Dev

Shortest way to save DataTable to Textfile

From Dev

Printing the length of shortest way in maze

From Dev

shortest way to replace characters in a variable

From Dev

Shortest way to read this file line?

From Dev

Applescript - New file SHORTEST WAY

From Dev

Shortest way to download from GitHub

From Dev

Efficient way of counting True and False

From Dev

better way of counting unique item

From Dev

Efficient way of counting True and False

From Java

Shortest possible way to get "id" from JSONObject

From Dev

FSharp-- Shortest Way to Filter Out Nones?

From Dev

Shortest way to invoke a method with parameters in C#

From Dev

Gnuplot: Shortest way to ignore first line in datafile

From Dev

Shortest way to write immutable struct in C#

From Dev

Shortest way to replace parts of strings in NumPy array

From Dev

Python - Shortest way to format a string to an url

From Dev

Get longest and shortest string in a esthetical way

From Dev

The shortest way to create a array in descending order in Swift

From Dev

Effective and shortest way to convert array A[] to ArrayList<B>