Multiply a list of numbers by a range of numbers

I am trying to write code for module 11, but I cannot make it more like python.

I am currently using weight and increasing it, checking when it reaches a number, and then setting it to its original value.

Say I have a list of numbers 1..20, and I would like to multiply them by 2,3,4,5,6,7,8,9,2,3,4,etcso that each index is multiplied by an increasing number.

1x2, 2x3, 3x4, 4x5, 5x6, 6x7, 7x8, 8x9, 9x2, 10x3, etc.

Is there an elegant way to do this?

Inconvenient way:

def mod11(list, max_weight=9):
    sum = 0
    weight = 2

    for item in reversed(list):
        sum += item * weight
        weight += 1

        if weight > max_weight:
            weight = 2

    mod = 11 - sum % 11

    if mod > 9:
        return 0

    else:
        return mod
+4
source share
6 answers

You can use the current index to determine the multiplier:

>>> [(index % 8 + 2) * item for index, item in enumerate(range(1, 21))]
[2, 6, 12, 20, 30, 42, 56, 72, 18, 30, 44, 60, 78, 98, 120, 144, 34, 54, 76, 100]
#^        resets               ^            resets               ^

"" zip :

>>> from itertools import cycle
>>> [x * y for x, y in zip(range(1, 21), cycle(range(2, 10)))]
[2, 6, 12, 20, 30, 42, 56, 72, 18, 30, 44, 60, 78, 98, 120, 144, 34, 54, 76, 100]
+2

, , , 11, , , 2 9 .

1..20 , :

list(range(1,21))    (This is if you want to include the 20)

, 2 9, :

x -> x % 8 + 2

, :

[((x-1) % 8 + 2) * x for x in xrange(1,21)]

( -1 2 3)

+1

, itertools.cycle:

>>> itertools.cycle(range(2, 10))
2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, ...

, :

from itertools import cycle

def mod11(lst, max_weight=9):
    multipliers = cycle(range(2, max_weight + 1))
    zipped = zip(lst, multipliers))
    summed = sum(a * b for a, b in zipped)
    mod = 11 - (summed % 11)
    return (0 if mod > 9 else mod)
+1

, numpy ( ), :

In [1]: import numpy as np

In [2]: a = np.arange(1, 21)  # like range in numpy array

In [3]: a - 1  # calculation performs per element wise... so will a * n
Out[3]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])


In [4]: a * ((a - 1) % 8 + 2)  # directly translate the calculation
Out[4]:
array([  2,   6,  12,  20,  30,  42,  56,  72,  18,  30,  44,  60,  78,
        98, 120, 144,  34,  54,  76, 100])
+1

starmap cycle itertools.

, :

>>> from itertools import starmap, cycle
>>> li=zip(range(1,21), cycle(range(2,10)))
>>> li
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6), (14, 7), (15, 8), (16, 9), (17, 2), (18, 3), (19, 4), (20, 5)]

starmap mul ( ) :

>>> from operator import mul 
>>> list(starmap(mul, li))
[2, 6, 12, 20, 30, 42, 56, 72, 18, 30, 44, 60, 78, 98, 120, 144, 34, 54, 76, 100]

, :

>>> [x*y for x, y in ((e+1, (e%8)+2) for e in range(20))]
[2, 6, 12, 20, 30, 42, 56, 72, 18, 30, 44, 60, 78, 98, 120, 144, 34, 54, 76, 100]
+1

, :

s = map(lambda x : x*(x+1),list(range(1,20)))

map - , . - , . , ,

0

Source: https://habr.com/ru/post/1653713/


All Articles