Questions tagged [any]

"Any" means "at least one". Use this tag for questions that deal with testing elements in a collection to see if at least one of them passes a condition.

682 questions
152
votes
3 answers

Check if list of objects contain an object with a certain attribute value

I want to check if my list of objects contain an object with a certain attribute value. class Test: def __init__(self, name): self.name = name # in main() l = [] l.append(Test("t1")) l.append(Test("t2")) l.append(Test("t2")) I want a…
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
94
votes
3 answers

Good way to get *any* value from a Java Set?

Given a simple Set, what is a good way (fast, few lines of code) to get any value from the Set? With a List, it's easy: List things = ...; return things.get(0); But, with a Set, there is no .get(...) method because Sets are not ordered.
Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
90
votes
8 answers

any() function in Python with a callback

The Python standard library defines an any() function that Return True if any element of the iterable is true. If the iterable is empty, return False. It checks only if the elements evaluate to True. What I want it to be able so specify a callback…
Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90
88
votes
8 answers

JavaScript/jQuery equivalent of LINQ Any()

Is there an equivalent of IEnumerable.Any(Predicate) in JavaScript or jQuery? I am validating a list of items, and want to break early if error is detected. I could do it using $.each, but I need to use an external flag to see if the item was…
dilbert
  • 881
  • 1
  • 6
  • 3
87
votes
2 answers

Using any() and all() to check if a list contains one set of values or another

My code is for a Tic Tac Toe game and checking for a draw state but I think this question could be more useful in a general sense. I have a list that represents the board, it looks like this: board = [1,2,3,4,5,6,7,8,9] When a player makes a move,…
DasSnipez
  • 2,182
  • 4
  • 20
  • 29
83
votes
2 answers

scala - Any vs underscore in generics

What is the different between the following Generics definitions in Scala: class Foo[T <: List[_]] and class Bar[T <: List[Any]] My gut tells me they are about the same but that the latter is more explicit. I am finding cases where the former…
Sean Connolly
  • 5,692
  • 7
  • 37
  • 74
72
votes
11 answers

Difference between IN and ANY operators in SQL

What is the difference between IN and ANY operators in SQL?
Jagan
  • 4,649
  • 19
  • 60
  • 70
42
votes
3 answers

Difference between any/interface{} as constraint vs. type of argument?

As generics have been released in Go 1.18 pretty recently, I've started learning them. I generally get the concept, because I have some Java experience from the past. But I don't get some implementation specifics. For instance: when it's more…
Dmytro Titov
  • 2,802
  • 6
  • 38
  • 60
42
votes
3 answers

PostgreSQL - IN vs ANY

I have tried both: smthng = ANY (select id from exmplTable) smthng IN (select id from exmplTable) and I am getting the same results for my data. Is there any difference for the two expressions?
PROvlima
  • 541
  • 1
  • 4
  • 9
36
votes
1 answer

when use JS .includes() vs .some()?

some() - if it finds an array element, the function returns a true and does not check the remaining values. while... includes() - for all elements in the array does a generic compare and return true if it finds the match or at the first match? When…
34
votes
2 answers

Check if a pandas Series has at least one item greater than a value

The following code will print True because the Series contains at least one element that is greater than 1. However, it seems a bit un-Pythonic. Is there a more Pythonic way to return True if a Series contains a number that is greater than a…
ChaimG
  • 7,024
  • 4
  • 38
  • 46
32
votes
2 answers

Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?

I have this code (the whole code is not important but can be seen on this link): internal static class PlayCardActionValidator { public static bool CanPlayCard(...) { // ... var hasBigger = playerCards.Any( …
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
30
votes
5 answers

How to check if any value of a column is in a range (in between two values) in Pandas?

I have a DataFrame, and I would like to check if any of the values (v) of a column satisfies x<=v<=y. equal = any(df['columnX'] == value) # No problems here in_between = any(x <= df['columnX'] <= y) # ValueError :/ The error I get is ValueError:…
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
27
votes
2 answers

How to achieve python's any() with a custom predicate?

>>> l = list(range(10)) >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> if filter(lambda x: x > 10, l): ... print "foo" ... else: # the list will be empty, so bar will be printed ... print "bar" ... bar I'd like to use any()…
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
25
votes
4 answers

Protobuf 3.0 Any Type pack/unpack

I would like to know how to transform a Protobuf Any Type to the original Protobuf message type and vice versa. In Java from Message to Any is easy: Any.Builder anyBuilder = Any.newBuilder().mergeFrom(protoMess.build()); But how can I parse that…
Overholt
  • 867
  • 1
  • 10
  • 23
1
2 3
45 46