How to subtract one from each value in a tuple in Python?

I have a tuple created with help zip(), and I need to subtract it from every integer in the tuple. I tried the following, but apparently it only works on lists, so how would I adapt this for tuples in Python?

[...]
lower, upper = zip(*table)
lower[:] = [x + 1 for x in lower]
upper[:] = [x - 1 for x in upper]
holes = zip(lower[:-1], upper[1:])

TypeError: 'tuple' object does not support element assignment

Large image. I have a series of disjoint sorted intervals stored in table, and I need to get a series of holes. For instance. my spacing table could be:

[ 6,  7]
[ 8,  9]
[14, 18]
[23, 32]

And I want to calculate holesbetween intervals:

[10, 13]
[19, 22]
+5
source share
3 answers

Using the generator function makes the task easier.

table = [(2,3),(5,6),(12,20),(21,25),(28,28),(35,48),(53,55)]

def gaps_between(intervals):
    prec = intervals[0][1] + 1
    for L,H in intervals:
        print '\nprec = %d   (L,H) = (%d,%d)' % (prec,L,H)
        print 'prec = %d <= L-1 = %d : %s' % (prec,L-1,prec<=L)
        if prec<=L-1:
            yield (prec,L-1)
        prec = H + 1
        print 'next prec = %d' % prec

holes = list(gaps_between(table))

print
print 'table =',table
print 'holes =',holes 

, .
, :
first prec = first H = intervals[0][1].
, H>=L (L, H), first H > first L - 1 β†’ first prec > first L - 1.
, False, .

prec = 3   (L,H) = (2,3)
prec = 3 <= L-1 = 1 : False
next prec = 4

prec = 4   (L,H) = (5,6)
prec = 4 <= L-1 = 4 : True
next prec = 7

prec = 7   (L,H) = (12,20)
prec = 7 <= L-1 = 11 : True
next prec = 21

prec = 21   (L,H) = (21,25)
prec = 21 <= L-1 = 20 : True
next prec = 26

prec = 26   (L,H) = (28,28)
prec = 26 <= L-1 = 27 : True
next prec = 29

prec = 29   (L,H) = (35,48)
prec = 29 <= L-1 = 34 : True
next prec = 49

prec = 49   (L,H) = (53,55)
prec = 49 <= L-1 = 52 : True
next prec = 56

table = [(2, 3), (5, 6), (12, 20), (21, 25), (28, 28), (35, 48), (53, 55)]
holes = [(4, 4), (7, 11), (26, 27), (29, 34), (49, 52)]

:
- (4,4) (2,3) (5,6)
- (12,20) (21,25)
- 28, (28,28), -

OP , .
if prec<=L-1 , :
....., (7, 11), (21, 20), (26, 27), .......

.

,
[[ 8, 9],[14, 18],[18, 32]]
[[8, 9], [14, 18], [19, 20], [16, 21], [23, 32]]
( , OP)

,
.

, , , .

.

yield (prec,L-1) yield range(prec,L) .

yield (prec,L-1) holes.append((prec,L-1)), , .

+2

tuple :

lower = tuple(x - 1 for x in lower)
upper = tuple(x + 1 for x in upper)
+6

:

holes = [(table[i][1]+1, table[i+1][0]-1) for i in range(len(table)-1)]
+6

All Articles