i am beginner of python and i have a question. How to pass the result of one function to another one. I want to make new list based on first one only with prime digits, and when i run my program second list is empty
def get_list_of_int_numbers(n: int):
list1 = [random.randint(10, 100) for x in range(random.randint(10, 100))] * n
return list1
def get_prime_digits(n: int) -> bool:
return n in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.]
def get_second_list_based_on_old_one_with_prime_digits(expression: list) -> list:
list2 = [n for n in expression if n == get_prime_digits(n)]
return list2
def main() -> None:
n1 = random.randint(10, 100)
print(get_list_of_int_numbers(n1))
print(get_second_list_based_on_old_one_with_prime_digits(get_list_of_int_numbers(n1)))
if __name__ == '__main__':
main()
any help would be appreciated
CodePudding user response:
Problem is in your get_second_list_based_on_old_one_with_prime_digits
, you don't need to use n
to compare with the result of get_prime_digits
def get_second_list_based_on_old_one_with_prime_digits(expression: list) -> list:
list2 = [n for n in expression if n == get_prime_digits(n)]
# ^^^^
return list2
CodePudding user response:
Well, I don't know why the second printed list would always be empty but it will certainly not be using the values created in your first call to get_list_of_int_numbers(...)
.
This is because you are not saving the results of the first call to get_list_of_int_numbers(...)
into a variable. And the second call to it, creates a different list.
def main() -> None:
n1 = random.randint(10, 100)
new_list : list = get_list_of_int_numbers(n1) # Save first list here
print(get_second_list_based_on_old_one_with_prime_digits(new_list)) # Reuse first list