How would one achieve an inline 'but' in python?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1773 Views - Last Post: 13 January 2016 - 12:28 PM Rate Topic: -----

#16 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: How would one achieve an inline 'but' in python?

Posted 13 January 2016 - 10:48 AM

.. there are also iterators and generators to consider.
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: How would one achieve an inline 'but' in python?

Posted 13 January 2016 - 11:10 AM

For example, a generator:
def RangeEx(n, excludes):
    i = 0
    while i < n:
        while i in excludes:
            i += 1
        yield i
        i += 1

print(list(RangeEx(19, (15, 16))))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 18]



Added: This isn't complete because it is possible to carry beyond the range-end with, for example, excludes of 18,19,20.

Something like this is nearer:
def RangeEx(n, excludes):
    i = 0
    while i < n:
        while i in excludes:
            i += 1
        if i < n:
            yield i
        i += 1


Was This Post Helpful? 0
  • +
  • -

#18 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: How would one achieve an inline 'but' in python?

Posted 13 January 2016 - 11:22 AM

An iterator:

class RangeEx(object):
    def __init__(self, n, excludes):
        self.i = 0
        self.n = n
        self.excludes = excludes

    def __iter__(self):
        return self

    def __next__(self):
        if self.i < self.n:
            i = self.i
            self.i += 1
            if self.i in self.excludes:
                self.__next__()
            return i
        else:
            raise StopIteration()

print(list(RangeEx(19, (15, 16))))

# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 18]

(Python 3 uses __next__ rather than next.)
Was This Post Helpful? 0
  • +
  • -

#19 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,765
  • Joined: 27-December 13

Re: How would one achieve an inline 'but' in python?

Posted 13 January 2016 - 11:51 AM

Following OP request, we just had a similar situation in other thread.
How do you like this:
class MyList(list):
    def __init__(self, lst):
        self.lst = lst

    def __str__(self):
        return str(self.lst)

    def __sub__(self, other):
        return [x for x in self.lst if x not in other.lst]

a = MyList([x for x in range(19)])
b = MyList([16, 17])

print(a)
print(b )
print(a-b )


Producing this result:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[16, 17]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18]


Validation of input and exceptions are omitted here, - code only indicative.

Note that DIC sometime prints normal 'b' as Capitals and adds />

This post has been edited by DK3250: 13 January 2016 - 12:12 PM

Was This Post Helpful? 1
  • +
  • -

#20 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: How would one achieve an inline 'but' in python?

Posted 13 January 2016 - 12:00 PM

Quote

Note that DIC sometime prints normal 'b' as Capitals and adds />

Note that this can be avoided by placing a space between b and ).
Was This Post Helpful? 0
  • +
  • -

#21 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,765
  • Joined: 27-December 13

Re: How would one achieve an inline 'but' in python?

Posted 13 January 2016 - 12:12 PM

ok, did the edit in my post #19
Was This Post Helpful? 0
  • +
  • -

#22 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: How would one achieve an inline 'but' in python?

Posted 13 January 2016 - 12:19 PM

View PostDK3250, on 13 January 2016 - 06:51 PM, said:

Following OP request, we just had a similar situation in other thread.
How do you like this:

..

My 2 cents. I would favour sets unless there is a particular reason to not use them. When people look to subtract lists they usually mean to subtract paired elements, like this discussion at SO. Whereas set differences, and set algebra, are well defined.
Was This Post Helpful? 0
  • +
  • -

#23 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 560
  • View blog
  • Posts: 1,765
  • Joined: 27-December 13

Re: How would one achieve an inline 'but' in python?

Posted 13 January 2016 - 12:28 PM

Ok, point taken. Personally I also favor your generator approach. Just wanted to show the possibility of using subtract, and was inspired by the other thread.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2