Problems with Python sets

21 views
Skip to first unread message

Jonah Thomas

unread,
Jul 8, 2022, 8:26:38 PM7/8/22
to glowscri...@googlegroups.com
I am getting some failures with Python sets.
a={1,2,3} gets an error, but
a=set((1,1,1,2))  gets no error.
print(a) then gives a lot of code, but
print([a]) gives  [{1, 2}]

a=set(1,2)
print([a]) gives [{}] I can live with that.
 
if 1 in a: print(1) gives 1. Good.

But I don't find any way to put tuples into a set.

a=set([1,2], [1,3], [1,1])
print([a]) gives [{1, 2}] as if it processes the first item.

a=set(([1,2], [1,3], [1,1]))
print([a]) gives [{[1, 2], [1, 3], [1, 1]}] as if it keeps the whole thing as one set element. But I can't find a way to get it to say that item is in the set. I haven't found any combination of writing them as tuples (instead of the lists as above) that works.

a=set((1,2,3))
b=set((4,5,6))
c=a.union(b)
print([c]) gives [{1, 2, 3, 4, 5, 6}] Good.

Do tuples just not work? Or is there a workable syntax I didn't find?
--
Jonah Thomas

We are limited, but the limits aren't where we think they are. We find them only by exceeding them.



Bruce Sherwood

unread,
Jul 9, 2022, 1:20:51 AM7/9/22
to Glowscript Users
Thanks for the report that lists and tuples don't work in sets. I'll comment that JavaScript, which Web VPython compiles to in order to be able to run in the browser, does not have something equivalent to the Python tuple.

I don't see the problems you see with elements that are numbers. Is it possible that you are using an old version of Web VPython?

Web VPython 3.2
# OK:
s = {1,2,3}
print(s, 2 in s, 7 in s) # {1, 2, 3} True False

# OK:
s = set((1,2,3))
print(s) # {1, 2, 3}

# Does not handle lists
s = {[1,2], [3,4], [5,6]}
print(s) # {[1, 2], [3, 4], [5, 6]}
s = set(([1,2], [3,4], [5,6]))
print(s, [3,4] in s) # {[1, 2], [3, 4], [5, 6]} False
s = set(((1,2), (3,4), (5,6)))
print(s) # {[1, 2], [3, 4], [5, 6]}
print([3,4] in s) # False; should be True

Harlan Gilbert

unread,
Jul 9, 2022, 1:53:56 AM7/9/22
to glowscri...@googlegroups.com
All the statements below work properly for me:

Web VPython 3.2
a = {1,2,3}
b=set((1,2,3))
print(a,b)
c=set(([1,2],[3,4]))
d={[5,6],[7,8]}
print(c,d)

But funny things happen in the following, including a different treatment of tuples and lists as parts of sets. Tuples are dissolved and merged, lists are retained as separate structures:

s = set(((1,2), (3,4), (5,6)))
print(s) #  {1, 2, 3, 4, 5, 6}  merges tuples

s = set(([1,2], [3,4], [5,6]))
print(s) # {[1, 2], [3, 4], [5, 6]}  does not merge lists
print([3,4] in s) # False; but still does not recognize them as members of the set

--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/glowscript-users/b4d0ef47-19e4-4caf-880e-0fc8bab0e784n%40googlegroups.com.


--
Harlan Gilbert, Ph.D.
High School Chair and Math, Physics, Computer Science, and Philosophy Teacher
Green Meadow Waldorf High School
Chestnut Ridge, NY 10977

Bruce Sherwood

unread,
Jul 9, 2022, 9:49:02 AM7/9/22
to Glowscript Users
The major problem is that the "in" operation fails with lists. I thought of a workaround.

Make a list of the lists to go into the set.
Place in the set the indices of those lists in the list.
Then when locating an item in the set, use it to index into the list of lists.

Bruce

Harlan Gilbert

unread,
Jul 9, 2022, 9:52:23 AM7/9/22
to glowscri...@googlegroups.com
The other problem is that tuples are not properly handled:

s = set(((1,2), (3,4), (5,6)))
print(s)  

The above should give 
{(1,2), (3,4), (5,6)}
But instead gives
 {1, 2, 3, 4, 5, 6} 
--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages