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 explains "how Python merges two list or dict". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Python merges two list or dict.
Merge list
If there are two list, they are:
X = [1, 2, 3]
Y = [4, 5, 6]
Want to get the result of the merger: Z = [1, 2, 3, 4, 5, 6]
What should I do?
This is relatively simple, we use z=x+y to merge the list directly:
In [1]: X = [1pm 2je 3]
In [2]: y = [4, 5, 5, 6]
In [3]: z=x+y
In [4]: z
Out [4]: [1, 2, 3, 4, 5, 6]
Merge dict
Similar to the previous question, if we want to merge the two dictionaries xQuery y, merge them into z. How does this work? You should remember that dict has a way for update to update the dictionary, but to update the dictionary instead of generating a merged result. If the two are added directly, the dictionary will report an error.
In [5]: X = {'averse: 1,' baked: 2}
In [6]: y = {'baked: 10,' cased: 11}
In [7]: Z = x.update (y)
In [8]: print (z)
None
In [9]: z=x+y
TypeError Traceback (most recent call last)
In
-> 1 z=x+y
TypeError: unsupported operand type (s) for +: 'dict' and' dict'
Obviously, this does not meet the requirements. So how do you do it?
Very cleverly, Python3.5 then completes the unpacking (unpacking, unpacking, or deconstructing) operation of *.
We directly add two * in front of the dictionary, and then create a new dictionary with a pair of {}.
In [10]: Z = {* * x, * * y}
In [11]: z
Out [11]: {'averse: 1,' baked: 10, 'cased: 11}
Expansion
When you go back to the original list and add it up, there is actually another way to do it with *.
In [12]: X = [1, 2, 5, 3]
In [13]: y = [4, 5, 5, 6]
In [14]: Z = [* x _ pr _ r _ y]
In [15]: z
Out [15]: [1, 2, 3, 4, 5, 6]
Moreover, this approach is not limited to merging list, but can also be merged with mixed tuple.
In [16]: a = [7, 8, 8, 9]
In [17]: B = (11pr 22pr 33)
In [18]: Z = [* x _
In [19]: z
Out [19]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33]
If there are duplicates in your list that need to be removed, just use it with set.
Note the number of * and the type of outer parentheses.
The PEP recommends extending the use of the * iterable unpacking operator and the * * dictionary unpacking operator to allow unpacking in more locations, any number of times, and other situations. Especially in function calls, understanding and generator expressions, and display.
At this point, I believe you have a deeper understanding of "how Python merges two list or dict". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.