简体   繁体   English

在嵌套列表中查找字符串的最后一次出现?

[英]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.我希望能够找到该列表中每个字母的最后一次出现,并能够将 + 1 添加到列表 token_winner 中的每个元素。

For example ['a', 2] is the last occurrence of a.例如 ['a', 2] 是最后一次出现的 a。 I want to add that to the second element in token_winner.我想将它添加到 token_winner 中的第二个元素。 so it will look like: token_winner = [0,1,0,0]所以它看起来像: 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. for 反向循环遍历您的列表。 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.您还必须从列表中的数字中减去 1 以对应于索引,因为 python 的索引从 0 开始,而不是 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:如果您有大量数据,并且您知道要查找多少个不同的可能字符串,并且不想通过所有数据 go,那么在每个循环结束时,您可以添加一个条件来测试您是否'完成,并打破循环:

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.第一个示例循环遍历所有元素,即使它不需要,第二个示例得到相同的结果,但只循环了 16 次。 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:您可以将next()与生成器表达式一起应用:

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您可以使用 set 来存储嵌套列表中的唯一元素

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] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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