extract overlapping arrays from a two-dimensional array using python

Asked 2 years ago, Updated 2 years ago, 16 views

By specifying the number of overlapping arrays from the two-dimensional array li, we are having trouble extracting the array.

 li = [[1, 2, 3], [5, 6, 7], [2, 3, 4, 5], [1, 2, 3], [7, 8, 9], [2, 3, 4, 5], [1, 2, 3], [5, 6, 7]]

For example, there are three overlapping arrays [1, 2, 3], and by specifying o=3, we would like to be able to take out [1, 2, 3].
If o=2, we assume that [5, 6, 7], [2, 3, 4, 5] can be taken out.

Thank you for your cooperation.

python

2022-09-29 22:17

2 Answers

Answer

>>>from collections import Counter
>>li = [[1, 2, 3], [5, 6, 7], [2, 3, 4, 5], [1, 2, 3], [7, 8, 9], [2, 3, 4, 5], [1, 2, 3], [5, 6, 7]]
>>>o=2
>>>c=Counter(tuple(x) for x inli)
>> [list(k)fork, vinc.items() if v==o]
[[2, 3, 4, 5], [5, 6, 7]]


2022-09-29 22:17

It's not convenient for me to count on the list, so I chose a tuple once.
For non-overlapping data, we use set().

li = [[1, 2, 3], [5, 6, 7], [2, 3, 4, 5], [1, 2, 3], [7, 8, 9], [2, 3, 4, 5], [1, 2, 3, 3], [5, 6, 7]
tp=map(tuple,li)
tp

[(1,2,3),
(5, 6, 7),
(2,3,4,5),
(1, 2, 3),
(7, 8, 9),
(2,3,4,5),
(1, 2, 3),
(5, 6, 7)

pattern=tuple(set(tp))
pattern

((2,3,4,5), (5,6,7), (7,8,9), (1,2,3)

qty=map(lambdax:tp.count(x), pattern)
qty

[2,2,1,3]

data=zip(qty, pattern)
data

[(2,(2,3,4,5)),(2,(5,6,7)),(1,(7,8,9)),(3,(1,2,3)]

o=lambdanum:map(lambday:list(y[1]),filter(lambdax:x[0]==num,data))
o(3)

[[1,2,3]

o(2)

[[2,3,4,5], [5,6,7]


2022-09-29 22:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.