Separating items from one list with items from another

I have two lists, let's say

a = [10, 20, 30 , 40, 50 , 60] 
b = [30, 70, 110]

As you can see, list b consists of a list with the sum of elements with window = 2:

b[0] = a[0] + a[1] = 10 + 20 = 30 etc.

How can I get another list that will consist of fractions of a list of elements and b elements with a given window? In my example, I want to get a list:

c = [10/30, 20/30, 30/70, 40/70, 50/110, 60/110]
+4
source share
3 answers

You can use lists for both tasks (creating band lists c)

a = [10, 20, 30, 40, 50, 60]

b = [i+j for i, j in zip(a[::2], a[1::2])]
print(b)  # [30, 70, 110]

c = [x / b[i//2] for i, x in enumerate(a)]
print(c)  # [0.3333333333333333, 0.6666666666666666, 0.42857142857142855, 0.5714285714285714, 0.45454545454545453, 0.5454545454545454]

If you really need fractions , you can use fractionsits data as well FractionType:

from fractions import Fraction

# same code as before

c = [Fraction(x, b[i//2]) for i, x in enumerate(a)]
print(c)  # [Fraction(1, 3), Fraction(2, 3), Fraction(3, 7), Fraction(4, 7), Fraction(5, 11), Fraction(6, 11)]

Note

@LaurentH. , ( ) 2. , yield :

# taken from https://stackoverflow.com/a/312464/6162307
def yield_chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]    

b = [sum(chunk) for chunk in yield_chunks(a, 2)]
# same code

n = 3:

n = 3
b = [sum(chunk) for chunk in yield_chunks(a, n)]
print(b)  # [60, 150]
c = [x / b[i//n] for i, x in enumerate(a)]
print(c)  # [0.16666666666666666, 0.3333333333333333, 0.5, 0.26666666666666666, 0.3333333333333333, 0.4]
+4
a = [10, 20, 30 , 40, 50 , 60] 
b = [30, 70, 110]

# take each value in a (multiply by 1.0 to get a double), and divide by the value in b in the corresponding index (i.e. indices 0,1 in a correspond with index 0 in b, and so on...)
c = [val*1.0/b[idx//2] for idx,val in enumerate(a)]

# here is the calculation using strings, to get the desired output by OP
d = ['{}/{}'.format(val, b[idx//2]) for idx,val in enumerate(a)]

print '{}\n{}'.format(c, d)

=

[0.3333333333333333, 0.6666666666666666, 0.42857142857142855, 0.5714285714285714, 0.45454545454545453, 0.5454545454545454]

['10/30', '20/30', '30/70', '40/70', '50/110', '60/110']
+1

Here is my suggestion with one function for the sum and one function for the fraction, given the width of the list and window. It does not use an external package and is rather short, easy to read and understand:

# Sum with a given window
def sumWithWindow(aList, window = 2):
    res = []
    mySum = 0
    for i,elem in enumerate(aList):
        mySum += elem
        if (i+1) % window == 0:
            res.append(mySum)
            mySum = 0
    return (res)


# Fraction with a given window
def fractionWithWindow(aList, window = 2):
    res = []
    b = sumWithWindow(aList, window)
    for i,elem in enumerate(aList):
        res.append(elem/b[int(i/window)])
    return (res)

# Example
a = [10, 20, 30 , 40, 50 , 60]
print(sumWithWindow(a, 2))
print(fractionWithWindow(a,2))

A single-layer version with a list is also available here, a little less clear, but very short:

# Sum with a given window
def sumWithWindow(aList, window = 2):
    return [sum(aList[n:n+window]) for n in range(0,len(aList),window)]

# Fraction with a given window
def fractionWithWindow(aList, window = 2):
    return [elem/sumWithWindow(aList,window)[i//window] for i,elem in enumerate(aList)]

# Example
a = [10, 20, 30 , 40, 50 , 60]
print(sumWithWindow(a, 2))
print(fractionWithWindow(a,2))
0
source

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


All Articles