And if you can't / don't want to create new lists for the values you
want to compare, a solution could be to use zip & all builtins:
print(all(a == b for a, b in zip(tuple_, list_)))
Le sam. 2 mai 2020 à 16:58, Steele Farnsworth
You can get the desired behavior by casting a list to a tuple, or a tuple to a list, in the equality statement. That way those that rely on the existing implementation don't have to change their code.
my_tup = (1, 2, 3) my_list = [1, 2, 3] print(list(my_tup) == my_list)
On Sat, May 2, 2020, 9:04 AM Ahmed Amr
wrote: I'd like to take your opinion on modifying some of the indexed collections like tuples, lists, arrays to evaluate its equality to True when having the same items in the same indexes. Currently, when comparing a list of items to an array of the same items for equality (==) it returns False, I'm thinking that it would make sense to return True in that context, as we're comparing item values and we have the same way of indexing both collections, so we can compare item values.
So what do you think about applying such behavior on collections that can be indexed the same way such as tuples, lists, and arrays?
Example: (Current)
import array tuple_ = (1.1, 2.2, 3.3) list_ = [1.1, 2.2, 3.3] array_ = array.array('f', [1.1, 2.2, 3.3])
# all of the following prints False. print(tuple_ == list_) print(tuple_ == array_) print(array_ == list_)
Example: (Proposed): All prints above to show True as they are populated with the same data in the same indexes.
A Side Note: An extra point to discuss, based on arrays implementation, array_.to_list() would actually get [1.100000023841858, 2.200000047683716, 3.299999952316284] which is not exactly what we've passed as args and this is normal, but I'm thinking about leaving it to the array implementation to encapsulate that implementation and perform exact equality based on passed arguments. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/UE5HGE... Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/NPEIUL... Code of Conduct: http://python.org/psf/codeofconduct/
-- Antoine Rozo