Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of python list, Dictionary, Loop and judgment

Shulou Source: shulou.com Published: 2022-06-01 20:10:21 09月14日 Update

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!

Tags: Customer quantity type apple loop data banana Lisi Wang Wu dictionary grape analysis way Zhang San code fragrant pear memory loop body speed learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia MariaDB Docker vpn