I have two lists, say
a = [10, 20, 30 , 40, 50 , 60]
b = [30, 70, 110]
正如你所看到的,列表 b 由列表 a 相邻两个元素之和构成:
b[0] = a[0] + a[1] = 10 + 20 = 30 etc.
我该如何获得另一个列表,其中包含给定窗口的列表a元素和b元素的分数?在我的例子中,我想要获得以下列表:
c = [10/30, 20/30, 30/70, 40/70, 50/110, 60/110]
b
和 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]
如果您确实需要使用分数,则可以使用fractions
模块及其Fraction
数据类型:
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)]
正如@LaurentH.在评论中指出的那样,上述方法仅适用于大小为2的块(您称之为窗口)。对于更一般化的方法,您可以定义一个生成器来yield
它们:
# taken from https://dev59.com/lnVC5IYBdhLWcg3wYQAp#312464
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]
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']
val*1.0
将val
转换为float
,只需使用float(var)
即可;这更加明确。
2)请注意,您的代码是Python 2.x版本。
3)根据他的问题格式,我认为OP并没有要求一个基于字符串的列表。 - Ma0# 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))
这里还有一种单行风格的版本,使用列表推导式,可读性稍差,但非常简短:
# 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))