如何从三维列表中移除多余的括号以创建一个二维列表?

huangapple go评论61阅读模式
英文:

How to remove excess brackets in a 3d list to make a 2d list?

问题

我正在解决一个名为3Sum的LeetCode问题,以下是我的代码:

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        def twoSum(nums, target):
            d = {
                target - nums[0]: 0 
            }

            for i in range(1, len(nums)):
                if nums[i] in d:
                    return [nums[i], target - nums[i]]
                else:
                    d[target-nums[i]] = i
        arr = []

        for i in range(len(nums)):
            s = twoSum(nums[i+1:] + nums[:i], -nums[i])
            if s == None:
                continue
            if s not in arr:
                arr.append(s)
                arr[len(arr) - 1].append(nums[i])
        
        n = list(map(sorted, arr))
        print(n)
        res = []
        [res.append(x) for x in n if x not in res]
        return res

这不是最佳的解决方案,但我遇到了一个问题。我注意到我不能简单地将我的2Sum解决方案实现在这里,因为简单来说,2Sum问题保证只有一个解决方案,而3Sum可能有多个解决方案。因此,我想,不仅仅返回一个列表,而是返回一个二维列表,这样外部列表可以容纳多个解决方案。

然而,这引发了另一个问题,这也是我希望这个问题的答案能解决的问题。我将2Sum添加到arr列表中。这样做后,arr变成了一个三维列表,而我希望它是一个二维列表。

我想知道如何将这样的列表:arr = [[[3, 2, 9, 4], [1, 9, 5]], [3, 2, 2]] 变成这样的列表:arr = [[3, 2, 9, 4], [1, 9 ,5], [3, 2, 2]],以满足问题的要求。换句话说,如何仅删除使列表成为三维列表的括号,使其成为二维列表。arr的数据类型是一个列表。

注意1:我理解在这个问题中,arr的子数组只会有3个元素,但我想将它们泛化,以便这个问题的答案不仅适用于这个LeetCode问题。

注意2:我希望得到[[3, 2, 9, 4], [1, 9 ,5], [3, 2, 2]],而不是[[3, 2, 9, 4, 1, 9 ,5], [3, 2, 2]],因为第二个列表也删除了一些二维列表的括号。希望这样说得清楚。

另一个示例是arr等于[[[5]], [3, 2, 3, 5, 10]]。如果是这种情况,我想要将其变成[[5], [3, 2, 3, 5, 10]],以便它只是一个二维数组。

最后一个示例是[[[2, 6, 3, 1, 9], [3, 1, 99, 4]], [[4, 15], [4, 7, 2, 1]]]。这将变成[[2, 6, 3, 1, 9], [3, 1, 99, 4], [4, 15], [4, 7, 2, 1]]

谢谢!

英文:

I am doing a leetcode problem named 3Sum: Here is my code:

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        def twoSum(nums, target):
            d = {
                target - nums[0]: 0 
            }

            for i in range(1, len(nums)):
                if nums[i] in d:
                    return [nums[i], target - nums[i]]
                else:
                    d[target-nums[i]] = i
        arr = []

        for i in range(len(nums)):
            s = twoSum(nums[i+1:] + nums[:i], -nums[i])
            if s == None:
                continue
            if s not in arr:
                arr.append(s)
                arr[len(arr) - 1].append(nums[i])
        
        n = list(map(sorted, arr))
        print(n)
        res = []
        [res.append(x) for x in n if x not in res]
        return res

It isn't the greatest of solutions, but I ran into a problem. I noticed that I couldn't just implement my 2Sum solution into it because, to put it simply, while the 2Sum problem guarantees that there is only one solution, there could be many solutions for 3Sum. Therefore, I thought instead of just returning one list, how about a return a 2d list, so that the outer list can hold the many solutions.

However, that raised another problem, the one I hope the answer to this problem will solve. I'm then appending that 2Sum into the arr list. In doing so, arr becomes a 3d list, which I would like to be a 2d list.

I am wondering how to make a list like this: arr = [[[3, 2, 9, 4], [1, 9, 5]], [3, 2, 2]] into a list like this: arr = [[3, 2, 9, 4], [1, 9 ,5], [3, 2, 2]], so that it meets the problem requirements. In other words, how to remove only the brackets that make the list a 3d list so that it becomes a 2d list. The data type of arr is a list.

Notice1: I understand that in the problem, the subarrays of arr would only have 3 elements each, but I would like to generalize it so that the answer to this question isn't specific to only this leetcode problem.

Notice2: I want [[3, 2, 9, 4], [1, 9 ,5], [3, 2, 2]] instead of [[3, 2, 9, 4, 1, 9 ,5], [3, 2, 2]], because the second list also removes some brackets of the 2d lists. Hope that makes sense.

Another example is arr being equal to [[[5]], [3, 2, 3, 5, 10]]. If it were that case, I would like to make it [[5], [3, 2, 3, 5, 10]], so that it is only a 2d array.

One last example is [[[2, 6, 3, 1, 9], [3, 1, 99, 4]], [[4, 15], [4, 7, 2, 1]]]. That would turn into [[2, 6, 3, 1, 9], [3, 1, 99, 4], [4, 15], [4, 7, 2, 1]].

Thank you!

答案1

得分: 7

循环遍历列表。如果元素是一个二维列表,就扩展结果以使其变平,否则将元素附加到结果。

data = [[[3, 2, 9], [1, 9, 5]], [3, 2, 2]]
result = []
for i in data:
    if isinstance(i[0], list):
        result.extend(i)
    else:
        result.append(i)

print(result)
英文:

Loop over the list. If an element is a 2d list, extend the result to flatten it, otherwise append the element onto the result.

data = [[[3, 2, 9], [1, 9, 5]], [3, 2, 2]]
result = []
for i in data:
    if isinstance(i[0], list):
        result.extend(i)
    else:
        result.append(i)

print(result)

huangapple
  • 本文由 发表于 2023年7月7日 05:06:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定