Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scan previous list values in order to add a new composite list value?

My problem is a bit hard to explain in words so please bear with me while I try my best. I have an array ‘a’ and I’m trying to write a piece of code which will tell when each component is working and if multiple components have failed at once. You can see when a component has failed if it says C1NW as this stands for ‘Component 1 Not Working’ or when a component is working it will say C2W which stands for ‘Component 2 Working’. As you can see from the list 'a' they are already sorted in time order so at time 1067 component 2 isn’t working.

a = [ [1067.8420440505633, 'C2NW'], [1287.3506292298346, 'C1NW'], [1363.9930359848377, 'C2W'], [1483.1371597306722, 'C1W'], [1767.6648314715849, 'C2NW']

Ideally, I would like a ‘Time Line’ array with a ‘Component Working or Not’ array to compare with. Essentially, I would like an array that looks like this:

TimeLine = [1067.8420440505633, 1287.3506292298346, 1363.9930359848377, 1483.1371597306722, 1767.6648314715849]
WorkingOrNot = [ C2NW, C1NWC2NW, C1NWC2W, C1WC2W, C1WC2NW]

The WorkingOrNot array comes from each point so looking at ‘list 2 of a’ you’ll notice that C1 is not working but as C2 still isn’t working yet (List 1 of a shows that C2 isn’t working and it doesn’t start working until list 3 of a) therefore the output should be C1NWC2NW as both components aren’t working at this time. I know I can get the ‘Time Line’ array with:

TimeLine = [item[0] for item in a]

However, I am not sure how to get the other array as this would mean scanning behind the list to check if a certain component was working or not. I’m hoping that I have explained this well enough if not please comment if you would like me to reword as I really struggled to explain this.

Thanks in advance, Ben

like image 770
15002941 Avatar asked Mar 01 '19 23:03

15002941


People also ask

How do I use the list item ID to calculate values?

Below is a screenshot of the entire workflow. In a nutshell, we are using the list item's ID to find the columns of the previous item, add those values to a new item column and then do the calculation. For the sake of this explanation, the workflow is two separate steps. This is not a requirement .

Is it possible to get the previous id of a list?

This is not a requirement . By design the list item number increase in a sequential order hence we can assign a temporary number column called 'Current ID' set the current Id of the list item and then find the ID number of the previous list item.

How to insert an item to a list at a specified index?

You can use the insert () method to insert an item to a list at a specified index. Each item in a list has an index. The first item has an index of zero (0), the second has an index of one (1), and so on. myList = ['one', 'two', 'three'] myList.insert (0, 'zero') print (myList) # ['zero', 'one', 'two', 'three']

How to get last item in a SharePoint list?

Get last item in row and do your calculation. Query SharePoint List returning Top and Order by ID desc - Get Items Action (call it LastItemObject) Update current item's HourRemaining field with output of above compose action. Show activity on this post.


1 Answers

Try this:

import re

a = [ [1067.8420440505633, 'C2NW'], [1287.3506292298346, 'C1NW'], [1363.9930359848377, 'C2W'], [1483.1371597306722, 'C1W'], [1767.6648314715849, 'C2NW'] ]

Timeline, statuses = zip(*a)

WorkingOrNot = []
current_statuses = {}

# Regex pattern to extract component and working/not working status
pattern = re.compile(r'(C\d+)(\w+)')

for status in statuses:
    match = pattern.search(status)
    # Set the key corresponding to component to the status (W or NW)
    current_statuses[match[1]] = match[2]
    WorkingOrNot.append(''.join(map(''.join, sorted(current_statuses.items(), key=lambda kv: int(kv[0][1:])))))

print(WorkingOrNot)
# ['C2NW', 'C1NWC2NW', 'C1NWC2W', 'C1WC2W', 'C1WC2NW']

The code is fairly self-explanatory. The only confusing parts may be

Timeline, statuses = zip(*a)

which is equivalent to

TimeLine = [item[0] for item in a]
statuses = [item[1] for item in a]

And this line:

WorkingOrNot.append(''.join(map(''.join, sorted(current_statuses.items(), key=lambda kv: int(kv[0][1:])))))

This does nothing other than format the output into the desired format form its dictionary form. Sorting the dictionary my component ID, join the key (component) and value ('W' or 'NW'), then join all of that.

like image 138
iz_ Avatar answered Sep 29 '22 02:09

iz_