简体   繁体   English

提高嵌套 Frozenset 的打印可读性

[英]Improve print readability of nested Frozenset

The output of the code example below has terrible readability.下面代码示例的 output 可读性很差。 The data I'd like to analyse is hidden within numerous frozenset({}) prints.我要分析的数据隐藏在大量frozenset({})打印中。

A = frozenset(["A1", "A2"])
B = frozenset(["B1", "B2"])
C = frozenset(["C1", "C2"])

foo = {A, B, C}

print(foo)
# {frozenset({'B1', 'B2'}), frozenset({'C1', 'C2'}), frozenset({'A1', 'A2'})}

print(*foo)
# frozenset({'B1', 'B2'}) frozenset({'C1', 'C2'}) frozenset({'A1', 'A2'})

Is there an easy way to neglect the printouts of the data collection type ?有没有一种简单的方法可以忽略data collection type的打印输出? I'm only interested in the grouping of the entries.我只对条目的分组感兴趣。

A more readable output would be:更具可读性的 output 将是:

({'B1', 'B2'}, {'C1', 'C2'}, {'A1', 'A2'})

TLD顶级域名
I stumbled upon this issue when trying to find to optimal way to group a large number of items in pairs.当我试图找到将大量项目成对分组的最佳方法时,我偶然发现了这个问题。 The grouping had to comply to a lot of boundary conditions and I need a quick check if the solution found makes sense.分组必须遵守很多边界条件,我需要快速检查找到的解决方案是否有意义。

items = {'B1', 'B2', 'C1', 'C2', 'A1', 'A2'}
pairs = get_best_grouping(items) # Outputs a set containing frozensets
print(pairs)

Change the str function of frozenset (or any other native type) is a related question, but seems like a big intervention for this problem. Change the str function of frozenset (or any other native type) is a related question,但似乎是对这个问题的一个很大的干预。

As you mentioned in the question, writing your own __str__ or __repr__ function would be the propper way to go, if that sounds to much hassle, how about you modify the string after the fact?正如您在问题中提到的,编写您自己的__str____repr__ function 将是 go 的正确方法,如果这听起来很麻烦,您在事后修改字符串怎么样?

print(str(frozenset({1,2,3})).replace('frozenset',''))

({1, 2, 3}) ({1, 2, 3})

Assuming of course your set does not contain the word "frozenset".当然假设你的集合不包含“frozenset”这个词。

But it is really not much more effort to make a new _frozenset class follwing the same logic as above (note the 1 in replace to make sure that we only replace the first occurence of the str '_frozenset'.):但是按照与上面相同的逻辑制作一个新的_frozenset class 真的没有太多的努力(注意 replace 中的1以确保我们只替换 str '_frozenset' 的第一次出现。):


class _frozenset(frozenset):
    def __repr__(self):
        return (frozenset.__repr__(self)).replace('_frozenset','',1)

print(_frozenset([1, 2, '_frozenset']))

({1, 2, '_frozenset'}) ({1, 2, '_frozenset'})

Imho, key is here to simply reuse the definition of __str__ of the builtin frozenset so we don't have to worry too much about the logic behind how to represent an iterable.恕我直言,这里的关键是简单地重用内置__str__frozenset定义,这样我们就不必太担心如何表示可迭代对象背后的逻辑。 Against first intuition, frozenset.__str__() seems to (I have not looked into it) inspect the name of the class it is in and prepend that so it is not 'frozenset' but '_frozenset' one needs to replace.与第一直觉相反, frozenset.__str__()似乎(我没有调查过)检查它所在的class的名称并在前面加上它,因此它不是 'frozenset' 而是需要替换的 '_frozenset'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM