Reputation: 1
result with the originalI am trying to figure out how I can turn the following tuple comprehension into the for loop instead. In the class video, this was only done in the following manner. I have asked class discord for help and the teaching assistant, but I am not getting any feedbacks that I can understand. Edit: Yes, I understand that I should not be using the same variable names. I've been commenting out and uncommenting between the original and mine so that I wouldn't have to constantly change few lines below. ------------------this is the original-------------------------------
def get_password_leaks_count(hashes, hash_to_check):
hashes = (line.split(':') for line in hashes.text.splitlines())
print(hashes)
for h, count in hashes:
if h == hash_to_check:
return count
return 0
print(h, count)
Below is what I have worked out so far, but not even sure if I am heading the right way; I'm not even sure if hash_to_check = [] is even supposed to be there.
def get_password_leaks_count(hashes, hash_to_check):
hashes = hashes.text.splitlines()
for line in hashes:
hashes = line.split(":")
hashes = hash_to_check
print(hashes)
# for h, count in hashes:
# if h == hash_to_check:
# return count
# return 0
Upvotes: 0
Views: 139
Reputation: 13079
The code is confusing because it reuses the variable hashes
. Avoiding this, and omitting the first print
statement which only prints a generator object rather than the values it generates (I doubt whether this is really needed) gives the following. Note that I am also omitting the final print
, which is never reached because it follows a return
statement.
def get_password_leaks_count(hashes, hash_to_check):
for line in hashes.text.splitlines():
for h, count in line.split(':'):
if h == hash_to_check:
return count
return 0
By the way, despite the title of the question, you do not have a list comprehension in the original. Instead, you have a generator expression. Iterating over it will have a similar effect to iterating over a list produced by a list comprehension, but a generator will have been used in order to avoid creating an unnecessary temporary list in memory.
Upvotes: 1