我有两个列表,比如说
a = [10, 20, 30 , 40, 50 , 60]
b = [30, 70, 110]
如您所见,列表b由一个列表的元素和组成,其中window = 2:
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]
发布于 2018-02-26 13:58:34
您可以对这两个任务使用列表理解(创建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://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]
发布于 2018-02-26 13:58:18
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']
发布于 2018-02-26 14:21:42
这是我的建议,一个函数用于求和,一个函数用于分数,列表和窗口宽度作为参数。它不使用外部包,并且非常简短,易于阅读和理解:
# 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))
https://stackoverflow.com/questions/48990152
复制