In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the related topics of Python lambda". In the daily operation, I believe many people have doubts about the related topics of Python lambda. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the related problems of Python lambda?" Next, please follow the editor to study!
Topic 1 given an array, output two new arrays, each of which is the second and third power of each number.
Enter:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
2 sort the list of dictionaries according to a specified parameter.
For example, sort the following list according to the color of the internal dictionary
Enter:
[{'make':' Nokia', 'model': 216,' color': 'Black'}, {' make':'Mi Max', 'model':' 2march, 'color':' Gold'}, {'make':' Samsung', 'model': 7,' color': 'Blue'}] output:
[{'make':' Nokia', 'model': 216,' color':' Black'}, {'make':' Samsung', 'model': 7,' color': 'Blue'}, {' make':'Mi Max', 'model':' 2arrays, 'color':' Gold'}] 3 given an array, output two new arrays, which are the even and odd numbers, respectively.
Enter:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output:
Even: [2, 4, 6, 8, 10]
Odd: [1, 3, 5, 7, 9]
Answer 1
For this problem, we need to cycle through each number and then calculate the quadratic and cubic powers respectively. Since you're going to use lambda, of course you don't need a for loop. Using lambda syntax, each x in the array is taken as a parameter, and then the function body is calculated directly. Because it is to be mapped to each number to calculate, so use map to convert. Finally, use list to convert to a new list.
Nums = [1,2,3,4,5,6,7,8,9,10]
Square_nums = list (map (lambda x: X * * 2, nums))
Print (square_nums)
Cube_nums = list (map (lambda x: X * * 3, nums))
Print (cube_nums) 2
This topic requires us to sort with the lambda function. You can use the lambda function with the sorted function to do this. Looking at the def sorted (iterable, key, reverse) function, you can achieve the purpose of custom sorting by specifying the second parameter, key, where key is a custom function. That's where we use the lambda function. The original list consists of a series of dictionaries. We take out the color to be sorted in the dictionary one by one and use the lambda function to specify the value of key.
Models = [{'make':'Nokia',' model':216, 'color':'Black'}, {' make':'Mi Max', 'model':'2',' color':'Gold'}, {'make':'Samsung',' model': 7, 'color':'Blue'}]
Print ("before sorting:")
Print (models)
Sorted_models = sorted (models, key=lambda x: X ['color'])
Print ("after sorting:")
Print (sorted_models) 3
This topic is very similar to the first one just now. We need to loop through each number and then filter the even and odd numbers respectively. Even and odd numbers are judged by whether divisible 2 is zero or not. How do you filter it here? Easy, you just need to change the map to filter. Finally, use list to convert to a new list.
Nums = [1,2,3,4,5,6,7,8,9,10]
Even_nums = list (filter (lambda x: X% 2 = = 0, nums))
Print (even_nums)
Odd_nums = list (filter (lambda x: X% 2! = 0, nums))
Print (odd_nums) at this point, the study of "what are the related topics of Python lambda" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.