简体   繁体   中英

Find element in list n times Python

Given are

a = [1, 4, 2, 5]

b = [[[0, 1]], [[0, 2]], [[0, 3]], [[0, 4]], [[0, 5]], [[0, 6]], [[0, 2]], [[0, 3]], [[0, 4]], [[0, 5]], [[0, 4]],
     [[0, 5]], [[0, 6]], [[0, 4]], [[0, 2]], [[0, 2]], [[0, 4]], [[0, 4]], [[0, 5]], [[0, 5]], [[0, 5]], [[0, 1]],
     [[0, 5]], [[0, 1]], [[0, 1]]]

My goal is to iterate over the list a and to identify ( print ) those elements in b which have the element of a as element at index 1 . The whole thing becomes difficult by the fact that this process may occur exactly three times for each element. After that, the next index in a is to be selected. In concrete terms, the whole thing should look like this at the end:

[[0, 1]]
[[0, 1]]
[[0, 1]]
[[0, 4]]
[[0, 4]]
[[0, 4]]
[[0, 2]]
[[0, 2]]
[[0, 2]]
[[0, 5]]
[[0, 5]]
[[0, 5]]

All elements in b beyond that are to be ignored, even and especially if they occur more than three times.

I have already tried various techniques ( random, while loop, etc.), racked my brain and searched this forum, but I am stuck.

It doesn't matter which elements are selected in b , the main thing is that there are three for each element in a .

This will count and filter your b input on a values using a Counter to count the values and appending to the filtered list only is the value count is less than 3.

from collections import Counter

a = [1, 4, 2, 5]

b = [
    [[0, 1]],
    [[0, 2]],
    [[0, 3]],
    [[0, 4]],
    [[0, 5]],
    [[0, 6]],
    [[0, 2]],
    [[0, 3]],
    [[0, 4]],
    [[0, 5]],
    [[0, 4]],
    [[0, 5]],
    [[0, 6]],
    [[0, 4]],
    [[0, 2]],
    [[0, 2]],
    [[0, 4]],
    [[0, 4]],
    [[0, 5]],
    [[0, 5]],
    [[0, 5]],
    [[0, 1]],
    [[0, 5]],
    [[0, 1]],
    [[0, 1]],
]

c = Counter()

b_filtered = []

b.sort(key=lambda x: x[0][-1])

for x in b:
    v = x[0][-1]
    if v in a and c[v] < 3:
        b_filtered.append(x)
    c[v] += 1

b_filtered contains

[[[0, 1]],
 [[0, 1]],
 [[0, 1]],
 [[0, 2]],
 [[0, 2]],
 [[0, 2]],
 [[0, 4]],
 [[0, 4]],
 [[0, 4]],
 [[0, 5]],
 [[0, 5]],
 [[0, 5]]]

If you want to make it easier, you can simply follow the steps below:

a = [1, 4, 2, 5]

b = [[[0, 1]], [[0, 2]], [[0, 3]], [[0, 4]], [[0, 5]], [[0, 6]], [[0, 2]], [[0, 3]], [[0, 4]], [[0, 5]], [[0, 4]],
     [[0, 5]], [[0, 6]], [[0, 4]], [[0, 2]], [[0, 2]], [[0, 4]], [[0, 4]], [[0, 5]], [[0, 5]], [[0, 5]], [[0, 1]],
     [[0, 5]], [[0, 1]], [[0, 1]]]

for val_a in a:
    counter = 0
    for val_b in b:
       if val_a == val_b[0][1]:
           counter += 1
           if counter < 4:
                print(val_b)

I hope I could help: :D

You may follow these steps to achieve your desired output

a = [1, 4, 2, 5]
b = [[[0, 1]], [[0, 2]], [[0, 3]], [[0, 4]], [[0, 5]], [[0, 6]], [[0, 2]], [[0, 3]], [[0, 4]], [[0, 5]], [[0, 4]],
     [[0, 5]], [[0, 6]], [[0, 4]], [[0, 2]], [[0, 2]], [[0, 4]], [[0, 4]], [[0, 5]], [[0, 5]], [[0, 5]], [[0, 1]],
     [[0, 5]], [[0, 1]], [[0, 1]]]

result = []
count = {}
for i in a:
    for j in b:
        if j[0][1] == i:
            if i not in count:
                count[i] = 1
                result.append(j)
            elif count[i] < 3:
                count[i] += 1
                result.append(j)

for i in result:
    print(i)

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