我想知道是否有一条快捷方式可以在Python列表中列出一个简单的列表.
我可以在for循环中做到这一点,但也许有一些很酷的"单行"?我用reduce尝试了,但是我收到了一个错误.
码
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
reduce(lambda x, y: x.extend(y), l)
Run Code Online (Sandbox Code Playgroud)
错误信息
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
AttributeError: 'NoneType' object has no attribute 'extend'
Run Code Online (Sandbox Code Playgroud) 我有这个代码
from opensky_api import OpenSkyApi
api = OpenSkyApi()
states = api.get_states(bbox=(51.3500, 51.5900, -0.6342, -0.2742))
for s in states.states:
lat = s.latitude
print(lat)
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样
51.4775
51.4589
51.4774
51.4774
Run Code Online (Sandbox Code Playgroud)
如何使输出看起来像这样?
[51.4775, 51.4589, 51.4774, 51.4774]
Run Code Online (Sandbox Code Playgroud) 我需要访问文件夹中的所有图像并将其存储在矩阵中.我能够使用matlab完成它,这里是代码:
input_dir = 'C:\Users\Karim\Downloads\att_faces\New Folder';
image_dims = [112, 92];
filenames = dir(fullfile(input_dir, '*.pgm'));
num_images = numel(filenames);
images = [];
for n = 1:num_images
filename = fullfile(input_dir, filenames(n).name);
img = imread(filename);
img = imresize(img,image_dims);
end
Run Code Online (Sandbox Code Playgroud)
但我需要使用python来完成它,这是我的python代码:
import Image
import os
from PIL import Image
from numpy import *
import numpy as np
#import images
dirname = "C:\\Users\\Karim\\Downloads\\att_faces\\New folder"
#get number of images and dimentions
path, dirs, files = os.walk(dirname).next()
num_images = len(files)
image_file = "C:\\Users\\Karim\\Downloads\\att_faces\\New folder\\2.pgm"
im = Image.open(image_file)
width, height …
Run Code Online (Sandbox Code Playgroud)