简体   繁体   English

检查两个变量是否在同一个列表中

[英]Checking if two variables are in the same list

What would be the best way to ensure that two different variables are in the same list before doing something with them? 在对它们做某事之前,确保两个不同变量在同一个列表中的最佳方法是什么? For example: 例如:

var1 = 1
var2 = 2
var3 = 3
var4 = 4
list1 = [var1, var2, var3, var4]
var5 = 5
var6 = 6
var7 = 7
var8 = 8
list2 = [var5, var6, var7, var8]

if var1 and var2 (are in the same list):
    print("In the same list")
else: 
    print("Not the same")

Would it be better to use a tuple or something else instead of lists, which would make this easier? 使用元组或其他东西而不是列表会更好吗?这会使这更容易吗?

>>> list1 = list(range(1, 5))
>>> 1 in list1 and 2 in list1
True
>>> 1 in list1 and 6 in list1
False

If you're checking multiple items, then go for all : 如果您正在检查多个项目,那么请all

>>> to_be_checked = [1, 2, 3, 4]
>>> all(item in list1 for item in to_be_checked)
True

Don't create separate variables for each item, just put them in the list from the start. 不要为每个项目创建单独的变量,只需将它们从一开始就放在列表中。


For efficieny use sets, sets provide O(1) loopkup. 对于高效的使用集,集合提供O(1) loopkup。 Note that sets don't have any order and you can't do indexing on them 请注意,集合没有任何顺序,您无法对它们建立索引

>>> list1 = list(range(10**5))
>>> s = set(range(10**5))
>>> %timeit 1000 in list1 and 10**5 in list1
100 loops, best of 3: 2.71 ms per loop
>>> %timeit 1000 in s and 10**5 in s
1000000 loops, best of 3: 403 ns per loop

The cleanest way to do this I think is to test whether your var_1 and var_2 are a subset of one or more of your lists 我认为最干净的方法是测试你的var_1和var_2是否是你的一个或多个列表的子集

list_a = [1,2,3,4]
list_b = [5,6,7,8]
test_list = [1,4]    
if set(test_list)issubset(set(list_a)):
    do something

this preserves your data in their list form so you can do what you like with them and it is fast and not as confusing as using the and statement - which in my opinion adds unnecessary complexity to code 这会以列表形式保存您的数据,因此您可以使用它们执行您喜欢的操作,并且速度快且不像使用和语句那样令人困惑 - 在我看来,这会增加代码的不必要的复杂性

Just to be clear I am asking Python to test whether the set that has as its members the members of the test_list is a subset of the set that has as its members the members of list_a 为了清楚起见我要求Python测试作为其成员的test_list成员是否是该成员的一个子集,其成员是list_a的成员

Actually I think you are asking something more specific here - so you have some lists- presumably you have some way to get the lists to you can iterate for them 实际上我认为你在这里要求更具体的东西 - 所以你有一些列表 - 大概你有办法让你的列表可以迭代它们

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

 temp_list = [1,4]

 for each_list in my_lists:
     if set(temp_list).issubset(set(each_list)):
         print 'members of the same list'
      else:
         print 'not members of the same list'

On the face of it: 从表面上看:

if ((var1 in list1 and var2 in list2) or (var1 in list2 and var2 in list2))

to generalize a bit: 概括一下:

mylists = (list1, list2)
if any(var1 in x and var2 in x for x in mylists)

to generalize a bit more: 更多地概括一下:

mylists = (list1, list2)
myvars = (var1, var2)
if any(all(v in x for v in myvars) for x in mylists)

Or you could look at it totally differently: 或者你可以完全不同地看待它:

which_list = dict()
which_list.update((val, 1) for val in list1)
which_list.update((val, 2) for val in list2)

if which_list[var1] == which_list[var2]

of course, that assumes each variable appears in exactly one list. 当然,假设每个变量都出现在一个列表中。

Note that in all cases, you aren't really checking whether the variable is in the list. 请注意,在所有情况下,您都没有真正检查变量是否在列表中。 A variable cannot be in a list. 变量不能在列表中。 You're checking whether the current value of the variable is in the list. 您正在检查变量当前值是否在列表中。 So if any of the variables refer to identical values, then there's no way to tell from the list which variable name was used to put the value in the list. 因此,如果任何变量引用相同的值,则无法从列表中判断使用哪个变量名将值放入列表中。

As Ashwini Chaudhary says, a set can generally execute in faster than a list or tuple . 作为阿什维尼乔杜里说,一set可以执行通常in比更快的listtuple But for such small lists it's unlikely to make a noticeable difference. 但是对于这样的小名单,它不太可能产生明显的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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