In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the advanced skills for using Python lists". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Use Python to filter the list
1. Use the Filter () function
The filter () function takes two arguments: a function and an iterator. In this case, we will define a function and filter a list.
Let's try it with an example! We will start with the list and filter out all items less than 3:
Original_list = [1Jing 2 Jing 3 Jing 4 Jing 5] def filter_three (number): return number > 3 filtered = filter (filter_three, original_list) filtered_list = list (filtered) print (filtered_list) # Returns [4P5]
Let's see what's going on here:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
We defined the original list
Then define a function that takes parameters ("number"). If the number is greater than 3, the function returns True.
We define an item ("filtered") that applies the filter function. Our items have been filtered and are filtered.
Finally, we create "filtered_list" and replace the list function with a filtered object.
two。 Have list understanding
Similarly, we can use list understanding to filter lists. Remember, list understanding is a very simple and quick way to define and modify lists.
Let's take a look at how to accomplish the previous tasks through list understanding:
Original_list = [1, number for number in original_list if number 2, 3, 4, 5] filtered_list = [number for number in original_list if number > 3] print (filtered_list) # Return [4, 5]
From these two examples, we can see that list understanding is a simpler and faster way to filter lists.
Second, modify the list
1. Use the Map () function
The Python Map function allows us to apply the function to each item in the iterable object.
Suppose we have a list and want to return the square of each number. We can use the map () function to do this. Let's take a look at the specific operation:
Original_list = [1Jing 2Jing 3Jing 4Jing 5] def square (number): return number * * 2 squares = map (square, original_list) squares_list = list (squares) print (squares) # Returns [1,4,9,16,25]
Let's discuss what's going on here:
First, we define the original list and a function that returns the square of its parameters ("number").
Then, we create a new variable called "squares", which is the result of the map function, with the function and the original list as its arguments.
Finally, we create another new variable and apply the list function to the squares variable.
This method is a little complicated! Next, let's take a look at the list to understand!
two。 Have list understanding
We can use list understanding to modify list items. This is even an easier and more elegant way to write our code.
Let's have a try!
Original_list = [1Jing 2 for number in original_list 3 Jing 4 Jing 5] squares_list = [number * * 2 for number in original_list] print (squares_list) # Returns [1 Jing 4 Jing 9 Jing 16 Jing 25] 3. Merge the list with the Zip () function
In some cases, you may need to merge two or more lists. That's what the zip () function does: create an object at each index that contains the corresponding elements of the list.
Let's try it with an example:
Original_list = [1Jing 2 Jing 3 Jing 4 Jing 5] reversed_list = original_list [::-1] print (reversed_list) # Returns: [5 Jing 4 Jing 3 Jing 2 Ji 1] IV, upside down list
Python lists are ordered data structures. Therefore, the order of projects is important. Sometimes we may need to reverse the items in the list, which can be easily done using Python slicing.
Let's try it with an example:
Original_list = [1, original_list 2, 3, 4, 5] reversed_list = original_list [::-1] print (reversed_list) # Returns: [5, 4, 4, 3, 2, 1]
5. Check the membership in the list
Sometimes we want to see if an item exists in the list.
We only need to use the in operator to do this.
Games = ['Yankees',' Yankees', 'Cubs',' Blue Jays', 'Giants'] def isin (item, list_name): if item in list_name: print (f "{item} isin the list!") Else: print (f "{item} is not in the list!") Isin ('Blue Jays', games) isin (' Angels', games) # Returns # Blue Jays is on the list! # Angels is not on the list! Find the most common items in the list
If you want to find the most common items in the list. For example, you may record the winners of the positive and negative games in the list and want to know which wins the most.
Games = ['heads',' heads', 'tails',' heads', 'tails'] items = set (games) print (max (items, key = games.count))
Let's see what this code does:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
We define a list with the results of five positive or negative games.
We define a collection of all items in the list. The set () function filters out duplicates in the list
Finally, we apply max () to the item set and use the key parameter as the highest count of elements in the collection.
Expand the list
Sometimes we get a list that contains other lists, that is, tables in the table. You can use the list to understand and easily do this!
Let's try this:
Nested_list = [[1mage2, flat_list 3], [4, 5, 6], [7, 8, flat_list)] flat_list = [i for j in nested_list for i in j] print (flat_list) # Returns [1, 2, 4, 5, 6, 7, 8, 9] VIII, check the uniqueness.
If you need to check that all items in the list are unique, you can use the features of the collection to do so!
A collection in Python is similar to a list (because it is mutable and unordered), but it can only contain unique items.
To do this, we need to use a function to convert the list to a collection and compare the length of the two items:
List1 = [1 list 2 3 4] list2 = [1 list 1 2 3 4] def isunique (list): if len (list) = = len (set (list)): print ('uniform') Else: print ('Not Uniqueues') Isunique (list1) isunique (list2) # Returns # uniqueness # is not the only "what are the advanced tips for using Python lists". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.