简体   繁体   English

for 循环同时递减列表的大小

[英]for loop while decrementing the size of the list

so I have a list of 5 or fewer elements and the elements are just integers from 0-9 and the elements are randomly assigned and its possible for the list to have 5 zeros or 5 ones etc, and I have the following function to check if there is a zero in the list.所以我有一个包含 5 个或更少元素的列表,这些元素只是 0-9 之间的整数,元素是随机分配的,列表可能有 5 个零或 5 个 1,等等,我有以下功能来检查是否列表中有一个零。 this will return the index of the first zero it finds.这将返回它找到的第一个零的索引。 just ignore the .getValue()忽略 .getValue()

    def check0(self):
        '''
        check for the index of the first card with value 0 in hand
        :return:
        '''
        index = 0
        found = False
        while not found and index<len(self.hand):
            if self.hand[index].getValue() == 0:
                found = True
            index += 1
        if not found:
            index = -1
        return index

but the problem is that it always returns the first zero it finds in the list.但问题是它总是返回它在列表中找到的第一个零。 in another class I am using this function to check if the hand has any zeros.在另一堂课中,我使用这个函数来检查手是否有任何零点。

I need to write a for loop or some other loop that will traverse the list hand and tell me if all the elements in the hand are zeros.我需要编写一个 for 循环或其他一些循环来遍历列表手并告诉我手中的所有元素是否为零。

so the only solution I can think of for this problem is to traverse the list once and when the first zero is found increment the counter and then traverse the list again this time excluding the zero that had already been found.所以我能想到的唯一解决方案是遍历列表一次,当找到第一个零时增加计数器,然后这次再次遍历列表,排除已经找到的零。

for example:例如:

I have the list
[0,0,0,0,0]

in the first traversal, the check0() method will return the index 0 for the first zero but then I traverse the list again this time excluding the first zero and repeating that until I reach the last element.

I was thinking something like this:我在想这样的事情:

def find_zeros():
counter = 0
     for I in some_list(0,len(some_list),-1):
          if I.check0() != -1:
              counter += 1
          if counter == len(some_list):
             return True
     return False

can anyone help me with this issue?谁能帮我解决这个问题? let me know if anything is unclear also I'm not allowed to import anything and time complexity isn't an issue如果有任何不清楚的地方,请告诉我,我不允许导入任何内容,时间复杂度也不是问题

"I need to write a for loop or some other loop that will traverse the list hand and tell me if all the elements in the hand are zeros." “我需要编写一个 for 循环或其他一些循环来遍历列表手并告诉我手中的所有元素是否为零。” (OP) (OP)

Well, to check if all elements in your list are zero you could use count :好吧,要检查列表中的所有元素是否都为零,您可以使用count

lst1 = [0,0,0,0,0]
print(len(lst1) == lst1.count(0))

Or maybe list comprehension:或者也许列表理解:

lst1 = [0,0,0,0,0]
print(lst1 == [nr for nr in lst1 if nr == 0])

probably better written using all like:可能更好地使用all如:

lst1 = [0,0,0,0,0]
print(all(i==0 for i in lst1))

Or maybe create a second list the same size:或者创建一个相同大小的第二个列表:

lst1 = [0,0,0,0,0]
print(lst1 == [0]*len(lst1))

You can use enumerate for this type of problem.您可以使用 enumerate 解决此类问题。

for index, ch in enumerate(list_name):
   print(i, ch)

This will give you the index of each and every character in the list.这将为您提供列表中每个字符的索引。 You can use an 'if' statement later to check if 'ch' is a zero.您可以稍后使用“if”语句来检查“ch”是否为零。 Hope it helped.希望它有所帮助。

listt=[1,0,2,0,1]

for i in range(len(listt)):
    if listt[i]==0:
        print(i)
        break #if you want to find first occurence

To check all ekements are 0,要检查所有元素都是 0,

  if len(set(listt))==1 and listt[0]==0:   
           print("All index have 0 ")

You could define the function like this:你可以这样定义函数:

def check0(self):
    index = (self.hand+[0]).index(0)
    return -1 if not any(self.hand) else index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM