我在将列表合并到列表中遇到问题
以下是我尝试过的
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()函数以获取总值
有人可以帮我吗? 谢谢
答案 0 :(得分: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)
答案 1 :(得分: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)