最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to create set with values 1, 0, True, False along with other values in the same set - Stack Overflow

programmeradmin3浏览0评论

I am trying to create a set having different values along with True, False, 1, 0.

I am aware that Python treats 1 as True and 0 as False. Imperatively, since the set keeps only the unique values, my set stores either of 1 or True, and of 0 or False.

Is there any workaround to deal with the above requirement?

set1 = {"Python", "Java", 1, 0, 23.45, True, False}

Output1: {"Python", "Java", 1, 0, 23.45} (not in the same order obviously)

Output2: {"Python", "Java", 1, False, 23.45} (not in the same order obviously)

I am trying to create a set having different values along with True, False, 1, 0.

I am aware that Python treats 1 as True and 0 as False. Imperatively, since the set keeps only the unique values, my set stores either of 1 or True, and of 0 or False.

Is there any workaround to deal with the above requirement?

set1 = {"Python", "Java", 1, 0, 23.45, True, False}

Output1: {"Python", "Java", 1, 0, 23.45} (not in the same order obviously)

Output2: {"Python", "Java", 1, False, 23.45} (not in the same order obviously)

Share Improve this question edited Mar 17 at 12:36 mkrieger1 23.5k7 gold badges64 silver badges82 bronze badges asked Mar 17 at 12:28 VenkataVenkata 3111 gold badge3 silver badges11 bronze badges 4
  • Python hashes them to the same value. From the perspective of a set they are identical. – Klaus D. Commented Mar 17 at 12:34
  • 9 You may have to workshop that "requirement" if it's technically not possible/not easy. Before trying to implement some workarounds which may or may not have side effects, what's the goal with this exactly? Then at least we'd be able to tell whether the side effects of any proposed solution may interfere with those goals. – deceze Commented Mar 17 at 12:36
  • 1 yes, they can't both exist in a set also because 1 == True. You can see this related question: stackoverflow/questions/30851580/… If you share the end goal we can try to think of another approach – Gei Stoyanov Commented Mar 17 at 12:45
  • You could use a little enumeration to replace True and False in your context – Timus Commented Mar 17 at 13:45
Add a comment  | 

1 Answer 1

Reset to default 2

The problem is, that set’s logic for existence uses both "equality" and "hash", and True==1, as well as hash(1) == hash(True)

So I see only 2 workarounds

You could write your own set with a wrapper (see how contains and add now also checks for "type")

class MySet:
    def __init__(self):
        self._data = {}
    
    def add(self, item):
        key = (type(item), item)
        self._data[key] = None
    
    def __contains__(self, item):
        key = (type(item), item)
        return key in self._data
    
    def __len__(self):
        return len(self._data)
    
    def __iter__(self):
        for (typ, val) in self._data.keys():
            yield val
    
    def __repr__(self):
        items = ', '.join(repr(x) for x in self)
        return f"MySet({{{items}}})"


s = MySet()
s.add(True)
s.add(1)
s.add(False)
s.add(0)

print(s) # PRINTS MySet({True, 1, False, 0})

OR you could tag them in tuples:

set2 = {
    ("int", 1),
    ("bool", True),
    ("int", 0),
    ("bool", False),
    ("str", "Python"),
    ("str", "Java"),
    ("float", 23.45),
}

if you want to add something, you could run ( or maybe write add_value method) ?)

value = 1 
set2.add(str(type(value)),value) 

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论