一些i / o问题(Some i/o problems)
免责声明:这是一项任务。 如果你觉得我只是“请你为我做功课”让我知道,我会问一个更广泛的问题,或者只是给你一些提示,如果你能取悦。
好的,我有两套100个文件。 第一组称为cell_spks_n,其中n = 1,...,100,第二组称为cell_dirs_n,其中n = 1,...,100。 numpy的loadtxt将这些文件加载到5x8数组中,这是完美的。 我想加载这些并为它们做一些事情。 现在我的问题是命名所有这些文件 。 我想到制作两个名为dirs和spks的列表,并按顺序将数组存储在其中。 然而出了问题,它只附加了一个 numpy加载的元素,我不知道出了什么问题。
from numpy import * files = 100 for i in range(1, files+1): dirs = [] spks = [] if (0<i<9): dirs_name = 'neurondata/cell_dirs_00' + str(i) + '.txt' spks_name = 'neurondata/cell_spks_00' + str(i) + '.txt' dirs.append(loadtxt(dirs_name)) spks.append(loadtxt(spks_name)) elif (9<i<=99): dirs_name = 'neurondata/cell_dirs_0' + str(i) + '.txt' spks_name = 'neurondata/cell_spks_0' + str(i) + '.txt' dirs.append(loadtxt(dirs_name)) spks.append(loadtxt(spks_name)) else: dirs.append(loadtxt('neurondata/cell_dirs_100.txt')) spks.append(loadtxt('neurondata/cell_spks_100.txt')) # Fancy stuff gets done here
我认为将这些作为数组加载甚至可能是一个坏主意,我将不得不考虑我的索引来访问数据。 理想的情况是有某种循环,如下所示:
for i in range(1,files+1): spk_i = loadtxt('cell_spks_i') dir_i = loadtxt('cell_dirs_i')
思考?
编辑:我忘记了一些输出
如果我说
for item in spks: print item print shape(spks)
我得到了输出
[[ 25.287356 23.655914 22.988506 14.285714 2.3809524 4.3478261 19.354839 11.764706 ] [ 16.129032 26.666667 19.565217 7.2289157 5.8823529 13.861386 7.0588235 12.195122 ] [ 13.157895 16.86747 26.190476 29.62963 12.121212 12.307692 27.5 19.047619 ] [ 18.518519 25.396825 34.482759 14.814815 20.224719 9.4117647 6.6666667 21.686747 ] [ 32.55814 22.988506 26.506024 21.782178 13.114754 2.7777778 14.814815 8.6021505]] (1, 5, 8)
Disclaimer: This is for an assignment. If you feel I'm just "asking you to do my homework for me" let me know and I'll ask a more broad question, or just give me hints if you can please.
Ok so I've got two sets of 100 files. The first set is called cell_spks_n where n=1,...,100 and the second set is called cell_dirs_n, where n=1,...,100. numpy's loadtxt loads these files in a 5x8 array which is perfect. I want to load these all up and do some stuff to them. Now my issue is naming all these files. I thought of making two lists called dirs and spks, and storing the arrays in them sequentially. However something goes wrong and it only appends one element that numpy loads and I'm not sure what's going wrong.
from numpy import * files = 100 for i in range(1, files+1): dirs = [] spks = [] if (0<i<9): dirs_name = 'neurondata/cell_dirs_00' + str(i) + '.txt' spks_name = 'neurondata/cell_spks_00' + str(i) + '.txt' dirs.append(loadtxt(dirs_name)) spks.append(loadtxt(spks_name)) elif (9<i<=99): dirs_name = 'neurondata/cell_dirs_0' + str(i) + '.txt' spks_name = 'neurondata/cell_spks_0' + str(i) + '.txt' dirs.append(loadtxt(dirs_name)) spks.append(loadtxt(spks_name)) else: dirs.append(loadtxt('neurondata/cell_dirs_100.txt')) spks.append(loadtxt('neurondata/cell_spks_100.txt')) # Fancy stuff gets done here
I think it might even be a bad idea loading these as arrays which I'll have to mind my indexing to access the data. The ideal case would be to have some kind of loop that goes something like this:
for i in range(1,files+1): spk_i = loadtxt('cell_spks_i') dir_i = loadtxt('cell_dirs_i')
Thoughts?
Edit: I forgot to some output
If I say
for item in spks: print item print shape(spks)
I get as output
[[ 25.287356 23.655914 22.988506 14.285714 2.3809524 4.3478261 19.354839 11.764706 ] [ 16.129032 26.666667 19.565217 7.2289157 5.8823529 13.861386 7.0588235 12.195122 ] [ 13.157895 16.86747 26.190476 29.62963 12.121212 12.307692 27.5 19.047619 ] [ 18.518519 25.396825 34.482759 14.814815 20.224719 9.4117647 6.6666667 21.686747 ] [ 32.55814 22.988506 26.506024 21.782178 13.114754 2.7777778 14.814815 8.6021505]] (1, 5, 8)
原文:https://stackoverflow.com/questions/20052527
最满意答案
最简单的方法。
import { Http } from '@angular/http'; export class CustomComponent implements OnInit { length: number; constructor(private http: Http){ this.http.request('www.url...',{method:'GET'}).map(response => response.json()).subscribe(result=>{ this.length=result.length; }); } }
Easiest method.
import { Http } from '@angular/http'; export class CustomComponent implements OnInit { length: number; constructor(private http: Http){ this.http.request('www.url...',{method:'GET'}).map(response => response.json()).subscribe(result=>{ this.length=result.length; }); } }
相关问答
更多-
这是console.log如何在某些浏览器console.log工作的问题。 您可以查看使用console.log(JSON.stringify(response.key5))来获取时间点视图。 基本上, console.log记录了最高级别的东西,但是如果稍后再扩展其中一个,则会显示扩展它时的内容,而不是您记录时的内容。 所以, 当你在控制台中展开它之前, response.key5是空的,但是在事件被添加到它之前,它是空的。 这种行为相当松动。 例如,在Chrome上,当console.log发生时您是 ...
-
如何通过IConfiguration了解JSON数组的长度?(How to know the length of JSON array via IConfiguration?)[2022-11-29]
您可以创建一个与您的结构匹配的类,如: public class User { public string UserName { get; set; } public string Email { get; set; } public string Password { get; set; } public TodoItem[] Todos { get; set; } } public class TodoItem { public string Todo { ge ... -
假设您不希望更改数据结构,可以迭代数组,然后迭代其中每个对象的属性(然后访问名称): var champions = [{ "1":{ "name":"Aatrox", "role1":"Top", "role2":"Jungle"}, "2":{ "name":"Ahri", "role1":"Middle"}, "3":{ "name":"Akali", "role1":"Middle", "role2":"Top" ...
-
找到json字符串的长度(Find length of json string)[2023-03-07]
让我们从json字符串开始: var jsonString = '{"name":"John"}'; 你可以很容易地确定它的长度: alert("The string has "+jsonString.length+" characters"); // will alert 15 然后将其解析为一个对象: var jsonObject = JSON.parse(jsonString); JavaScript Object 不是 Array ,没有长度。 如果您想知道它有多少属性,您需要计算它们: va ... -
计算JSON数组的长度并访问PHP中的json元素(Count the length of JSON array and access the json element in PHP)[2023-01-14]
你尝试过不同的网址/文件吗? 我准确地修改了你的代码,它100%工作。 确保php://input正确。 尝试将JSON存储在外部文件中并尝试链接到该文件。 编辑 尝试这个: $json = file_get_contents('something.json'); $arr = json_decode($json, true); $count = 0; for($i = 0; $i < count($arr['data']); $i++) { $count++; } echo $count; 在那 ... -
最简单的方法。 import { Http } from '@angular/http'; export class CustomComponent implements OnInit { length: number; constructor(private http: Http){ this.http.request('www.url...',{method:'GET'}).map(response => response.json()).subscribe(result=>{ this.le ...
-
您可以对someObjects使用dynamic 。 代码可能如下所示: using System; using System.Collections.Generic; using Newtonsoft.Json; public class RootObject { public string chatName { get; set; } public List
users { get; set; } public bool someBooleanValue { g ... -
这与Object.keys(obj).length相同 ,但没有浏览器兼容性问题(我认为)。 关于什么: var obj = { yo: { a: 'foo', b: 'bar' } hi: { c: 'hello', d: 'world', e: 'json' } } var arr = [], len; for(key in obj) { arr.push(key); } l ...
-
验证JSON数组长度(Validate JSON array length)[2022-06-08]
你的JSON响应实际上是一个对象,而不是一个数组。 您正在检查的数组是该对象中名为items的属性。 因此,无论您使用的是getDept ,您都应该使用getDept.items : $.getJSON("https://apex.oracle.com/pls/apex/mfajertest1/department/"+$x('P2_DEPT_NO').value, function(getDept) { console.log(getDept.items); if(getDept.ite ... -
在Hugo模板中,如何检查JSON文件数组的长度?(In Hugo templates, how do you check length of JSON file array?)[2023-04-30]
您可以使用eq函数来比较长度: {{ if (eq ($json | len) 0) }} no data {{ else }} show posts {{ end }} You can use the eq function to compare the length: {{ if (eq ($json | len) 0) }} no data {{ else }} show posts {{ end }}