How to get multiple values from “return” as in “print”?
本问题已经有最佳答案,请猛点这里访问。
当我运行下面的代码时,我只返回第一个结果。如果我用
我需要做些什么来获得完整的输出,而不仅仅是第一个结果?
1 2 3 4 5 6 7 8 9 10 11 12 13 | nums = [(str(i)) for i in range(100,106)] def foo(aa): for a in nums: for b in a : c= sum(int(b)**2 for b in a) d=''.join(sorted(a,reverse=True)) if (c>5): return(a,d) output = foo(nums) print(output) |
更新——我期待着以下输出:
1 2 3 4 5 6 7 8 9 | 103 310 103 310 103 310 104 410 104 410 104 410 105 510 105 510 105 510 |
1 | 103 310 |
给你
1 2 3 4 5 6 7 8 9 10 11 12 13 | nums = [(str(i)) for i in range(100,106)] def foo(aa): for a in nums: for b in a : c= sum(int(b)**2 for b in a) d=''.join(sorted(a,reverse=True)) if (c>5): return(a,d) output , output2 = foo(nums) print(output, output2) |
编辑
创建列表并插入元组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | nums = [(str(i)) for i in range(100,106)] def foo(aa): list_of_numbs = list() # create a list for a in nums: for b in a : c= sum(int(b)**2 for b in a) d=''.join(sorted(a,reverse=True)) if (c>5): list_of_numbs.append((a,d)) #insert your desire tuplet in the list #print(a,d) return list_of_numbs # return the list x = foo(nums) print(x) # print the list # OR for i,j in x: print(i,j) |