LINUX.ORG.RU

История изменений

Исправление Nervous, (текущая версия) :

Шота у меня c данными ТСа не работает:

def three(a, b):
    c = list(a)
    ind1 = 0
    for elem in c:
        key1 = elem[0]
        ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
        if not(ind2 is None):
            c[ind1] = (key1, elem[1]+b[ind2][1])
        ind1 += 1

    return sorted(c, key=lambda x: x[1], reverse=True)


a = [(0,10.8),(1,8.2),(2,0.3)]
b = [(0,0.4),(3,20.2),(2,0.3)]


three(a, b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nervous/tuples.py", line 48, in three
    ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
StopIteration

Хотя с синтетическими все окей (в смысле не падает) и даже шустрее всех:

data_size = 100
iterations = 1000

xs = [(x, x*x) for x in range(data_size)]
ys = [(x, x + x) for x in range(data_size)]

...

One imperative:  4.3429664069844875
One pythonic:  4.039353622996714
Two:  3.1694852040091064
Three:  1.737760635005543

Исправление Nervous, :

Шота у меня c данными ТСа не работает:

def three(a, b):
    c = list(a)
    ind1 = 0
    for elem in c:
        key1 = elem[0]
        ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
        if not(ind2 is None):
            c[ind1] = (key1, elem[1]+b[ind2][1])
        ind1 += 1

    return sorted(c, key=lambda x: x[1], reverse=True)


a = [(0,10.8),(1,8.2),(2,0.3)]
b = [(0,0.4),(3,20.2),(2,0.3)]


three(a, b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nervous/tuples.py", line 48, in three
    ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
StopIteration

Хотя с синтетическими все окей и даже шустрее всех:

data_size = 100
iterations = 1000

xs = [(x, x*x) for x in range(data_size)]
ys = [(x, x + x) for x in range(data_size)]

...

One imperative:  4.3429664069844875
One pythonic:  4.039353622996714
Two:  3.1694852040091064
Three:  1.737760635005543

Исправление Nervous, :

Шота у меня c данными ТСа не работает:

def three(a, b):
    c = list(a)
    ind1 = 0
    for elem in c:
        key1 = elem[0]
        ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
        if not(ind2 is None):
            c[ind1] = (key1, elem[1]+b[ind2][1])
        ind1 += 1

    return sorted(c, key=lambda x: x[1], reverse=True)


a = [(0,10.8),(1,8.2),(2,0.3)]
b = [(0,0.4),(3,20.2),(2,0.3)]


three(a, b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nervous/tuples.py", line 48, in three
    ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
StopIteration

Хотя с синтетическими все окей и даже шустрее всех:

One imperative:  4.3429664069844875
One pythonic:  4.039353622996714
Two:  3.1694852040091064
Three:  1.737760635005543

Исправление Nervous, :

Шота у меня c данными ТСа не работает:

def three(a, b):
    c = list(a)
    ind1 = 0
    for elem in c:
        key1 = elem[0]
        ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
        if not(ind2 is None):
            c[ind1] = (key1, elem[1]+b[ind2][1])
        ind1 += 1

    return sorted(c, key=lambda x: x[1], reverse=True)


a = [(0,10.8),(1,8.2),(2,0.3)]
b = [(0,0.4),(3,20.2),(2,0.3)]


three(a, b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nervous/tuples.py", line 48, in three
    ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
StopIteration

Хотя с синтетическими все окей.

Исходная версия Nervous, :

Шота у меня не работает:

def three(a, b):
    c = list(a)
    ind1 = 0
    for elem in c:
        key1 = elem[0]
        ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
        if not(ind2 is None):
            c[ind1] = (key1, elem[1]+b[ind2][1])
        ind1 += 1

    return sorted(c, key=lambda x: x[1], reverse=True)


a = [(0,10.8),(1,8.2),(2,0.3)]
b = [(0,0.4),(3,20.2),(2,0.3)]


three(a, b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nervous/tuples.py", line 48, in three
    ind2 = next(i for i, e in enumerate(b) if e[0] == key1)
StopIteration