0

I have this Matrix=[['1', '2', '3'], ['4', 'a', 'n']]

Im doing this:

Matrix=[arr.split() for arr in Matrix]
Matrix=[list(map(int, arr)) for arr in Matrix]

as you can see I have 'a' and 'n' there, I want to stop the process and raise a flag like con=false everytime I got a char inside the Matrix, how do I do that?

Alexander
  • 1,288
  • 5
  • 19
  • 38
  • 4
    You can't. It's "buried" in the list comprehension. You'll need to go back to a `for` loop with a `try`/`except` and print the exceptional values. – roganjosh Mar 19 '19 at 23:16
  • can I have a snippet please? – Alexander Mar 19 '19 at 23:19
  • 2
    I find it remarkably unlikely that you couldn't unpack your list comprehension here into a bog-standard `for` loop so I'm not sure what you're asking for – roganjosh Mar 19 '19 at 23:21

2 Answers2

2

One solution is to declare a "better" casting function and call it instead of int in map:

matrix = [['1', '2', '3'], ['4', 'a', 'n']]

def int_with_default(value, default="NaN"):
    try: 
        return int(value)
    except ValueError: 
        return default

matrix = [list(map(int_with_default, arr)) for arr in matrix]

The output matrix will be [[1, 2, 3], [4, 'NaN', 'NaN']]. Note that you could also use math.nan instead of this arbitrary string I used as an example.

cglacet
  • 8,873
  • 4
  • 45
  • 60
  • 1
    No reason to add `numpy` here; https://stackoverflow.com/a/944733/4799172 – roganjosh Mar 19 '19 at 23:27
  • I use numpy so often I wasn't even aware python had its own representation of `nan`. Now I'm wondering why numpy doesn't use this directly. – cglacet Mar 19 '19 at 23:28
  • Because `math.isnan()` is a CPython function and would require calling all of that code to do the check. If `numpy` keeps its own version of nan then it doesn't require CPython – roganjosh Mar 19 '19 at 23:32
  • I would like to hear more about this but I can't find anything that talks about that choice, do you have any ref? (Strangely `dir(math.nan) == dir(np.nan)` evaluate to `True`, but sadly `math.nan == np.nan` doesn't, luckily `math.isnan(np.nan)` evaluates to `True`.) – cglacet Mar 19 '19 at 23:39
  • 1
    Well, `np.nan == np.nan` is also `False`. That's [by design](https://en.wikipedia.org/wiki/NaN#Comparison_with_NaN) hence the need for methods to identify `NaN` rather than check for equality. Your choice would be dictated by the libraries you are using and there is _no need_ to introduce `np.nan` here because the OP didn't say they were using it. – roganjosh Mar 19 '19 at 23:43
  • That makes sense indeed. I agree that there is point in using `np.nan` here, that's why I updated my suggestion as soon as you pointed out the existence of `math.nan`. – cglacet Mar 19 '19 at 23:46
0

If you have only positive integers you can use the following listcomp:

m = [['1', '2', '3'], ['4', 'a', 'n']]

[list(map(lambda x: int(x) if x.isdigit() else None, row)) for row in m]
# [[1, 2, 3], [4, None, None]]
Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73