Hi Roberto, It looks like you're expecting python sets to do something they're not intended to do. From the docs <https://docs.python.org/3.7/library/stdtypes.html#set-types-set-frozenset>, "A set object is an unordered collection". Of course when you iterate over all the elements they have to come out in some order, but that's an implementation choice. Elements are not even guaranteed to be returned in the same order each time! (Though probably they will be.)
I can see how one might expect enumerate to only make sense for containers which have some guaranteed order, but that's not the way it's been designed to work. It simply iterates over any iterable and gives you an index counting how many elements have been seen so far*. In general if you want a container with guaranteed ordering, you probably want to use a list or a tuple, or even a dict (which has had ordering guarantees since python 3.7). You might also be interested in the answers to this stackoverflow question on implementing an OrderedSet <https://stackoverflow.com/q/1653970>in python. * Actually one is able to change the starting value of this index from 0 to something else, but that's somewhat beside the point. On Thursday, July 16, 2020 at 5:34:27 PM UTC-4, Roberto wrote: > > I am starting to wonder why python is so loose on this. I say "python" > because the issue appears for enumerate as well. How can I enumerate a set? > Still I am allowed to enumerate a set without receiving any warning. Look > at this for instance > > set(["a","b"])==set(["b","a"]) > list(enumerate(set(["a","b"]))) > list(enumerate(set(["b","a"]))) > import sympy as sp > _a, _b = sp.symbols('a b') > set([_a,_b])==set([_b,_a]) > list(enumerate(set([_a,_b]))) > list(enumerate(set([_b,_a]))) > > > Funny enough, moving this code from one notebook to another may change the > order in which the sets are ordered. In all cases they are ordered in the > same way within the same notebook, but you and I running the same code may > get different outcomes! > I do not know if this is special with sets of sympy symbols, but I would > have said that deep down enumerate should throw a warning when you try to > enumerate a set. > > > > On Thursday, July 9, 2020 at 9:55:15 PM UTC+2, Aaron Meurer wrote: >> >> Can you open an issue in the issue tracker for this? I agree that sets >> should not be allowed. >> >> Aaron Meurer >> >> On Thu, Jul 9, 2020 at 3:08 AM Roberto <francesch...@gmail.com> wrote: >> > >> > I have seen that lambdify wants a list for the arguments to be treated >> as symbols. This list can be also give as a python set, e.g. {x,y,z }, >> which is exactly what would be returned by a .free_symbol property. If >> lambdify is feed a set like that of the output of .free_symbol it may >> change their order in the conversion from set to list. This is done >> silently and may cause major disfunction in the use of the lamdified >> function because you think x is x, but is y instead and so on. >> > I would like to ask developers to check for the type of the lambdify >> list of arguments and throw a warning to flag that a set is being converted >> and that the ordering is not guaranteed. >> > Given that .free_symbols returns a set, not a list, this is very common >> pitfall in my opinion and must be prevented. >> > >> > -- >> > You received this message because you are subscribed to the Google >> Groups "sympy" group. >> > To unsubscribe from this group and stop receiving emails from it, send >> an email to sy...@googlegroups.com. >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/sympy/c1517b79-490b-4a1b-b526-3e3842b55fbco%40googlegroups.com. >> >> >> > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/ff8185d8-e34b-4353-82c1-0bb9fa3dd610o%40googlegroups.com.