4 ms·
To avoid repetitions, can you do this? assert sorted(result) == sorted(['a','b'])
by oli5679 6y ago
To avoid repetitions, can you do this?
assert sorted(result) == sorted(['a','b'])
- quietbritishjim 6y agoAh that's better than my collections.Counter trick. Yours only needs the items to comparible rather than hashable, which is usually a looser requirement. (In principle you could have an object that's hashable but not comparible I can't immediately think of any).
- duckerude 6y agoThe main problem is probably heterogenous collections. If you have a collection with both ints and strings then hashing will work fine but comparing will fail (in Python 3). (Frozensets are hashable but not sortable but that's probably more niche.)
- wodenokoto 6y agoI think that only works if `result` is a list
- alexchamberlain 6y agoNah, sorted can be used on any iterable
- BerislavLopac 6y agoOr even better (since we're testing against an oracle value anyway): assert sorted(result) == ['a', 'b']
- oli5679 6y agoTrue you can just sort your oracle values once/in your head this is a valid point. Reasonable to prefer this way!