简体   繁体   English

如何从两个单独的列表中获取公共 integer 元素的索引并将其插入另一个列表?

[英]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_actA_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)

Use index function to get the index of the element in the list.使用索引function 获取列表中元素的索引。

>>> 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]
>>>
>>> print([dur[a_set.index(q)] for q in set(a_set).intersection(q_act)])

[4, 5]

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_setdur然后 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.

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