liuzhidong
liuzhidong

Reputation: 548

How could I use map() in this code?

I have a string list

[str1, str2, str3.....]

and I also have a def to check the format of the strings, something like:

def CheckIP(strN):
    if(formatCorrect(strN)):
        return True
    return False

Now I want to check every string in list, and of course I can use for to check one by one. But could I use map() to make code more readable...?

Upvotes: 1

Views: 63

Answers (3)

user2555451
user2555451

Reputation:

You can map your list to your function and then use all to check if it returns True for every item:

if all(map(CheckIP, list_of_strings)):
    # All strings are good

Actually, it would be cleaner to just get rid of the CheckIP function and use formatCorrect directly:

if all(map(formatCorrect, list_of_strings)):
    # All strings are good

Also, as an added bonus, all uses lazy-evaluation. Meaning, it only checks as many items as are necessary before returning a result.


Note however that a more common approach would be to use a generator expression instead of map:

if all(formatCorrect(x) for x in list_of_strings):

In my opinion, generator expressions are always better than map because:

  1. They are slightly more readable.

  2. They are just as fast if not faster than using map. Also, in Python 2.x, map creates a list object that is often unnecessary (wastes memory). Only in Python 3.x does map use lazy-computation like a generator expression.

  3. They are more powerful. In addition to just mapping items to a function, generator expressions allow you to perform operations on each item as they are produced. For example:

    sum(x * 2 for x in (1, 2, 3))
    
  4. They are preferred by most Python programmers. Keeping with convention is important when programming because it eases maintenance and makes your code more understandable.

  5. There is talk of removing functions like map, filter, etc. from a future version of the language. Though this is not set in stone, it has come up many times in the Python community.

Of course, if you are a fan of functional programming, there isn't much chance you'll agree to points one and four. :)

Upvotes: 6

inspectorG4dget
inspectorG4dget

Reputation: 113955

L = [str1, str2, str3.....]
answer = list(map(CheckIP, L)) 

answer is a list of booleans such that answer[i] is CheckIP(L[i]). If you want to further check if all of those values are True, you could use all:

all(answer)

This returns True if and only if all the values in answer are True. However, you may do this without listifying:

all(map(CheckIP, L)), as, in python3, `map` returns an iterator, not a list. This way, you don't waste space turning everything into a list. You also save on time, as the first `False` value makes `all` return `False`, stopping `map` from computing any remaining values

Upvotes: 1

Marcin
Marcin

Reputation: 238209

An example, how you could do:

in_str = ['str1', 'str2', 'str3', 'not']
in_str2 = ['str1', 'str2', 'str3']


def CheckIP(strN):
    # different than yours, just to show example.
    if 'str' in strN:       
        return True
    else:
        return False

print(all(map(CheckIP, in_str)))   # gives false
print(all(map(CheckIP, in_str2)))  # gives true 

Upvotes: 1

Related Questions