In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what is the grammar of python list derivation. I hope you will gain something after reading this article. Let's discuss it together.
List derivation
List derivation can replace annoying loops in list filling, and its basic syntax is
[expression for item in list if conditional]
Let's look at a very basic example of populating a list with a sequence of numbers:
Mylist = [i for i inrange (10)] print (mylist) # [0, 1, 2, 3, 4,5,6,7,8,9]
Because you can use expressions, you can also do some mathematical operations:
Squares = [x**2for x inrange (10)] print (squares) # [0,1,4,9 people 16, 25, 36, 49, 64, 81]
You can even call external functions:
Defsome_function (a): return (a + 5) / 2 my_formula= [some_function (I) for i inrange (10)] print (my_formula) # [2.5,3.0 dint 3.5,4.0 4.5, 5.0, 5.5, 6.0, 6.5, 7.0]
Finally, you can use the if function to filter the list. In this case, only values that are divisible by 2 are retained:
Filtered = [i for i inrange (20) if I% 2 examples 0] print (filtered) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] after reading this article, I believe you have a certain understanding of "what is the grammar of python list derivation". If you want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!
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.