[英]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
.我的目标是遍历列表a
并识别( print
) b
中具有a
元素作为index 1
处元素的那些元素。 The whole thing becomes difficult by the fact that this process may occur exactly three times for each element.整个事情变得困难,因为这个过程可能对每个元素恰好发生 3 次。 After that, the next index in a
is to be selected.之后,要选择a
中的下一个索引。 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. b
中除此之外的所有元素都将被忽略,即使它们出现超过 3 次,尤其如此。
I have already tried various techniques ( random, while loop, etc.), racked my brain and searched this forum, but I am stuck.我已经尝试了各种技术(随机、while 循环等),绞尽脑汁搜索了这个论坛,但我被卡住了。
It doesn't matter which elements are selected in b
, the main thing is that there are three for each element in a
.在b
中选择哪些元素并不重要,主要的是 a 中的每个元素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.这将使用Counter
对值进行计数和过滤b
输入, a
计算值并仅在值计数小于 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 b_filtered
包含
[[[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我希望我能帮上忙::D
You may follow these steps to achieve your desired output您可以按照以下步骤来实现您想要的 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.