This request is called "one" in more-itertools:
http://more-itertools.readthedocs.io/en/latest/api.html
It raises ValueError as Steve suggested.
On Monday, October 30, 2017 at 8:34:26 AM UTC-6, Guido van Rossum wrote:
>
> This is a key example of a case where code speaks. Can you write an
> imp
On 31Oct2017 22:50, Greg Ewing wrote:
Koos Zevenhoven wrote:
|defsingle(i): try: ||v =i.next()
|||exceptStopIteration:raiseException('No values')|||try: ||i.next()
||exceptStopIteration: ||returnv||else: ||raiseException('Too many
values')|||printsingle(name forname in('bob','fred')
On 2017-10-31 10:58 AM, Joao S. O. Bueno wrote:
On 31 October 2017 at 10:52, Steven D'Aprano wrote:
On Tue, Oct 31, 2017 at 10:42:23AM -0200, Joao S. O. Bueno wrote:
When I need something like this, I usually rop a line on the module
namespace that goes like:
first = lambda x: next(iter(x))
On 31 October 2017 at 10:52, Steven D'Aprano wrote:
> On Tue, Oct 31, 2017 at 10:42:23AM -0200, Joao S. O. Bueno wrote:
>> When I need something like this, I usually rop a line on the module
>> namespace that goes like:
>>
>> first = lambda x: next(iter(x))
>
> That doesn't meet the requirement th
On Tue, Oct 31, 2017 at 10:42:23AM -0200, Joao S. O. Bueno wrote:
> When I need something like this, I usually rop a line on the module
> namespace that goes like:
>
> first = lambda x: next(iter(x))
That doesn't meet the requirement that x has ONLY one item.
And using lambda like that is bad st
When I need something like this, I usually rop a line on the module
namespace that goes like:
first = lambda x: next(iter(x))
On 30 October 2017 at 23:09, Steven D'Aprano wrote:
> On Tue, Oct 31, 2017 at 07:51:02AM +1100, Cameron Simpson wrote:
>
>> return the(nodes)
>>
>> It's this kind of th
(Reposting this to list -- pushed wrong reply button!)
Koos Zevenhoven
wrote:
|defsingle(i): try: ||v =i.next()
|||exceptStopIteration:raiseException('No
values')|||try: ||i.next() ||exceptStopIteration: ||returnv||else:
||raiseException('Too many values')|||printsingle(name f
>
>
> |defsingle(i): try: ||v =i.next()
> |||exceptStopIteration:raiseException('No
> values')|||try: ||i.next() ||exceptStopIteration: ||returnv||else:
> ||raiseException('Too many values')|
> ||printsingle(name forname in('bob','fred')ifname=='bob')||| |
>
>
Now that looks seriously weird.
On Tue, Oct 31, 2017 at 3:50 PM, Ivan Pozdeev via Python-ideas
wrote:
> On 30.10.2017 17:32, Guido van Rossum wrote:
>>
>> This is a key example of a case where code speaks. Can you write an
>> implementation of how you would want single() to work in Python code?
>>
>> On Mon, Oct 30, 2017 at 2:49
On 30.10.2017 17:32, Guido van Rossum wrote:
This is a key example of a case where code speaks. Can you write an
implementation of how you would want single() to work in Python code?
On Mon, Oct 30, 2017 at 2:49 AM, Ivan Pozdeev via Python-ideas
mailto:python-ideas@python.org>> wrote:
Th
On Tue, Oct 31, 2017 at 07:51:02AM +1100, Cameron Simpson wrote:
> return the(nodes)
>
> It's this kind of thing that expresses my intent better than the:
>
> node, = nodes
> return node
>
> idiom.
If the intent is to indicate that there is only one node, then
"the(nodes)" fails completely
On 30Oct2017 07:32, Guido van Rossum wrote:
This is a key example of a case where code speaks. Can you write an
implementation of how you would want single() to work in Python code?
Myself, I'm not advocating for putting such a thing in itertools. However, I do
have an equivalent utility func
This is a key example of a case where code speaks. Can you write an
implementation of how you would want single() to work in Python code?
On Mon, Oct 30, 2017 at 2:49 AM, Ivan Pozdeev via Python-ideas <
python-ideas@python.org> wrote:
>
>
> On 30.10.2017 9:29, python-ideas-requ...@python.org wrot
On 30.10.2017 9:29, python-ideas-requ...@python.org wrote:
If I have understood your use-case, you have a function that returns a
list of results (or possibly an iterator, or a tuple, or some other
sequence):
print(search(haystack, needle))
# prints ['bronze needle', 'gold needle', '
IIUC, this would be similar to "first" ( https://pypi.python.org/pypi/first/
) but would raise exception in case the iterable returns more than one (or
less than one) element.
Would also be similar to one() in SQLAlchemy queries (
http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.
On Mon, Oct 30, 2017 at 07:14:10AM +0300, Ivan Pozdeev via Python-ideas wrote:
> The eponymous C#'s LINQ method, I found very useful in the following,
> quite recurring use-case:
If I have understood your use-case, you have a function that returns a
list of results (or possibly an iterator, or
The eponymous C#'s LINQ method, I found very useful in the following,
quite recurring use-case:
I need to get a specific element from a data structure that only
supports search semantics (i.e. returns a sequence/iterator of results).
For that, I specify very precise search criteria, so only tha
17 matches
Mail list logo