[英]How do I get the index of the common integer element from two separate lists and plug it to another list?
I have 3 lists.我有 3 个清单。
A_set = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Q_act = [2, 3]
dur = [0, 4, 5, 2, 1, 3, 4, 8, 2, 3]
All lists are integers.所有列表都是整数。
What I am trying to do is to compare Q_act
with A_set
then obtain the indices of the numbers that match from A_set
.我要做的是将
Q_act
与A_set
进行比较,然后从A_set
获取匹配的数字的索引。
(Example: Q_act
has the elements [2,3]
it is located in indices [1,2]
from A_set
) (示例:
Q_act
具有元素[2,3]
它位于A_set
的索引[1,2]
中)
Afterwards, I will use those indices to obtain the corresponding value in dur
and store this in a list called p_dur_Q_act
.之后,我将使用这些索引来获取
dur
中的相应值并将其存储在名为p_dur_Q_act
的列表中。
(Example: using the result from the previous example, [1,2]
(示例:使用上一个示例的结果,
[1,2]
The values in the dur
list corresponding to the indices [1,2]
should be stored in another list called p_dur_Q_act
dur
列表中对应于索引[1,2]
的值应存储在另一个名为p_dur_Q_act
的列表中
ie [4,5]
should be the values stored in the list p_dur_Q_act
)即
[4,5]
应该是存储在列表p_dur_Q_act
中的值)
So, how do I get the index of the common integer element (which is [1,2]
) from two separate lists and plug it to another list?那么,如何从两个单独的列表中获取常见 integer 元素(即
[1,2]
)的索引并将其插入另一个列表?
So far here are the code(s) I used:到目前为止,这是我使用的代码:
This one, I wrote because it returns the index.这个,我写是因为它返回索引。 But not
[4,5]
.但不是
[4,5]
。
p_Q = set(Q_act).intersection(A_set)
p_dur_Q_act = [i + 1 for i, x in enumerate(p_Q)]
print(p_dur_Q_act)
I also tried this but I receive an error TypeError: argument of type 'int' is not iterable
我也试过这个,但我收到一个错误
TypeError: argument of type 'int' is not iterable
p_dur_Q_act = [i + 1 for i, x in enumerate(Q_act) if any(elem in x for elem in A_set)]
print(p_dur_Q_act)
Another option is to use the enumerate
iterator to generate every index, and then select only the ones you want:另一种选择是使用
enumerate
迭代器生成每个索引,然后 select 只生成你想要的:
a_set = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
q_act = [2, 3]
dur = [0, 4, 5, 2, 1, 3, 4, 8, 2, 3]
p_dur_q_act = [i for i,v in enumerate(a_set) if v in q_act]
print([dur[p] for p in p_dur_q_act if p in dur]) # [4, 5]
This is more efficient than repeatedly calling index if the number of matches is large, because the number of calls is proportional to the number of matches, but the duration of calls is proportional to the length of a_set.如果匹配的数量很大,这比重复调用 index 更有效,因为调用的数量与匹配的数量成正比,但调用的持续时间与 a_set 的长度成正比。 The enumerate approach can be made even more efficient by turning q_act into a set, since
in
scales better with sets than lists.通过将 q_act 转换为集合,可以使枚举方法更加有效,因为集合比列表更好
in
扩展。 At these scales, though, there will be no observable difference.但是,在这些尺度上,不会有明显的差异。
You don't need to map these to index values, though.不过,您不需要 map 这些索引值。 You can get the same result if you use
zip
to map a_set
to dur
and then select the d
values whose a
values are in q_act
.如果您使用
zip
到 map a_set
到dur
然后 select 的d
值,其a
值在q_act
中,您可以获得相同的结果。
a_set = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
q_act = {2, 3}
dur = [0, 4, 5, 2, 1, 3, 4, 8, 2, 3]
p_dur_q_act = [d for a, d in zip(a_set, dur) if a in q_act]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.