-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flattening list of lists of dicts leads to surprising results #3
Comments
Currently, |
By the way, if you just want to concatenate one level of iterables in |
To clarify, @tuturto, did you expect |
The latter one, I expected dictionaries be left as they were. And even if dictionaries were flattened, we shouldn't be discarding half of the information (keeping only keys and discarding values) while doing so. |
Don't use
It's a mere implementation detail that this works at all. A type checker has every right to flag that use, and other Python implementations (even later CPython implementations) might not support it. |
Yes, it's allegedly an implementation detail, but it's an obvious desideratum. I doubt it will ever be removed. |
In Python, dicts are iterables. Their iterator returns the keys. In a for loop, this is more useful, since you can still use the keys to get the values via lookup. If you want the pairs, you have to use Clojure arguably handles this more consistently. user=> (seq {1 2, 3 4})
([1 2] [3 4]) The default iterator is like Python's So how does Clojure flatten maps? user=> (flatten {1 2, 3 4})
()
user=> (doc flatten)
-------------------------
clojure.core/flatten
([x])
Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat sequence.
(flatten nil) returns an empty sequence.
nil
user=> (flatten (seq {1 2, 3 4}))
(1 2 3 4) It doesn't. Flatten should support third-party iterables too. How can you treat them consistently if you only make dicts a special case? This is how Python is. How do you want this to work? |
It looks like Clojure keeps maps in one piece when they're not the top level. user=> (flatten [10 20 [{1 2, 5 6} {3 4}]])
(10 20 {1 2, 5 6} {3 4}) |
Right; that's the hard part about choosing the right criterion, I think. Perhaps, using the types of |
Clojure also keeps sets in one piece. user=> (flatten [10 #{1 2 3}])
(10 #{1 3 2}) I don't think "iterable" is the right criterion. The appropriate abc is probably |
Except strings or bytestrings. Is an >>> isinstance(OrderedDict(), abc.Sequence)
False Python says no. |
Instead of
I was expecting
The text was updated successfully, but these errors were encountered: