Python: Iterate over list of lists and do something

Question:

I realize that there’s a lot of questions on here on the topic, however, I am unable to find one that helps me achieve a certain logic. I have the following list of lists:

    unq_act = [[15, 'F7'],
 [45, 'F7'],
 [17, 'F7'],
 [19, 'F7'],
 [49, 'F7'],
 [23, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [15, 'F17'],
 [45, 'F17'],
 [16, 'F7'],
 [46, 'F7'],
 [17, 'F7'],
 [18, 'F7'],
 [48, 'F7'],
 [23, 'F7']]

The logic is that I want to:
1- Iterate over the list of lists. If the first element of a list reoccurs, check if the second element did NOT occur for the same first element(e.g. if 15 reoccurs, check if the second element did NOT reoccur).

2- If the above equates to true, I want to update the value by adding 30 to the first element.
For example:
For list [15, ‘F7’], we check if the first element 15 reoccurs in the list which is true for list [15, ‘F17’]. We then check if the second elements are the same. Here, ‘F7’ is not equal to ‘F17’. The , I want to add 30 to 15 making it 45.
This process is iterative, so then when checking for 45 using same condition it would update it to 75 and so on.

Essentially, this list would be converted to:

unq_act = [[15, 'F7'],
 [45, 'F7'],
 [17, 'F7'],
 [19, 'F7'],
 [49, 'F7'],
 [23, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [2, 'F7'],
 [7, 'F7'],
 [11, 'F3'],
 [8, 'F7'],
 [5, 'F7'],
 [6, 'F3'],
 [9, 'F7'],
 [75, 'F17'],
 [105, 'F17'],
 [16, 'F7'],
 [46, 'F7'],
 [17, 'F7'],
 [18, 'F7'],
 [48, 'F7'],
 [23, 'F7']]

See lists that have elements 15 and 45 at the beginning of the list and how they were transformed.
Here is the code I tried which isn’t updating anything in the list:

for i in unq_act:
    for j in unq_act:
        if i[0] == j[0] and i[1] != j[1]:
            unq_act[unq_act.index(j)][0] == unq_act[unq_act.index(j)][0] + 30
            print(unq_act[unq_act.index(j)][0])

Any help is appreciated.

Asked By: HOSSAM

||

Answers:

If I understand the question properly, you want each number to correspond to exactly one string. If a second string is found, the number should be incremented by 30 until it either the string matches a previously found string for the new number, or until the number is unique.

For example, [[1, "F1"], [1, "F1"], [1, "F2"]] should have the last element turned into [31, "F2"].

This should be pretty easy to solve using a dictionary mapping from number to string:

mapping = {}

for inner in list_of_lists:
    n, f = inner                             # unpack the list for convenience
    
    while n in mapping and mapping[n] != f:  # update the number if necessary
        n += 30
        inner[0] = n

    if n not in mapping:                     # add unique numbers to the mapping
        mapping[n] = f
Answered By: Blckknght
s = set()
for l in unq_act:
    sl = frozenset(l)
    if sl in s:
        l[0] += 30
    else:
        s.add(sl)

if it’s possible for more than 1 equale sublists

s = set()
for l in unq_act:
    sl = frozenset(l)
    while True:
        if sl in s:
            l[0]+=30
            sl = frozenset(l)
        else:
            s.add(sl)
            break
    
Answered By: trigonom
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.