I have data containing
Код: Выделить всё
[status] [device type] in a list of arrays. Device type can either be A or B. The array looks like this:
sample_list = [["C1-0001", "Offline", "A"], ["C1-0001", "Offline", "B"], ["C1-0002", "Online", "A"]] The list of arrays could be interpreted like this:
[img]https://i.stack.imgur.com/qrab8.png[/img]
I have another list containing [code] only.
The list of codes could be interpreted like this:
[img]https://i.stack.imgur.com/MMdYD.png[/img]
Basically, I want to check the statuses of the list containing [code] only including some other parameters that could be derived from the list of arrays.
All in all, the parameters I want to output are its [solution] [code] [status] [Online A type device] [Offline A type Device] [Online B type device] [Offline B type device].
solution is C if it has only 1 type "A" device. B if otherwise. status is "Online" if even just 1 type of device is on for a particular "code" This kinda sums up the outputs I want:
[img]https://i.stack.imgur.com/iTICb.png[/img]
I was doing stuff differently before using dictionaries and got my solution here: How can I tally my data such that the correspondence of my data simplifies like an OR operation?
But now that I have extra parameters and basically having duplicate [code] on my output which is not allowed in dictionaries, I'm having trouble wrapping my head around the data. Hope you guys could me some ideas.
Edit: I successfully outputted the [solution] [code] and [status] in order. For the online devices on each type, I was thinking if there's something that checks if index 0 hasn't change for an array inside a list if I'm cycling through many lists inside a list as such:
sample_list = [["C1-0001", "Offline", "A"], ["C1-0001", "Offline", "B"], ["C1-0002", "Online", "A"]] Here is my current code on the device statuses:
for i in sites: for j in nested_list if j[0] == i and j[1] == "Online" and j[2] == "A": something elif j[0] == i and j[1] == "Offline" and j[2] == "A": something elif j[0] == i and j[1] == "Online" and j[2] == "B": something elif j[0] == i and j[1] == "Offline" and j[2] == "B": something The sites list are the codes duplicated such that if it contains 2 devices, it will appear twice disregarding the device type.
The nested_list list contains the arrays of [codes] [status] [device type]
Edit: Kinda got somewhere. This is the actual code I currently have.
results = dict() for site, status, device in local_site_status_device_list: pretally = results.setdefault(site, {}) tally = pretally.setdefault(device, {}) tally[status] = tally.get(status, 0) + 1 Basically, this is what I wanted to output:
[img]https://i.stack.imgur.com/qrab8.png[/img]
Since no type "A" or type "B" device is online. The overall status of the site is Offline as seen from the left hand side. At the right side. You can see the type of device that site has. And both sites only have one type "A" device, so it outputs C on the solution used. Hope this clears up what I want for my output. What's left now is the overall tally of these data.
Источник: [url]https://stackoverflow.com/questions/78078528/comparing-and-arranging-data-from-a-list-of-arrays[/url]