英文:
Can't find common elements is two lists
问题
我正在编写一个小的Python程序,它应该找到两个数字之间的最大公约数。到目前为止一切顺利,但我无法让程序找到两个列表中的正确公共元素。
此外,你可以看到列表'combines_prime_lst'在'common_member()'函数中被赋值,但在代码返回到'ggT()'函数时为空。该变量在代码顶部被赋值为空列表。
我检查了,这些列表实际上是列表,我尝试了intersection()函数和&方法。我得到的输出总是false。
combined_prime_lst = common_member(prime_number_lst_1, prime_number_lst_2)
print("Common elements 2:", combined_prime_lst)
print("Der ggT von", number_1, "und", number_2, "ist:", combined_prime_lst)
def common_member(lst_1, lst_2):
lst1 = set(lst_1)
lst2 = set(lst_2)
combined_prime_lst = list(set(lst1).intersection(lst2))
print("Common elements:", combined_prime_lst)
if combined_prime_lst == []:
print("Keine gemeinsamen Elemente.")
最后,这是我运行程序时得到的输出。
Was ist deine erste Zahl? 36
Wie lautet deine zweite Zahl? 66
Fist list: [2, 2, 3, 3]
Second list: [2, 3, 11]
Common elements 2: []
Der ggT von 36 und 66 ist: []
英文:
I'm writing a little python program, which is supposed to find the greatest common divisor between two numbers. Until now everything went fine but I can't get the program to find the correct common elements in the two lists the program provides.
Also you can see that the list 'combines_prime_lst' is assigned in the 'common_member()' function but appears empty when the code gets back to the 'ggT()' function. The variable was assigned at the top of the code as an empty list.
I checked, the lists are actually lists, I tried with the intersection() function and the & method. The output I get is always false.
combined_prime_lst = common_member(prime_number_lst_1, prime_number_lst_2)
print("Common elements 2:", combined_prime_lst)
print("Der ggT von", number_1, "und", number_2, "ist:", combined_prime_lst)
def common_member(lst_1, lst_2):
lst1 = set(lst_1)
lst2 = set(lst_2)
combined_prime_lst = list(set(lst1).intersection(lst2))
print("Common elements:", combined_prime_lst)
if combined_prime_lst == []:
print("Keine gemeinsamen Elemente.")
At last this is the output I get when I run the program.
Was ist deine erste Zahl? 36
Wie lautet deine zweite Zahl? 66
Fist list: [2, 2, 3, 3]
Second list: [2, 3, 11]
Common elements 2: []
Der ggT von 36 und 66 ist: []
答案1
得分: 0
"你遇到的问题是因为你将 'combined_prime_lst' 赋值为 common_member(lst_1, lst_2) 的结果;但是这个函数实际上没有返回任何内容。所以,在函数内部 'combined_prime_lst' 有一个值,但在函数外部你将其赋值为 'None'。
我修改了代码如下:
prime_number_lst_1 = [2, 2, 3, 3]
prime_number_lst_2 = [2, 3, 11]
def common_member(lst_1, lst_2):
lst1 = set(lst_1)
lst2 = set(lst_2)
combined_prime_lst = list(set(lst1).intersection(lst2))
print("共同元素:", combined_prime_lst)
if combined_prime_lst == []:
print("没有共同元素.")
return combined_prime_lst
combined_prime_lst = common_member(prime_number_lst_1, prime_number_lst_2)
print("共同元素 2:", combined_prime_lst)
number_1, number_2 = combined_prime_lst
print("最大公约数是", number_1, "和", number_2, "是:", combined_prime_lst)
输出:
共同元素: [2, 3]
共同元素 2: [2, 3]
最大公约数是 2 和 3 是: [2, 3]"
英文:
The problem you are experiencing is because you are assigning to 'combined_prime_lst' the result of common_member(lst_1, lst_2); but this function isn't actually returning anything. So, inside the function 'combined_prime_lst' has a value, but outside it you are assigning 'None' to it
Here is how I modified the code:
prime_number_lst_1 = [2, 2, 3, 3]
prime_number_lst_2 = [2, 3, 11]
def common_member(lst_1, lst_2):
lst1 = set(lst_1)
lst2 = set(lst_2)
combined_prime_lst = list(set(lst1).intersection(lst2))
print("Common elements:", combined_prime_lst)
if combined_prime_lst == []:
print("Keine gemeinsamen Elemente.")
return combined_prime_lst
combined_prime_lst = common_member(prime_number_lst_1, prime_number_lst_2)
print("Common elements 2:", combined_prime_lst)
number_1, number_2 = combined_prime_lst
print("Der ggT von", number_1, "und", number_2, "ist:", combined_prime_lst)
Output:
Common elements: [2, 3]
Common elements 2: [2, 3]
Der ggT von 2 und 3 ist: [2, 3]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论