Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将多个列表合并为一个列表:numpy.mean()_Python_Numpy_Python Imaging Library_Scikit Image - Fatal编程技术网

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

如何在python中将多个列表合并为一个列表:numpy.mean(),python,numpy,python-imaging-library,scikit-image,Python,Numpy,Python Imaging Library,Scikit Image,我无法将列表合并到一个列表中 以下是我尝试过的 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 filena

我无法将列表合并到一个列表中

以下是我尝试过的

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)))
导入操作系统,全局
从PIL导入图像
从撇渣进口io
将numpy作为np导入
从统计数据导入stdev
path=“/Users/Xin/Desktop/SVM图像分类主控/测试”
#删除低像素值的图像
对于os.listdir(路径)中的文件名:
images=Image.open(os.path.join(路径,文件名))
值=[四舍五入(np.mean(images.tolist(),2)]
打印(值)
打印(类型(值))
#如果np.平均值(图像)<20:
#删除(os.path.join(路径,文件名))
#打印(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'>

[12.69]
[14.46]
[12.25]
[9.51]
[18.7]
[10.0]
[18.13]
[12.63]
我需要的是将上面的列表合并成一个列表,这样我就可以使用sum()函数得到一个总值

谁能帮我一个忙吗?
谢谢

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

所有_值=[]
对于os.listdir(路径)中的文件名:
images=Image.open(os.path.join(路径,文件名))
值=[四舍五入(np.mean(images.tolist(),2)]
所有_值=[*所有_值,*值]
打印(所有_值)
尝试以下方法

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)

您可以定义一个列表,并在其中不断添加值,然后打印/返回
sum(list)
可能重复的
value=round(np.mean(图像),2)
是否适合您?我认为您不需要
列表和外括号。如果您将列表添加到sum_list,那么将无法工作,这实际上会在
sum(sum_list)
期间导致错误。我唯一不明白的是,为什么在
value=[…]
中需要方括号;放下它,例如,
value=round(np.mean(图像),2)
生活就会变得更轻松。。。