简体   繁体   English

我如何确定不是数字Y的数字出现在列表中的次数

[英]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.

相关问题 如何计算一个数字出现在文件中每个数字开头的次数? (蟒蛇) - How to count the number of times a digit appears at the beginning of each number in a file? (python) 我将如何制作一个没有重复数字且第一位必须为 0 的 4 位数字? - How would I make a 4-digit number with no repeating digits and the first digit has to be 0? 如何将4位数字变量与4位数字列表进行比较,并确定它们是否具有完全相同的数字,而不管顺序如何? - How Can I Compare A 4 Digit Variable To A List Of 4 Digit Numbers And Determine If They Have The Exact Same Numbers, Regardless Of Order? 查找数字中有多少次 - Finding how many times a digit is in a number 检查数字在python词典中出现多少次? - Checking how many times a digit appears in a dictionary in python? 如何检查素因数列表以确定原始数字是否可以表示为 Python 中两个 3 位数字的乘积? - How do I check a list of prime factors to determine if the original number can be expressed the product of two 3-digit numbers, in Python? 如何将 1 位、2 位、3 位、4 位数字转换为 '%H%M'? - how to convert 1 digit, 2 digit, 3 digit, 4 digit number to '%H%M'? 如何获取 4 位数字的所有可能组合的列表,其单个数字总和为 13,最后一位数字为 5 - how get list of all the possible combinations of 4 digit numbers whose individual digit sum is 13 and last digit is 5 in that number 如何确定单词在输入中出现的次数 - How to determine the number of times a word appears in an input 如何从 Tkinter 条目中获取 3 位数字并将其转换为 IP 地址? - How would I take a 3 digit number from a Tkinter entry and translate it into an IP address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM