如何将函数映射到三重嵌套列表并保持三重嵌套列表完好无损? [英] How to map a function to a triple nested list and keep the triple nested list intact?

查看:27
本文介绍了如何将函数映射到三重嵌套列表并保持三重嵌套列表完好无损?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的博士构建一个分析工作流,并一直使用三重嵌套列表来表示我的数据结构,因为我希望它能够在第二和第三级扩展到任意数量的数据.第一级是整个数据集,第二级是数据集中的每个主题,第三级是每个主题的每个度量的行.

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] 

这篇关于如何将函数映射到三重嵌套列表并保持三重嵌套列表完好无损?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆