如何将函数映射到三重嵌套列表并保持三重嵌套列表完整?

发布于 2024-12-02 15:40:57 字数 581 浏览 0 评论 0原文

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

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼前雾蒙蒙 2024-12-09 15:40:57

与其原地踏步,不如制定一个新清单

 dataset = [[[float(value) for value in measure] 
                           for measure in subject] 
                           for subject in dataset] 

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] 
东风软 2024-12-09 15:40:57

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!)

会傲 2024-12-09 15:40:57

就地执行此操作的直接方法是:

for subject in dataset:
    for measure in subject:
        for i, elem in enumerate(measure):
            measure[i] = float(elem)

或者,使用切片运算符使用 map 的结果就地更新列表

for subject in dataset:
    for measure in subject:
        measure[:] = map(float, measure)

A straight-forward way to do that in place would be:

for subject in dataset:
    for measure in subject:
        for i, elem in enumerate(measure):
            measure[i] = float(elem)

Alternatively, use the slice operator to upate the list in-place with the results of map

for subject in dataset:
    for measure in subject:
        measure[:] = map(float, measure)
℉服软 2024-12-09 15:40:57

这应该可以完成工作

for subject in dataset:
    for measure in subject:
        for i, m in enumerate(measure):
            measure[i] = float(m)

This should do the job

for subject in dataset:
    for measure in subject:
        for i, m in enumerate(measure):
            measure[i] = float(m)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文