Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12201

Differences in Initializing Device Objects and Device Objects Index in Python

$
0
0

I am really confused and need help from community seeing this issue, I couldn't figure out where is the issue.

Python 3.10, on Macbook. The sig is a dict from json.My program use objects to do state tracking, and the following code snippet is the portion to do the init. I think the following two methods of code have expressed the same idea, but the outcome are different, specifically, the key of device_objects_idx are identical, but the values of device_objects might be different.

# Begin of Method Adevice_objects = []for controller in sig:    for device_vendor in sig[controller]:        for device_type in sig[controller][device_vendor]:            device_objects.append(                Device(device_vendor, device_type, sig[controller][device_vendor][device_type], True))src_addr_set = set(frames["_ws.col.def_src"].unique())device_objects_idx = {}for addr in src_addr_set:    device_objects_idx[addr] = device_objects.copy()# End of Method A# Begin of Method Bsrc_addr_set = set(frames["_ws.col.def_src"].unique())device_objects_idx = {}for addr in src_addr_set:    device_objects = []    for controller in sig:        for device_vendor in sig[controller]:            for device_type in sig[controller][device_vendor]:                device_objects.append(                    Device(device_vendor, device_type, sig[controller][device_vendor][device_type], True))    device_objects_idx[addr] = device_objects# End of Method B# Begin of Method Csrc_addr_set = set(frames["_ws.col.def_src"].unique())device_objects_idx = {}for addr in src_addr_set:    device_objects_idx[addr] = []    for controller in sig:        for device_vendor in sig[controller]:            for device_type in sig[controller][device_vendor]:                device_objects_idx[addr].append(                    Device(device_vendor, device_type, sig[controller][device_vendor][device_type], True))# End of Method C

The method A given a dict of list of objects correctly, and the calculation afterwards given correct results. I want to move the logic inside the for loop so I can add the addr as a new parameter for the Device init, which is more complicated if I can only do method A. Neither the method B or C works.


Viewing all articles
Browse latest Browse all 12201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>