[英]How do i check if the elements in a list are contained in other list?
I´m new to Python and I´m having a problem.我是 Python 的新手,我遇到了问题。 I have 2 lists containing the names of the columns of a dataset: one has all the columns names (columnas = total.columns.values.tolist());
我有 2 个包含数据集列名称的列表:一个包含所有列名称(columnas = total.columns.values.tolist()); and the other one has a subset of them ( in the form of "c = [a,b,c,d,c,e,...]".
另一个有它们的子集(形式为“c = [a,b,c,d,c,e,...]”。
I would like to know how could I check if each element in "c" is contained in the longer list "columnas".我想知道如何检查“c”中的每个元素是否包含在较长的列表“columnas”中。 The result i have been trying to get is as it follows ( this is just an example):
我一直试图得到的结果如下(这只是一个例子):
a: True b: True c: False... a:正确 b:正确 c:错误...
Looking forward to your answers, Santiago圣地亚哥,期待您的回答
You can use what is called a "dictionary-comprehension" to form your result:您可以使用所谓的“字典理解”来形成您的结果:
columnas = ['a', 'b', 'z']
c = ['a', 'b', 'c', 'd', 'c', 'e']
contained = {x : x in columnas for x in c}
which gives contained
as {'a': True, 'b': True, 'c': False, 'd': False, 'e': False}
.其中
contained
为{'a': True, 'b': True, 'c': False, 'd': False, 'e': False}
。
a=[ ] for i in c: if i in columns: a.append(true) else: a.append(false)
a=[ ] for i in c: if i in columns: a.append(true) else: a.append(false) a=[ ] for i in c:如果 i 在列中:a.append(true) else:a.append(false)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.