How to map a function to a triple nested list and keep the triple nested list intact?(如何将函数映射到三重嵌套列表并保持三重嵌套列表完好无损?)
问题描述
我一直在为我的博士构建一个分析工作流,并一直使用三重嵌套列表来表示我的数据结构,因为我希望它能够在第二和第三级扩展到任意数量的数据.第一级是整个数据集,第二级是数据集中的每个主题,第三级是每个主题的每个度量的行.
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]
我正在尝试将函数映射到每个度量 - 例如将所有点转换为浮点数或用 None 替换异常值 - 并希望根据其嵌套返回整个数据集,但我当前的代码:
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.
您能否提出一种高效且 Pythonic 的方法来做到这一点?三重嵌套列表是在程序中组织数据的愚蠢方式吗?
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?
推荐答案
与其就地做,不如新建一个列表
Rather than doing it in place, make a new list
dataset = [[[float(value) for value in measure]
for measure in subject]
for subject in dataset]
这篇关于如何将函数映射到三重嵌套列表并保持三重嵌套列表完好无损?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将函数映射到三重嵌套列表并保持三重嵌套


基础教程推荐
- 求两个直方图的卷积 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01