简体   繁体   English

如何检查列表中是否只有一个 position 为空白,其他所有不是

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

相关问题 当所有其他输入都有效时,列表中只有一个输入的 int base 10 无效文字(后缀) - invalid literal with int base 10 for only one input in list, when all others work (postfix) 显示匿名用户错误,也无法检查用户是否仅在一个页面上进行了身份验证。在 django 中的所有其他页面上工作正常 - Showing Anonymous user error and also not able to check whether user is authenticated only on one page.Working fine on all others in django 如何在 Pandas 中列出最高相关对(一个规范列与所有其他列)? - How to list highest correlation pairs (one spec. column with all others) in pandas? 迭代地将一个元素与 Python 列表中的所有其他元素进行比较 - Iteratively compare one element to all others in a Python list 如何检查列表中的所有元素是否都包含在其他元素中 - How to check if all elements of a list are contained in other one 如何检查具有相同长度的 2 个列表是否在同一个 position 中具有相同的元素,但仅针对一个特定元素 - How to check if 2 lists with the same length have the same element in the same position but only for one specific element 如何打印列表的所有元素,而不仅仅是最后一个? - How to print all element of the list and not only the last one? 只检测到第一个字母是假的,其他的都没有,怎么都能看出来? - Only first letter detecting as false, not others, how can all be looked at? Python-如何始终检查Turtle是否在位 - Python - How to check all the time if Turtle in Position MPI:如何使一个进程终止所有其他进程-python-> fortran - MPI: How to get one process to terminate all others - python -> fortran
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM