[英]How to check if only one position in a list is blank and all others are not
I have a list我有一个清单
list = [' ' for x in range(10)]
If I add values to this list, how do I check that only list[0] is blank and list[1] - list[9] are not?如果我向这个列表添加值,我如何检查只有 list[0] 为空而 list[1] - list[9] 不是?
Assuming l
is the list containing the values, you can do something like:假设l
是包含值的列表,您可以执行以下操作:
if l[0] == ' ' and all(s != ' ' for s in l[1:9]):
print("Complete!!!!!!")
You can use all
你可以使用all
list1=[' ' if i==0 else i for i in range(10) ]
if list1[0]==" " and all(x!=' ' for x in list1[1:9]):
print("Complete List!!!")
The easiest, most straight forward way is to just write a loop to go through each element.最简单、最直接的方法是通过每个元素向 go 编写一个循环。 Keep track of what the element is and then do something with that information.跟踪元素是什么,然后用这些信息做一些事情。
A way for you to know that would constant time would be to track how many different indices you have changed, and then to just check how many empty spaces there are left.一种让您知道恒定时间的方法是跟踪您更改了多少不同的索引,然后只检查剩下多少空白空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.