Skip to content
Advertisement

How to change values in a list with respect to other list?

I have 2 lists:

JavaScript

So in these 2 lists,

For list a there are values repeating in groups of AA,BB and CC for those same repeated value’s index I want to change values in list c.

In list c, I want to change values according to group AA’s,BB’s,CC’s index in such a way that whichever value is repeating maximum number of times replaces other values in that group AA’s,BB’s,CC’s index with the value which is being repeated maximum times.

JavaScript

Because AA is repeated four times we checked first 4 values in list C and replaced all values which was being repeated the most. Same for BB and CC.

In example 2: I want to keep empty strings ” as it is and remaining logic should be same as example 1. Empty strings are repeated four times ” in input which should remain same in expected output and rest logic should be same for remaining non empty string values.

Advertisement

Answer

itertools.groupby with zip gives you slices of c based on consecutive equal values in a. With collections.Counter the most frequent value can be determined per group.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement