GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update `combinations` docs by ruaridhw · Pull Request #19732 · python/cpython

Conversation

@ruaridhw

@ruaridhw ruaridhw commented Apr 27, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • The order of the iterable is not modified within combinations() as these docs imply
  • Instead, the order of elements is used as-is
>>> from itertools import combinations
>>> list(combinations(list(range(4)), 2))
## [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
>>> list(combinations(reversed(list(range(4))), 2))
## [(3, 2), (3, 1), (3, 0), (2, 1), (2, 0), (1, 0)]

I would have expected that should these docs be true, the last line starts with (0, 1) or perhaps (2, 3) if we are to interpret that the tuple itself is sorted. Instead, the order of iterable is used (as expected).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ruaridhw
ruaridhw requested a review from rhettinger as a code owner April 27, 2020 07:48
@bedevere-bot bedevere-bot added docs Documentation in the Doc dir awaiting review labels Apr 27, 2020
@the-knights-who-say-ni

the-knights-who-say-ni commented Apr 27, 2020

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@ruaridhw

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@the-knights-who-say-ni the-knights-who-say-ni added the CLA not signed label Apr 27, 2020
@ruaridhw

ruaridhw commented Apr 27, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Found these docs a little misleading, please let me know if this requires a BPO entry.

The same can be said of combinations_with_replacement(), permutations(), and product() so if this is applicable, probably best to add changes to those docs as well.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

remilapeyre
remilapeyre reviewed Apr 27, 2020
View reviewed changes

@remilapeyre remilapeyre left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ruaridhw, thanks for taking the time to improve the documentation.

I'm not sure this new wording is much better though, I think we should keep the reference to the lexicographic order. Maybe we should move the note that elements are used based on their order and not their value here instead?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ruaridhw

ruaridhw commented Apr 27, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I think we should keep the reference to the lexicographic order

@remilapeyre, what was your reasoning for this? Personally, this was the confusing part for me and the only reference that I'd like to remove. I think I'm missing something because I can't see how these methods care about lexicographic order at all.

Take for example, a completely unsortable class:

from itertools import combinations


class NotSortable:
    def __init__(self, id_):
        self.id = id_

    def __repr__(self):
        return f'{self.id}'


sorted([NotSortable(3), NotSortable(2)])
## TypeError: '<' not supported between instances of 'NotSortable' and 'NotSortable'
combs = list(combinations([NotSortable(3), NotSortable(2), NotSortable(4), NotSortable(1)], 2))
print(combs)
## [(3, 2), (3, 4), (3, 1), (2, 4), (2, 1), (4, 1)]

Surely any "lexicographic order" (or any ordering for that matter other than the one provided) of this input iterable is undefined?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@remilapeyre

remilapeyre commented Apr 27, 2020

Copy link
Copy Markdown
Contributor

With this unsortable class we can have the objects

a = NotSortable(3)
b = NotSortable(2)
c = NotSortable(4)
d = NotSortable(1)

and we can define pos() so that

pos(a) = 1
pos(b) = 2
pos(c) = 3
pos(d) = 4

and pos() defines an order over {a, b, c, d} and combinations() will output pairs in lexicographic order. It's just not the order defined by NotSortable.__lt__() but the one defined by list.index(). It can be confusing but it seems to me that it is correct and we should maybe improve the phrasing but not remove the whole paragraph.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ruaridhw

ruaridhw commented Apr 27, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Hmmm that does seem highly confusing. My point is that there are no cases where it output order is defined by anything other than list.index(). Hence we should explicitly state this.

Stating "combinations are emitted in lexicographic sort order" reads to me as if the elements of the iterable or the iterable itself must define a lexicographic sort order and this is not the case. Especially because if the elements do define a lexicographic sort order (eg. str), this is not the order of the output pairs:

>>> print(list(combinations('dbca', 2)))
## [('d', 'b'), ('d', 'c'), ('d', 'a'), ('b', 'c'), ('b', 'a'), ('c', 'a')]
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger rhettinger self-assigned this Apr 28, 2020
rhettinger
rhettinger reviewed Apr 28, 2020
View reviewed changes
Comment thread Doc/library/itertools.rst Outdated

@rhettinger rhettinger Apr 28, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the second sentence.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@csabella csabella removed the CLA not signed label May 23, 2020
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label May 23, 2020
@csabella

csabella commented May 23, 2020

Copy link
Copy Markdown
Contributor

@ruaridhw, please address the code review comments. Thank you!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ruaridhw
ruaridhw force-pushed the patch-1 branch from d126dcb to 5caf2f8 Compare May 26, 2020 10:08
@csabella
csabella changed the base branch from 3.8 to master May 26, 2020 11:36
@csabella
csabella changed the base branch from master to 3.8 May 26, 2020 11:37
@csabella

csabella commented May 26, 2020

Copy link
Copy Markdown
Contributor

@ruaridhw, thank you for making the change. I apologize for not seeing this before, but I just noticed you made this PR against the 3.8 branch instead of the master branch. Generally, all of our PRs are against master and then we have a bot that will backport changes to other releases. Based on that, please change this to be over the master branch. It's a little more involved than just changing the base branch. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ruaridhw Ruaridh Williamson
📚 Update combinations docs
ccb982c
- The order of the iterable is not modified within `combinations()` as these docs imply
- Instead, the order of elements is used as-is
@ruaridhw
ruaridhw force-pushed the patch-1 branch from 5caf2f8 to ccb982c Compare May 28, 2020 07:24
@ruaridhw
ruaridhw requested review from 1st1, asvetlov, berkerpeksag, brettcannon, encukou, ericsnowcurrently, ericvsmith, ezio-melotti and gpshead as code owners May 28, 2020 07:24
@ned-deily
ned-deily requested review from rhettinger and removed request for ericsnowcurrently, ericvsmith, ezio-melotti, gvanrossum, ilevkivskyi, ncoghlan, pablogsal, skrah, terryjreedy and tiran May 28, 2020 07:37
@the-knights-who-say-ni the-knights-who-say-ni added CLA signed and removed CLA not signed labels May 28, 2020
@ned-deily ned-deily added skip issue skip news labels May 28, 2020
@ned-deily ned-deily changed the title Update combinations docs Update itertools combinations docs May 28, 2020
@ned-deily ned-deily changed the title Update itertools combinations docs [3.9] Update combinations docs May 28, 2020
@ned-deily ned-deily changed the title [3.9] Update combinations docs Update combinations docs May 28, 2020
rhettinger
rhettinger approved these changes May 28, 2020
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels May 28, 2020
@rhettinger

rhettinger commented May 28, 2020

Copy link
Copy Markdown
Contributor

Attempting to restart the bots by closing and reopening.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger rhettinger closed this May 28, 2020
@rhettinger rhettinger reopened this May 28, 2020
@rhettinger rhettinger added needs backport to 3.9 labels May 28, 2020
@miss-islington

miss-islington commented May 28, 2020

Copy link
Copy Markdown
Contributor

Thanks @ruaridhw for the PR, and @rhettinger for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented May 28, 2020

Copy link
Copy Markdown

GH-20501 is a backport of this pull request to the 3.9 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented May 28, 2020

Copy link
Copy Markdown

GH-20502 is a backport of this pull request to the 3.8 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mdickinson
mdickinson reviewed May 31, 2020
View reviewed changes
Comment thread Doc/library/itertools.rst
in sorted order.
The permutation tuples are emitted in lexicographic ordering according to
the order of the input *iterable*. So, if the input *iterable* is sorted,
the combination tuples will be produced in sorted order.

@mdickinson mdickinson May 31, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should "combination tuples" be "permutation tuples" here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ruaridhw ruaridhw Jun 1, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good spot 🤦

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@rhettinger rhettinger rhettinger approved these changes
+2 more reviewers
@mdickinson mdickinson mdickinson left review comments
@remilapeyre remilapeyre remilapeyre left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@rhettinger rhettinger

Labels

docs Documentation in the Doc dir skip issue skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

9 participants

@ruaridhw @the-knights-who-say-ni @remilapeyre @csabella @rhettinger @miss-islington @bedevere-bot @mdickinson @ned-deily
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.