python所有可能的成对的2个列表元素,并获取该对的索引 [英] python all possible pairs of 2 list elements, and getting the index of that pair

查看:104
本文介绍了python所有可能的成对的2个列表元素,并获取该对的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个列表:

a = list(1,2,3)
b = list(4,5,6)

所以我可以有9对这些列表成员:

So I can have 9 pairs of these list members:

(1,4)
(1,5)
(1,6)

(2,4)
(2,5)
(2,6)

(3,4)
(3,5)
(3,6)

现在,给定上述两个列表成员,我能找出该对的索引吗? 像上面的(1,4)就是第一对.

Now, given two list members like above, can I find out the pair's index? Like (1,4) from above would be the 1st pair.

推荐答案

并完成答案并停留在示例中:

And to complete the answer and stay in the example:

import itertools  

a = [1, 2, 3]
b = [4, 5, 6]
c = list(itertools.product(a, b))

idx = c.index((1,4))

但这将是从零开始的列表索引,因此是0而不是1.

But this will be the zero-based list index, so 0 instead of 1.

这篇关于python所有可能的成对的2个列表元素,并获取该对的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆