简体   繁体   中英

Find the last occurence of string in a nested list?

fixed_game_b0_1 = [['f', 3], ['a', 2], ['d', 2], ['f', 4],
               ['b', 2], ['a', 2], ['f', 3], ['f', 3],
               ['e', 1], ['b', 2], ['e', 1], ['c', 1],
               ['a', 3], ['d', 3], ['f', 1], ['f', 4],
               ['b', 4], ['b', 1], ['c', 4], ['d', 1],
               ['a', 3], ['e', 1], ['b', 2], ['c', 3],
               ['d', 3], ['c', 2], ['c', 1], ['a', 2],
               ['d', 4], ['b', 4], ['g', 2]]

token_winner = [0, 0, 0, 0] # 1, 2, 3, 4

I have a nested list of strings and integers. I want to be able to find the last occurrence of each letter in that list and be able to add + 1 to each element in the list token_winner.

For example ['a', 2] is the last occurrence of a. I want to add that to the second element in token_winner. so it will look like: token_winner = [0,1,0,0]

fixed_game_b0_1 = [['f', 3], ['a', 2], ['d', 2], ['f', 4],
               ['b', 2], ['a', 2], ['f', 3], ['f', 3],
               ['e', 1], ['b', 2], ['e', 1], ['c', 1],
               ['a', 3], ['d', 3], ['f', 1], ['f', 4],
               ['b', 4], ['b', 1], ['c', 4], ['d', 1],
               ['a', 3], ['e', 1], ['b', 2], ['c', 3],
               ['d', 3], ['c', 2], ['c', 1], ['a', 2],
               ['d', 4], ['b', 4], ['g', 2]]

token_winner = [0, 0, 0, 0] # 1, 2, 3, 4
lastoccurence = 0
for i in range(len(fixed_game_b0_1)):
     if fixed_game_b0_1[i][0] == "a":
          lastoccurence = i
token_winner[fixed_game_b0_1[lastoccurence][1]] += 1
#we get the element fixed_game_b0_1[lastoccurence][1] and we add 1 to it, like #you said. We got two indexes here : fixed_game_b0_1[lastoccurence][1]
# because you got lists in a list : fixed_game_b0_1[i][0] = the letter and
# fixed_game_b0_1[i][1] = the corresponding number

Don't hesitate if you got any questions; ;)

for loop through your list in reverse. You will also have to subtract 1 from the numbers in your list to correspond to the indices, since python's indices start at 0, and not 1.

_found = []
for items in reversed(fixed_game_b0_1):
    string = items[0]
    num = items[1] - 1
    if string not in _found:
        token_winner[num] += 1
        _found.append(string)

print(token_winner)

If you have a ton of data and you know how many different possioble strings you're looking for and don't want to go through all of your data, then at the end of each loop, you can add a conditional to test if you're done, and break the loop:

for items in reversed(fixed_game_b0_1):
    string = items[0]
    num = items[1] - 1
    if string not in _found:
        token_winner[num] += 1
        _found.append(string)
        if len(_found) == 7:
            break

The first example loops through all the elements, even though it doesn't need to, the second example gets the same results but only loops through 16 times. However, to do it, we need to know how many unique strings we have.

For clarity - some of these answers are great answers, but we';re understanding your question differently - you need the last instance of each string, not the last instance of a specific string.

You can apply next() with generator expression:

fixed_game_b0_1 = [
    ['f', 3], ['a', 2], ['d', 2], ['f', 4], ['b', 2], ['a', 2], ['f', 3], ['f', 3],
    ['e', 1], ['b', 2], ['e', 1], ['c', 1], ['a', 3], ['d', 3], ['f', 1], ['f', 4],
    ['b', 4], ['b', 1], ['c', 4], ['d', 1], ['a', 3], ['e', 1], ['b', 2], ['c', 3],
    ['d', 3], ['c', 2], ['c', 1], ['a', 2], ['d', 4], ['b', 4], ['g', 2]
]
token_winner = [0, 0, 0, 0] # 1, 2, 3, 4

token_winner[next(v - 1 for i, v in reversed(fixed_game_b0_1) if i == 'a')] += 1

You can try something like this

mylist = [
    ['f', 3], ['a', 2], ['d', 2], ['f', 4], ['b', 2], ['a', 2], ['f', 3], ['f', 3],
    ['e', 1], ['b', 2], ['e', 1], ['c', 1], ['a', 3], ['d', 3], ['f', 1], ['f', 4],
    ['b', 4], ['b', 1], ['c', 4], ['d', 1], ['a', 3], ['e', 1], ['b', 2], ['c', 3],
    ['d', 3], ['c', 2], ['c', 1], ['a', 2], ['d', 4], ['b', 4], ['g', 2]
]

You can use set for storing the unique elements from the nested list

unique_items = set(i[0] for i in fixed_game_b0_1)
# {'e', 'a', 'b', 'd', 'c', 'g', 'f'}
token_winner = [0, 0, 0, 0] # 1, 2, 3, 4

Then you can iterate the list from last to first for finding the last occurrence and break the loop as soon as you find all the occurrences of unique letters

for i in range(-1, -len(mylist)-1, -1):
    if mylist[i][0] not in unique_items:
        continue
        # moves the control back to the top of the loop. if the char is already found one
    token_winner[mylist[i][1] - 1] = token_winner[mylist[i][1] -1] + 1
    unique_items.remove(mylist[i][0])

    if len(unique_items) < 1:
        break

# output==> token_winner [2, 2, 0, 3] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM