Aug-24-2020, 07:11 PM
(This post was last modified: Aug-24-2020, 07:12 PM by Gribouillis.)
tester_V Wrote:Could you explain the code please?Well the expresssion
re.search(r"CELL\-(\d+)", line)
returns either None or a MatchObject in the sense of the re module. There is a match object if a substring such as CELL-8 was found in the line. With the match object, one can get the number 8, that's the value returned by the expression int(match.group(1))
. Thus the sequencesequence = (re.search(r"CELL\-(\d+)", line) for line in cells_file)is a sequence such as
None, None, match, None, match,...
with one item per line of the file.The expression
inum = set(int(match.group(1)) for match in sequence if match)computes the set of all integers found in the above matches, hence all the integers i such that CELL-i was found in the file (actually, only the first occurrence on each line is taken into account).
Then there is a loop, equivalent to
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]: if i in inum: print(f"CELL-{i} infile") else: print(f"CELL-{i} missing")