1

I have defined a "value" and I have an IF statement such as:

value = any ("1" in valueMaj for valueMaj in list)
if value == False:

Can I make it so my print statement prints out ALL that are not true, not just one. Example:

INPUT

print("FILE NOT MATCH:", file) 

OUTPUT

FILE NOT MATCH: filenumber1.txt

But, I would like it to print all that do not match rather than stopping the loop.

2
  • 1
    Don't use value == False. Use not value to test for false values. Commented Mar 9, 2016 at 15:01
  • 1
    Don't use list as a name for a variable, that masks the built-in type. Commented Mar 9, 2016 at 15:04

2 Answers 2

3

Don't use any() if you need all values that do not match. Create a list instead using a list comprehension with a filter:

not_matching = [valueMaj for valueMaj in some_list if '1' not in valueMaj]
if not_matching:
    print('The following files do not match:', ', '.join(not_matching))

or use a loop to print out individual files.

any() will short-circuit and stop iterating; it is only useful for efficient testing of a condition against a sequence, not for filtering that sequence.

Sign up to request clarification or add additional context in comments.

4 Comments

For some reason it prints out all of the files, there is decimal so I did try "1." and "1" after the if. Any suggestions? By the way thank you!
@Girls.Gone.Wired: sorry, without sample input data and expected output, that's impossible to answer.
I believe the problem is the the "value" is reading in as a String. Any suggestions for that?
value in your question is always going to be a bool, either True or False.
0
import shutil
import os
import subprocess
import sys
import glob
import re
import fileinput

root_dir = os.getcwd()
uop_dir = root_dir + "\\UoP"

  # .h files
  print ("Searching for .h files: " + uop_dir)
  for root, subfolders, files in os.walk(uop_dir):
   if root.endswith("WHATEVER YOU WANT HERE"):
   if myfile.endswith(".h"):
      print (root + "/" + myfile)
      for file in myfiles:
      for line in open(file):
         line = line.rstrip()
         if re.search('VERSION\s+("\d+\.\d+")$', line):
            version = re.findall("\d+\.\d+" , line)
            # version.append(file)
            if re.search('VERSION\s+("\d+\.\d+")$', line):

                    valueMaj = re.findall('\d+\.', line)
                    ###print(valueMaj)
                    #FINDS MINOR NUMBER
                    if re.search('VERSION\s+("\d+\.\d+")$', line):
                            valueMin = re.findall('\.\d+' , line)
                         ###print(valueMin)
                            value = [valueMaj for valueMaj in file if ('1.7') not in valueMaj]

                            if value:
                                    ###print("FILE NOT MATCH:", file, "\nROOT: " root, "\nVERSION: ", *version)
                                    print("FILE NOT MATCH:", file , "\nVERSION: ", version, "\nMAJOR_NUMBER:", (valueMaj),
                                           "\nMINOR_NUMBER: ", ''.join(valueMin)) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.