Pythonic方法迭代字典中的列表并行作为一个字典(Pythonic way to iterate over lists inside a dict in parallel AS a dict)
我意识到这个问题的标题可能不会立即超级明确,所以在一个例子中:
我的dict包含如下列表(假设其中没有其他内容):packed = { 'names': ['a', 'b', 'c', 'd'], 'values': ['A', 'B', 'C', 'D'] }
我想要做的是并行迭代每个列表(尽可能多的列表),但是在每次迭代中我都有一个带有每个列表的单个元素的dict 。 我想象这样的事情:
for pack in smartIterate(packed): #how to "smartIterate"? print(pack['names'], pack['values']) #pack is equal to {'names':'a', 'values':'A'} in the first iteration
打印出来:
a, A b, B c, C d, D
我意识到这可以通过显式迭代一个子列表的长度并在每次迭代期间构造一个dict来完成:
for i in range(len(packed.values()[0])): pack = dict(zip(packed.keys(),[v[i] for v in packed.values()]))
但感觉有一种更清洁,更有效 - pythonic - 的方式来做到这一点。
类似的问题:
I realize that the title of this question might not be immediately super clear, so on an example:
My dict contains lists as follows (assume there's nothing else in it):packed = { 'names': ['a', 'b', 'c', 'd'], 'values': ['A', 'B', 'C', 'D'] }
What I want to do is to iterate over each of these lists (as many as there might be) in parallel, but so that during each iteration I have a dict with a single element of each list. I imagine something like this:
for pack in smartIterate(packed): #how to "smartIterate"? print(pack['names'], pack['values']) #pack is equal to {'names':'a', 'values':'A'} in the first iteration
printing out that:
a, A b, B c, C d, D
I realize this can be done with explicitly iterating over the length of one sublist and constructing a dict during each iteration:
for i in range(len(packed.values()[0])): pack = dict(zip(packed.keys(),[v[i] for v in packed.values()]))
but it feels like there is a cleaner, more efficient - pythonic - way to do that.
Similar questions:
原文:https://stackoverflow.com/questions/50874572
最满意答案
命名变量
uploadBlob
是个误导,因为它不再是一个blob。 这是一个网址,你不会发送到服务器。基本上,将Blob /文件追加到FormData中 ,然后在你的ajax请求中发送formdata
const fd = new FormData() fd.append('avatar', this.files[0]) // fd.append('avatar', this.files[0], optionalFileName) fetch(uploadUrl, {method: 'post', body: fd}) /* or using XMLHttpRequest: var xhr = new XMLHttpRequest xhr.open('POST', uploadURL) xhr.send(fd) */
It's misleading to name the variable
uploadBlob
since it's not a blob any more. it's a url, you don't send that to the server.Basically, append the blob/file to a FormData, then send the formdata in your ajax request
const fd = new FormData() fd.append('avatar', this.files[0]) // fd.append('avatar', this.files[0], optionalFileName) fetch(uploadUrl, {method: 'post', body: fd}) /* or using XMLHttpRequest: var xhr = new XMLHttpRequest xhr.open('POST', uploadURL) xhr.send(fd) */
相关问答
更多-
你正试图使用multer的旧API。 它改变了一段时间,请参阅文档以获取更多信息。 在您上传单个文件的特定情况下,您将移除app.use(multer({ ... }))行,而是使用.single()和req.file如下所示: var upload = multer({ dest: '/tmp/'}); // File input field name is simply 'file' app.post('/file_upload', upload.single('file'), function(re ...
-
异步图像上传模式(Async image upload pattern)[2022-11-06]
使用FileReader API,您实际上可以从磁盘读取二进制blob,然后在提交表单之前将该图像纯粹用于客户端! 见http://www.onlywebpro.com/2012/01/24/create-thumbnail-preview-of-images-using-html5-api/ Using the FileReader API, you can actually read the binary blob from disk and then use that image purely cli ... -
uploadImagesApi(images) { let formData = new FormData() for (let i = 0; i < images.length; i++) { formData.append('images', images[i]); } return http.fetch(url + "/", { method: 'put', ...
-
我没有验证这一点,但你可以这样做: var fs = require('fs'), request = require('request'), url = 'http://admin:password@127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg?rev=2-2739352689'; var requestStream = request.put(url); requestStream.on('response ...
-
如果要将图像直接存储到数据库中,则必须使用fs.readFileSync以二进制形式获取图像,然后保存它,请注意, req.file.path返回上载文件的完整路径: var user = req.body; user.avatar.data = fs.readFileSync(req.file.path); user.avatar.contentType = req.file.mimetype; User.addUser(user, (err, user) => { ... }); 请记住,将 ...
-
命名变量uploadBlob是个误导,因为它不再是一个blob。 这是一个网址,你不会发送到服务器。 基本上,将Blob /文件追加到FormData中 ,然后在你的ajax请求中发送formdata const fd = new FormData() fd.append('avatar', this.files[0]) // fd.append('avatar', this.files[0], optionalFileName) fetch(uploadUrl, {method: 'post', bod ...
-
您应该查看多部分中间件文档 ,这是涉及文件上传的文档。 它表示,限制是通过“限制”选项设置的,如果您将“延迟”选项设置为true,则可以听取进度。 在这种情况下,上传使用的表单被设置为您的请求的属性。 然后,您将能够听取进度事件 。 所以你的代码应该看起来像这样(尚未测试): app.configure(function () { app.use(express.static(__dirname + "/media")); app.use(express.bodyParser({ ...
-
Hoping this might help someone 在安德鲁的帮助下,我解决了这个问题 我只是将代码行改为 : app.use(express.static(__dirname+'/public/images')); Hoping this might help someone With the help of Andrew in one of the answers i resolved this I just changed the line of code to below:: app.u ...
-
当您上传文件(使用multer.single方法)时,您将获得文件数据 req.file 它是属性originalname,mimetype,path等中的对象。 检查所有文档: https : //github.com/expressjs/multer 但不要相信mimetype。 如何保留图像名称和扩展名? a)使用req.file中的数据重命名上传的文件(不喜欢它) b)在db中存储文件数据(req.file) 编辑有关重命名:当所有下载的文件转到一个目录,并且您将名称更改为原始名称时,可能存在冲突 ...
-
需要考虑的是,上传的接收数据具有以下格式: ------WebKitFormBoundary9BhXe3lt2UddCDz9 Content-Disposition: form-data; name="document"; filename="globeSS.jpg" Content-Type: image/jpeg ÿØÿà JFIF d d ÿì Ducky d ÿá //[binary binary binary...] Ï[leñnœ“}ÛOyŠVÑ0êãXÂ}Ö'±”É iÉö ...