In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "python how to achieve dictionary derivation and set derivation", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "python how to achieve dictionary derivation and set derivation" this article.
Dictionary derivation (Dictionary comprehensions) and set derivation (Set comprehensions)
Most Python programmers know and have used list derivation (list comprehensions). If you are not familiar with the concept of list comprehensions-a list comprehension is a shorter, concise way to create a list.
> some_list = [1,2,3,4,5] > > another_list = [x + 1 for x in some_list] > another_list [2,3,4,5,6]
Since python 3. 1 (or even Python 2. 7), we can use the same syntax to create collections and dictionary tables:
> > # Set Comprehensions > some_list = [1, 2, 3, 4, 5, 2, 5, 1, 4, 8] > > even_set = {x for x in some_list if x% 2 = = 0} > even_setset ([8,2,4]) > # Dict Comprehensions > d = {x: X% 2 = = 0 for x in range (1,11)} > > d {1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False, 10: True}
In the first example, we create a set with non-repeating elements based on some_list, and the set contains only even numbers. In the example of the dictionary table, we create an integer between 1 and 10 that key is not repeated, and value is Boolean to indicate whether key is even.
Another thing worth noting here is the literal representation of the set. We can simply create a collection in this way:
> my_set = {1,2,2,3,4} > my_setset ([1,2,3,4])
Instead of using the built-in function set ().
These are all the contents of the article "how python implements dictionary derivation and set derivation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.