Skip to content
This repository was archived by the owner on Feb 14, 2024. It is now read-only.

Conversation

@guoqiao
Copy link

@guoqiao guoqiao commented Aug 22, 2019

Small improvements, could be worthy.

In py2:

    sorted(iterable[, cmp[, key[, reverse]]])

In py3:

    sorted(iterable, *, key=None, reverse=False)

The `cmp` arg is removed in py3.
Also, it's much slower than `key` since `cmp` is called multiple times
for each list element(O(n^2)), while `key` touch each element only once(O(n)).

Replace `cmp` with `key` as a keyword arg, this make `sorted` faster and
also python 2/3 compatible.

Signed-off-by: Joe Guo <[email protected]>
In py2:

    dict.items() -> list
    dict.iteritems() -> iterator

iteritems() is preferred for less memory usage in py2.

In py3:

    dict.items() -> iterator
    dict.iteritems() -> removed

Use items() instead of iteritems(), to be python 2/3 compatible.

In py2, the returned list may use more memory, but shouldn't be problem
in this case.

Didn't use six, which is unnecessary here and introduce noise in code.

Signed-off-by: Joe Guo <[email protected]>
make use of maxsplit arg to get db name and the rest in one line.

Signed-off-by: Joe Guo <[email protected]>
make use of defaultdict(dict) to avoid key check each time.

Signed-off-by: Joe Guo <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant