简体   繁体   English

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

[英]How to merge multiple lists into a list in python: numpy.mean()

I have problems with merging lists into a list我在将列表合并为列表时遇到问题

The followings are what I have tried以下是我尝试过的

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)))

The output as follows输出如下

[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'>

What I need is merging the above lists into a list so that I can do sum() function to get a total value我需要的是将上述列表合并到一个列表中,以便我可以执行 sum() 函数来获得总值

Can anyone give me a help?任何人都可以帮我吗? Thanks谢谢

Try following way尝试以下方式

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)

Create a list that will store all of the values, then append to it:创建一个将存储所有值的列表,然后附加到它:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM