【问题标题】:Best way to store multiple sets in python?在python中存储多组的最佳方法?
【发布时间】:2021-06-19 20:00:24
【问题描述】:

我想将多组(坐标)存储到一个主列表/字典/数组中。我不知道该怎么做...

这是我的目标: 使用主列表中的 for-each 循环,将每个列表相交并将它们相互比较

例子:

a = {'[2, 2]', '[2, 10]', '[3, 6]', '[4, 5]'}

b = {'[3, 6]', '[6, 6]', '[9, 1]', '[9, 8]'}

c = {'[2, 2]', '[6, 7]', '[7, 5]', '[9, 2]'}

d = {'[1, 2]', '[2, 2]', '[3, 6]', '[7, 5]'}


list1 = [a, b]

list2 = [c, d]


for each set1 in list1:

   for each set2 in list2:

      set1.intersection(set2)

【问题讨论】:

  • 您似乎将坐标存储为字符串。这是一种极其笨拙、难以使用的数据表示。我怀疑这是试图修复您在尝试将列表存储在集合中时遇到的 TypeError。你应该使用元组,而不是字符串。
  • 您还想比较 set a 和 set b 吗?还是只有 a 到 c、a 到 d、b 到 c 和 b 到 d?
  • @joostblack 只是想将 a 与 c、d/ b 与 c、d/ c 与 a、b/ d 与 a、b 进行比较。
  • 我使用了字符串,因为我不确定如何获得两组之间的交集。我最初确实希望它们作为元组,但交集方法仅适用于元组。抱歉,这是我第一次使用堆栈溢出。我希望你能看到这条评论。
  • @BrookeForster @像我对你做过的人,如果你想确保他们明白的话。

标签: python arrays list dictionary set


【解决方案1】:

首先是最简单的方法。那是通过使用像 cmets 建议的元组:

# your sets
a = {(2, 2), (2, 10), (3, 6), (4, 5)}
b = {(3, 6), (6, 6), (9, 1), (9, 8)}
c = {(2, 2), (6, 7), (7, 5), (9, 2)}
d = {(1, 2), (2, 2), (3, 6), (7, 5)}

#print output
print("Compare a to c+d:",a&(c|d))
print("Compare b to c+d:",b&(c|d))
print("Compare c to a+b:",c&(a|b))
print("Compare d to a+b:",d&(a|b))

输出:

Compare a to c+d: {(3, 6), (2, 2)}
Compare b to c+d: {(3, 6)}
Compare c to a+b: {(2, 2)}
Compare d to a+b: {(3, 6), (2, 2)}

如果您的输入与您的示例中描述的一样,我会这样做:

class Coord:
    def __init__(self, x, y):
        self.x = x
        self.y = y


    @staticmethod
    def from_string(coord_str):
        x,y = coord_str.strip("[]").split(",")
        return Coord(int(x),int(y))


    @staticmethod
    def convert(my_iter):
        return set(map(Coord.from_string, my_iter))


    def __eq__(self, other): 
        if not isinstance(other, Coord):
            return False
        return self.x == other.x and self.y == other.y


    def __repr__(self):
        return "Coord(%s, %s)" % (self.x, self.y)


    def __hash__(self):
        return hash(self.__repr__())

# your sets
a = {'[2, 2]', '[2, 10]', '[3, 6]', '[4, 5]'}
b = {'[3, 6]', '[6, 6]', '[9, 1]', '[9, 8]'}
c = {'[2, 2]', '[6, 7]', '[7, 5]', '[9, 2]'}
d = {'[1, 2]', '[2, 2]', '[3, 6]', '[7, 5]'}

# creating sets
set_a = Coord.convert(a)
set_b = Coord.convert(b)
set_c = Coord.convert(c)
set_d = Coord.convert(d)
set_a_and_b = Coord.convert(a)|Coord.convert(b)
set_c_and_d = Coord.convert(c)|Coord.convert(d)

# print requested information
print("Compare a to c+d:",set_a.intersection(set_c_and_d))
print("Compare b to c+d:",set_b.intersection(set_c_and_d))
print("Compare c to a+b:",set_c.intersection(set_a_and_b))
print("Compare d to a+b:",set_d.intersection(set_a_and_b))

输出:

Compare a to c+d {Coord(2, 2), Coord(3, 6)}
Compare b to c+d {Coord(3, 6)}
Compare c to a+b {Coord(2, 2)}
Compare d to a+b {Coord(2, 2), Coord(3, 6)}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2012-08-15
    • 1970-01-01
    • 2010-11-11
    • 2014-05-02
    相关资源
    最近更新 更多