如何在python中将多个列表合并到一个列表中:numpy.mean()

问题描述 投票:1回答:2

我将列表合并到列表中时遇到问题

以下是我的尝试

import os,glob
from PIL import Image
from skimage import io
import numpy as np
from statistics import stdev 

path = "/Users/Xin/Desktop/SVM-Image-Classification-master/test"
# Delete images with the low pixel value
for filename in os.listdir(path):
    images = Image.open(os.path.join(path,filename))  
    value = [round(np.mean(images).tolist(),2)]
    print(value)
    print(type(value))
    #if np.mean(images) < 20:
        #os.remove(os.path.join(path, filename))
#print(len(os.listdir(path)))

输出如下

[12.69]
<class 'list'>
[14.46]
<class 'list'>
[12.25]
<class 'list'>
[9.51]
<class 'list'>
[18.7]
<class 'list'>
[10.0]
<class 'list'>
[18.13]
<class 'list'>
[12.63]
<class 'list'>

我需要的是将上面的列表合并到一个列表中,以便我可以使用sum()函数来获得总值

谁能给我一个帮助?谢谢

python numpy python-imaging-library scikit-image
2个回答
1
投票

尝试以下方式

from numpy import array
from numpy import sum
sum_list = []
for filename in os.listdir(path):
    images = Image.open(os.path.join(path,filename))  
    value = [round(np.mean(images).tolist(),2)]
    sum_list.append(value)
v = array(sum_list)
return sum(v)

0
投票

创建一个列表,该列表将存储所有值,然后附加到它:

all_values = []
for filename in os.listdir(path):
    images = Image.open(os.path.join(path,filename))  
    value = [round(np.mean(images).tolist(),2)]
    all_values = [*all_values, *value]

print(all_values)
© www.soinside.com 2019 - 2024. All rights reserved.