如何将函数映射到三重嵌套列表并保持三重嵌套列表完整?
我一直在为我的博士学位构建一个分析工作流程,并一直使用三重嵌套列表来表示我的数据结构,因为我希望它能够在第二层和第三层扩展到任意数量的数据。第一级是整个数据集,第二级是数据集中的每个主题,第三级是每个主题的每个度量的一行。
[dataset]
|
[subject]
|
[measure1, measure2, measure3]
我正在尝试将函数映射到每个度量 - 例如将所有点转换为浮点数或将异常值替换为 None - 并希望根据其嵌套返回整个数据集,但我当前的代码:
for subject in dataset:
for measure in subject:
map(float, measure)
...结果是正确的并且正是我想要的,但问题是我无法想象如何有效地将结果分配回数据集或在不丢失嵌套级别的情况下。理想情况下,我希望它能够*就地更改测量值,但我不知道该怎么做。
你能建议一种高效且Python式的方法吗?三重嵌套列表是在程序中组织数据的愚蠢方法吗?
I've have been building an analysis workflow for my PhD and have been using a triple nested list to represent my data structure because I want it to be able to expand to an arbitrary amount of data in its second and third levels. The first level is the whole dataset, the second level is each subject in the dataset and third level is a row for each measure that each subject.
[dataset]
|
[subject]
|
[measure1, measure2, measure3]
I am trying to map a function to each measure - for instance convert all the points into floats or replace anomalous values with None - and wish to return the whole dataset according to its nesting but my current code:
for subject in dataset:
for measure in subject:
map(float, measure)
...the result is correct and exactly what I want but the problem is that I can't think how to assign the result back to the dataset efficiently or without losing a level of the nest. Ideally, I would like it to change the measure *in place but I can't think how to do it.
Could you suggest an efficient and pythonic way of doing that? Is a triple nested list a silly way to organize my data in the program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与其原地踏步,不如制定一个新清单
Rather than doing it in place, make a new list
return [[map(float,measure) formeasure in subject] for subject in dataset]
您可以返回一个列表,而不是就地更改它——这仍然非常高效,并且保留了您想要的所有信息。想。 (旁白:事实上,它通常比分配给列表索引[需要引用]更快,这是其他人在这里建议的!)
return [[map(float, measure) for measure in subject] for subject in dataset]
You can return a list instead of altering it in place -- this is still remarkably efficient and preserves all the information you want. (aside: In fact, it's often faster than assigning to list indexes [citation needed], which is what others have suggested here!)
就地执行此操作的直接方法是:
或者,使用切片运算符使用
map
的结果就地更新列表A straight-forward way to do that in place would be:
Alternatively, use the slice operator to upate the list in-place with the results of
map
这应该可以完成工作
This should do the job