[英]How would I determine the number of times a digit that is not Digit Y appears in a list
I have a list L full of digits or strings, I want to find how many elements are different from a given one. 我有一个由数字或字符串组成的列表L,我想查找与给定元素不同的元素。 So taking the list below, how would I figure find the elements that are not the digit 1 for example? 因此,以下面的列表为例,我将如何找到非数字1的元素?
[1,1,1,1,1,1,"hello",1,1,1,1,3,4,6,1,"world",1,1,1,1,3,4,3,"1",1,1,1,1,1,1,1,"welcome",3,4]
I'd use a conditional list comprehension to create a list without that element, and then check it's lenght: 我将使用条件列表理解来创建没有该元素的列表,然后检查其长度:
l = [1,1,1,1,1,1,"hello",1,1,1,1,3,4,6,1,"world",1,1,1,1,3,4,3,"1",1,1,1,1,1,1,1,"welcome",3,4]
result = len([x for x in l if x != 1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.