列表的平均值,以 100 项为单位
The average value of a list, in chunks of 100 items
我有一个文本文件,其中包含例如 1084 个元素。我列出来。
import csv
a = []
with open('example.txt', 'r') as csvfile:
file_name = csv.reader(csvfile, delimiter='\t')
for row in file_name:
a.append(int(row[1]))
print(a)
[144, 67, 5, 23, 64...456, 78, 124]
接下来,我需要对列表的每一百个元素取平均值,对最后 84 个元素取平均值,并将其放入新列表。
我到底该怎么做?也许用 numpy?
这将在 Python 3 中工作,我假设您正在使用它,因为您将 print
编写为函数。在 Python 2 中,您需要确保除法是浮点除法。
# a = [...]
# Separate the groups. The last slice will be fine with less than 100 numbers.
groups = [a[x:x+100] for x in range(0, len(a), 100)]
# Simple math to calculate the means
means = [sum(group)/len(group) for group in groups]
如果你确实想在Python2中这样做,你可以将最后一行中的len(group)
替换为float(len(group))
,这样它会强制进行浮点除法;或者,您可以添加模块的 from __future__ import division
,尽管这样做会更改整个模块,而不仅仅是这一行。
这是一个很好的、可重复使用的版本,它与迭代器一起工作,它本身就是一个生成器:
from itertools import islice
def means_of_slices(iterable, slice_size):
iterator = iter(iterable)
while True:
slice = list(islice(iterator, slice_size))
if slice:
yield sum(slice)/len(slice)
else:
return
a = [1, 2, 3, 4, 5, 6]
means = list(means_of_slices(a, 2))
print(means)
# [1.5, 3.5, 5.5]
你不应该让代码比你实际需要的更通用,但为了奖励经验值,你可以让它更通用和可组合。以上内容应该足够你使用了。
您可以使用 senderle 的食谱 How do you split a list into evenly sized chunks? 在基础 Python 中执行此操作:
a = range(1,1084)
from itertools import islice
def chunk(it, size):
it = iter(it)
return iter(lambda: tuple(islice(it, size)), ())
means = [sum(chk)/len(chk) for chk in chunk(a, size=100)]
# [50.5, 150.5, 250.5, 350.5, 450.5, 550.5, 650.5, 750.5, 850.5, 950.5, 1042.0]
简单地说,切块并取平均值。
import math
averages = []
CHUNK_SIZE = 100
for i in range(0, math.ceil(len(a) / CHUNK_SIZE)):
start_index = i * CHUNK_SIZE
end_index = start_index + CHUNK_SIZE
chunk = a[start_index:end_index]
averages.append(sum(chunk)/len(chunk))
我有一个文本文件,其中包含例如 1084 个元素。我列出来。
import csv
a = []
with open('example.txt', 'r') as csvfile:
file_name = csv.reader(csvfile, delimiter='\t')
for row in file_name:
a.append(int(row[1]))
print(a)
[144, 67, 5, 23, 64...456, 78, 124]
接下来,我需要对列表的每一百个元素取平均值,对最后 84 个元素取平均值,并将其放入新列表。 我到底该怎么做?也许用 numpy?
这将在 Python 3 中工作,我假设您正在使用它,因为您将 print
编写为函数。在 Python 2 中,您需要确保除法是浮点除法。
# a = [...]
# Separate the groups. The last slice will be fine with less than 100 numbers.
groups = [a[x:x+100] for x in range(0, len(a), 100)]
# Simple math to calculate the means
means = [sum(group)/len(group) for group in groups]
如果你确实想在Python2中这样做,你可以将最后一行中的len(group)
替换为float(len(group))
,这样它会强制进行浮点除法;或者,您可以添加模块的 from __future__ import division
这是一个很好的、可重复使用的版本,它与迭代器一起工作,它本身就是一个生成器:
from itertools import islice
def means_of_slices(iterable, slice_size):
iterator = iter(iterable)
while True:
slice = list(islice(iterator, slice_size))
if slice:
yield sum(slice)/len(slice)
else:
return
a = [1, 2, 3, 4, 5, 6]
means = list(means_of_slices(a, 2))
print(means)
# [1.5, 3.5, 5.5]
你不应该让代码比你实际需要的更通用,但为了奖励经验值,你可以让它更通用和可组合。以上内容应该足够你使用了。
您可以使用 senderle 的食谱 How do you split a list into evenly sized chunks? 在基础 Python 中执行此操作:
a = range(1,1084)
from itertools import islice
def chunk(it, size):
it = iter(it)
return iter(lambda: tuple(islice(it, size)), ())
means = [sum(chk)/len(chk) for chk in chunk(a, size=100)]
# [50.5, 150.5, 250.5, 350.5, 450.5, 550.5, 650.5, 750.5, 850.5, 950.5, 1042.0]
简单地说,切块并取平均值。
import math
averages = []
CHUNK_SIZE = 100
for i in range(0, math.ceil(len(a) / CHUNK_SIZE)):
start_index = i * CHUNK_SIZE
end_index = start_index + CHUNK_SIZE
chunk = a[start_index:end_index]
averages.append(sum(chunk)/len(chunk))