13 Respuestas
+ 15
malbar3353 Yes you can do calculation and filtering in list with the help of map,filter,list comprehension, etc ways...
Map:
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
result = map(lambda x, y: x + y, numbers1, numbers2)
print(list(result))
#output :- [5,7,9]
Filter:-
seq = [0, 1, 2, 3, 5, 8, 13]
result = filter(lambda x: x % 2 != 0, seq) #filtering odd numbers from seq list
print(list(result))
#output :- [1, 3, 5, 13]
+ 10
Ervis Meta I know but malbar3353 only wants to know how to do simple calculations in lists....
+ 7
malbar3353 Lamdas is similar to def() that create functions..... Sorry for tough examples you can also do calculation in list by :-
Adding 2 to all elements of a list let's say lista:-
lista = [1,5,7,9,5]
for i in range(len(lista)-1):
lista[i] += 2
print(lista)
#output :- [3,7,9,11,7]
For printing sum of all elements in list:-
lista = [1,5,7,9,5]
print(sum(lista))
#output :- 27
+ 4
instead of using map() / filter() and lambdas, you can use list comprehension or even the sum() builtin function:
evens = [ 2, 4, 6, 8 ]
odds = [v-1 for v in evens]
nums = [evens[i]*odds[i] for i in range(len(odds))]
less42 = [v for v in nums if v<42]
https://python-reference.readthedocs.io/en/latest/docs/comprehensions/list_comprehension.html
https://www.w3schools.com/python/python_lists_comprehension.asp
+ 3
Calvin Thomas sometimes it could be more advisable to use map/filter (to improve readability as most obvious example -- in that case, it would be advisable to use 'normal' function or lambdas assigned to variables rather than having lambdas inlined)...
however, that's also a question of coding style / preference ^^
+ 2
Calvin Thomas yes, me too, when possible without too much readability downside...
in addition, list comprehension is slightly more efficient than calling functions ^^
+ 1
Though example 😅😅 Pallavi Bhardwaj if I get it I need to use the keyword map to do calculation ? What the word lambda is used for ?
+ 1
The map() function is used to simplyfy aplying of a specific function over an iterable.
I usually use it in this form :
map( func, iter )
Where func represents a function and iter an iterable; ( list, dictionary, set, tuple, ect)
As lambda defines an 'on-fly' function, it is more suitable for using simple operations over an iterable (list) like adding or simple concatenation.
filter() function does the same, but instead of returning an transformed iterable, it returns an filtered one from conditions of the function:
filter( func, iter )
Note : To save the results of those functions use list() function or list comprehesion :
list(map(lambda x: x + 2, [1,2,3,4]))
[ i for i in map(lambda x: x + 2, [1,2,3,4])]
+ 1
Thank you Pallavi Bhardwaj Ervis Meta
+ 1
lst = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
#add something
print([n + 3 for n in lst])
#sum of lst members
print(sum(lst))
0
visph What's the point of using filter and map if everything can be done with a simple list comprehension?
0
visph Oh, now I get you. Thanks. I still prefer comprehension though.
0
Thanks Angela 😁