1

I have the following code:

a = '0'
b = '256'
mod_add = ['300', '129', '139']
list(map(lambda a, b, x: (a < x) and (x < b), a, b, mod_add))

I'd like to check every element in mod_add, but

list(map(lambda a, b, x: (a < x) and (x < b), a, b, mod_add))

returns only one False. With some values (a = '100', b = '200') it returns 'False', 'False', 'False'.

What am I doing wrong?

1
  • 1
    Are you intending to compare strings or would you like to compare the integer values? E.g., 20 < 100 but '20' > '100' Commented Sep 14, 2016 at 13:21

4 Answers 4

2

If you really have to use map:

list(map(lambda x: (a < x) and (x < b), mod_add))

Edit:

In response to the desire to map only one element from the list, it really doesn't make such sense to me to do that. But if that's what you wish to do, you can try:

list(map(lambda x: (a < x) and (x < b), [mod_add[0]]))

I hope this helps.

Sign up to request clarification or add additional context in comments.

7 Comments

thank you! I also tried to slightly change it (if I need to check only one element for example, but it returns a error: >>> list(lambda x: (a < x) and (x < b), mod_add[0]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list() takes at most 1 argument (2 given)
It really does not make sense to map a single element of a list. But you can , if you really insist, do: list(map(lambda x: (a < x) and (x < b), [mod_add[0]])).
I mean, I don't use map() now - I would like to check single element. I'm using list(lambda x: (a < x) and (x < b), mod_add[0]), still I see error ' list() takes at most 1 argument (2 given)'. (I already changed a and b to integers (and mod_add to list of integers too))
yes, it works fine. but it still returns error when i try to get rid of map() - can you please explain why it happens?
If you want compare mod_add[0] with a and b then just do (a < mod_add[0]) and (mod_add[0] < b). You really don't need map() for that.
|
2

a and b are strings, they will be rightly treated as iterables by map, not constants as you intend. You should either use a list comprehension or not pass a and b as parameters to map:

>>> [a < x < b for x in mod_add]
[False, True, True]

Comparisons can be chained arbitrarily, so (a < x) and (x < b) can be replaced with a < x < b


Comparing integers instead of strings (which is probably what you want) is just another step away:

>>> [int(a) < int(i) < int(b) for i in mod_add]
[False, True, True]

2 Comments

Just intersted, is there way to check it with map(), if I change type to int?
@grindelwaldus Yes, I don't see why not
0

This is really the kind of thing you should prefer list comprehensions for.

min_, max_ = '0', '256'
# do you mean for these to be compared lexicographically?!
# be aware that '0' < '1234567890' < '256' is True

mod_add = ['300', '129', '139']

result = [min_ < mod < max_ for mod in mod_add]
# [False, True, True]

map filter and reduce (now functools.reduce) are mighty tools to be sure, but Python tends to shy away from them in favor of more verbose, easier to read expressions.

Comments

0
a = '0'
b = '256'
mod_add['300', '129', '139']
map(lambda x: int(a)<int(x)<int(b), mod_add)

Output :

[False, True, True]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.