[英]How to convert tuples in nested list to lists?
I need to find three values in a list that returns the value of zero.我需要在返回零值的列表中找到三个值。 The problem that I have with my code is that it returns a nested list with tuples instead of lists.
我的代码的问题是它返回一个带有元组而不是列表的嵌套列表。
Input:输入:
[-1, 0, 1, 2, -1, -4]
Output:输出:
[(-1, -1, 2), (-1, 0, 1)]
Expected Output:预期输出:
[[-1, -1, 2], [-1, 0, 1]]
My code:我的代码:
def threeSum(a):
N = len(a)
c=[]
for i in range(N):
for j in range(i+1, N):
for k in range(j+1, N):
if a[i]+a[j]+a[k] == 0:
c.append([a[i],a[j],a[k]])
b_set = set(tuple(sorted(x)) for x in c)
#remove duplicates
return list(b_set)
print(threeSum([-1, 0, 1, 2, -1, -4]))
Insert this line插入这一行
b_set = [list(x) for x in b_set]
before your return statement for your expected output在预期输出的 return 语句之前
Change your return statement to just将您的退货声明更改为 just
return b_set
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.