Is there a map without result in python?

前端 未结 15 1076
北恋
北恋 2020-12-03 04:33

Sometimes, I just want to execute a function for a list of entries -- eg.:

for x in wowList:
   installWow(x, \'installed by me\')

Sometime

15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 05:18

    You could make your own "each" function:

    
    def each(fn, items):
        for item in items:
            fn(item)
    
    
    # called thus
    each(lambda x: installWow(x, 'installed by me'), wowList)
    
    

    Basically it's just map, but without the results being returned. By using a function you'll ensure that the "item" variable doesn't leak into the current scope.

提交回复
热议问题