In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "python list, dictionary, loop, judgment example analysis". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "python list, dictionary, loop, judgment example analysis" bar!
Practice case
The following is an in-depth study of python through a simplified virtual case. The sales data of an orchard are as follows:
Sell_list = [{'customer': 'Zhang San', 'category': 'Apple', 'quantity': 100}, {'customer':'Li Si', 'type': 'Apple', 'quantity': 200}, {'customer':'Li Si', 'type': 'fragrant pear', 'quantity': 200}, {'customer':'Li Si' 'species': 'fragrant pears', 'quantity': 300}, {'customer': 'Zhang San', 'type': 'Apple', 'quantity': 100}, {'customer': Wang Wu', 'species': 'banana', 'quantity': 500}, {'customer': 'Wang Wu', 'species': 'banana', 'quantity': 150} {'customer': 'Wang Wu', 'species': 'banana', 'quantity': 150}, {'customer': 'Wang Wu', 'type': 'Apple', 'quantity': 500}, {'customer': 'Zhao Liu', 'species': 'grape', 'quantity': 300}, {'customer': 'Lisi', 'type': 'grape' 'Qty': 300}, {'customer': 'Zhao Liu', 'type': 'Apple', 'Qty': 300},]
Customers want to organize the data and expect the format of the dataset to be similar to the following:
Result_list = [['Zhang San', {'Apple': 300,200,}], ['Li Si', {'Apple': 100,200,}], ['Wang Wu', {'Apple': 100,200,}],] demand analysis
The customer raw data pool is a list, and the list elements are customers, fruit names, and quantities. The requirement now is to summarize the list elements and get a summary table of customers, fruit names, and quantities.
First, traverse the original data pool to determine the customer name:
If the customer name does not exist in the dataset, the customer name is added to the dataset, and the fruit name and quantity are added to the dataset in dictionary format.
If the customer name exists in the dataset, determine the fruit name:
If the fruit name exists in the dataset, the quantity is accumulated.
If the fruit name does not exist in the dataset, add the fruit name and quantity.
Through the above analysis, there is a general framework, through 2-layer nested loop plus judgment to write code.
Specific code
The code I envisioned has two scenarios:
The list implementation of layer 2 loop nested if has the advantage of saving memory, but the disadvantage is that the speed is slightly slower:
Result_list = [] for i in sell_list: for j in result_list: if j [0] = I ['customer']: if I ['species'] not in j [1]: J [1] [I ['species']] = I ['quantity'] else: J [1] [I ['species']] = J [1] [I ['type']] + I ['quantity'] break else: result_list.append ([I ['customer'] {I ['species']: I ['quantity']) for i in result_list: print (I) out: ['Zhang San', {'Apple': 200}] ['Li Si', {'Apple': 200,500 'fragrant pear', 'grape': 300}] ['Wang Wu', {'banana': 800,' Apple: 500}] ['Zhao Liu', {'Grape': 'Apple': 300}]
One thing to pay special attention to in the above code is that the break in the body of the loop skips the entire body of the loop (including the else part of the body). Many textbooks have not been very thorough on this point.
The field implementation of single-layer loop nested if has the advantages of high speed and memory consumption:
Result_dict = {} for i in sell_list: if I ['customer'] in result_dict: if I ['type'] not in result_dict [I ['customer']]: result_dict [I ['customer']] [I ['type']] = I ['quantity'] else: result_dict [I ['customer']] [I ['category'] ']] + = I [' quantity'] else: result_dict [I ['customer']] = {I ['type']: I ['quantity']} for I J in result_dict.items (): print (I, j) out: Zhang San {'Apple': 200} Li Si {'Apple': 200,' fragrant pear': 500,300} Wang Wu {'banana': 800,500} Zhao Liu {'grape': 300,300}
In the dictionary implementation, there seems to be one less layer of loop than the list implementation. In fact, the purpose of this loop is "if I ['category'] not in result_dict [I ['customer]]:" this part has been implemented. To find members in the container, the dictionary is the fastest and most memory-intensive way.
At this point, I believe you have a deeper understanding of "python list, dictionary, loop, judgment example analysis". 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.