L
L
lemonlimelike2020-08-24 16:11:41
Python
lemonlimelike, 2020-08-24 16:11:41

Why was the original list not copied?

Why was the original list not copied?

def em(list_data):
    	with open('test2.json','r',encoding='utf-8') as file_json:
    		data_json = []
    		for item in file_json:
    			try:
    				data_json.append(json.loads(item))
    			except Exception as err:
    				print(err)
    	data_result = []
    	copy_list_data = list_data.copy()
    
    	for data in list_data:
    		for item in data_json:
    			if data['idnp'] in item[0] or data['idnp'] == item[0]:
    				data_result.append(item)
    				data.clear()
    				break
    		
    	for data in list_data:
    		if data.get('idnp'):
    			data_result.append(list(data.values()))
    
    	print(copy_list_data)

In the first loop, I get all the lines from the file. In the second loop, I add elements in the order I need to the `data_result` list and then clear what I added. And in `list_data` the already cleared list remains and then what remains I add to the final list.

But why is the cleared list stored in the `copy_list_data` variable after I have cleared the `list_data` list? I copied the original list before cleaning . Is it

possible to write this function somehow differently, without additional loops and variables?
I will explain what I need: The file contains lines of this type
["S18D8402100DY", "\u041a\u0410\u041f\u041e\u0422", "Chery", "0", "15", 11604.06, "BRAN", 11922.12, "TKUC", 11951.53, "FRZA", "None"]

And in the original list `list_data` there are dictionaries and each dictionary has a unique article, key idnp, example
{'idnp': 'F3102000C1', 'name': '', 'brand': 'LIFAN', 'min_count': 0, 'max_time_delivery': 15, 'count_sell': nan}


The initial list contains dictionaries for parsing, as a result, after parsing, the data is saved in json'e to a file. Next, I call this function. But the data that is in the original list and the file diverge. There may be such a thing in the file that there are no records. So I need to get a list that will have the missing entries from the original list. How to do it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2020-08-24
@lemonlimelike

Need to use deepcopy https://docs.python.org/3/library/copy.html#copy.d...

D
Denis Ineshin, 2015-06-11
@We-De

You don't have jQuery included.
Above the script tag, paste this line:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

S
Sergey, 2015-06-11
@Pjeroo

А где сам jQuery?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question