So I have a list with multiple list in it which represent something like coordinates. In my case they are positions on a chessboard. The list would look something like this: [(3, 3), (4, 3), (5, 3), (6, 3), (3, 4), (4, 4), (5, 4), (6, 4), (3, 5), (4, 5)] This is just an example. My problem is, I need to check if any of these coordiantes are out of a certain range, on the chessboard, for example 1-8. Unfortunately i could only get the all() command to work with a list which just consists of numbers and not a list with lists of numbers.
Asked
Active
Viewed 70 times
0
-
Couple of things: first, I assume you mean `tuple` when you're referring to `(4, 4)` etc, or are you suggesting that there's another list similar to `[(3, 3), (4, 3), (5, 3), (6, 3), (3, 4), (4, 4), (5, 4), (6, 4), (3, 5), (4, 5)]` and they're both together in another list? Second, what is supposed to happen if one of the tuples has a value exceeding 8? – roganjosh Feb 06 '17 at 18:55
2 Answers
1
Then iterate through each of the individual coordinates:
>>> coords = [(3, 3), (4, 3), (5, 3), (6, 3), (3, 4), (4, 4), (5, 4), (6, 4), (3, 5), (4, 5)]
>>> all(1 <= c <= 8 for coord in coords for c in coord)
True
Let's try two cases where there is a coordinate out of range:
>>> coords = [(3, 3), (4, 3), (5, 3), (6, 3), (3, 4), (0, 5), (4, 4), (5, 4), (6, 4), (3, 5), (4, 5)]
>>> all(1 <= c <= 8 for coord in coords for c in coord)
False
>>> coords = [(3, 3), (4, 3), (5, 3), (6, 3), (4, 88), (3, 4), (4, 4), (5, 4), (6, 4), (3, 5), (4, 5)]
>>> all(1 <= c <= 8 for coord in coords for c in coord)
False

juanpa.arrivillaga
- 88,713
- 10
- 131
- 172
-1
you can import numpy module and use function max
import numpy as np
>>> l =np.array([(3, 3), (4, 3), (5, 3), (6, 3), (3, 4), (4, 4), (5, 4), (6, 4), (3, 5), (4, 5)])
>>> l.max()
6

Jan Sila
- 1,554
- 3
- 17
- 36
-
I didn't downvote - but `numpy` seems like overkill, especially since OP did not request a `numpy` solution and may not even have `numpy` (it is not part of the standard library). – juanpa.arrivillaga Feb 06 '17 at 19:12