时变数据:元组列表与二维数组?

发布于 2024-10-31 17:55:05 字数 305 浏览 7 评论 0 原文

我的示例代码是用Python编写的,但我问的是一般原理。

如果我有一组时间-值对数据,我应该将它们存储为二维数组还是元组列表?例如,如果我有这个数据:

v=[1,4,4,4,23,4]
t=[1,2,3,4,5,6]

通常像这样存储它更好:

data=[v,t]

或者作为元组列表:

data=[(1,1),(4,2)(4,3)...]

是否有一个“标准”的方法来做到这一点?

My example code is in python but I'm asking about the general principle.

If I have a set of data in time-value pairs, should I store these as a 2D array or as a list of tuples? for instance, if I have this data:

v=[1,4,4,4,23,4]
t=[1,2,3,4,5,6]

Is it generally better to store it like this:

data=[v,t]

or as a list of tuples:

data=[(1,1),(4,2)(4,3)...]

Is there a "standard" way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

夜还是长夜 2024-11-07 17:55:05

如果速度是您最关心的问题,那么在 Python 中,请查看 Numpy。

一般来说,您应该选择一种使数据处理自然且容易的数据结构。在您知道它有效之后,再担心速度!

至于简单的数据结构,元组列表怎么样:

v=[1,4,4,4,23,4]
t=[1,2,3,4,5,6]

data=[(1,1),(4,2)(4,3)...]

然后你可以像这样解压:

v,t=data[1]
#v,t are 4,2

If speed is your biggest concern, in Python, look at Numpy.

In general, you should choose choose a data structure that makes dealing with the data natural and easy. Worry about speed later, after you know it works!

As for an easy data structure, how about an list of tuples:

v=[1,4,4,4,23,4]
t=[1,2,3,4,5,6]

data=[(1,1),(4,2)(4,3)...]

Then you can unpack like so:

v,t=data[1]
#v,t are 4,2
臻嫒无言 2024-11-07 17:55:05

聚合数组容器可能是最好的选择。假设您的时间点不是规则间隔的(因此您需要跟踪它而不仅仅是使用索引),这允许您对整个数据集进行切片,例如:

import numpy as np
v=[1,4,4,4,23,4]
t=[1,2,3,4,5,6]

data = np.array([v,t])

然后您可以对其进行切片以获取子集轻松获取数据:

data[:,2:4]  #array([[4, 4],[3, 4]])

ii = [1,2,5] # Fancy indexing
data[:,ii] # array([[4, 4, 4],
           #        [2, 3, 6]])

The aggregate array container is probably the best choice. Assuming that your time points are not regularly spaced (and therefore you need to keep track of it rather than just use the indexing), this allows you to take slices of your entire data set like:

import numpy as np
v=[1,4,4,4,23,4]
t=[1,2,3,4,5,6]

data = np.array([v,t])

Then you could slice it to get a subset of the data easily:

data[:,2:4]  #array([[4, 4],[3, 4]])

ii = [1,2,5] # Fancy indexing
data[:,ii] # array([[4, 4, 4],
           #        [2, 3, 6]])
骷髅 2024-11-07 17:55:05

你可以试试字典吗?在其他语言中,这可能被称为哈希映射、哈希表、关联数组或具有相同含义的其他术语。当然,这取决于您打算如何访问数据。

而不是:

v=[1,4,4,4,23,4]
t=[1,2,3,4,5,6]

你会:

v_with_t_as_key = {1:1,  # excuse the name...
                   2:4, 
                   3:4, 
                   4:4, 
                   5:23, 
                   6:4} 

这是 python 中相当标准的构造,尽管如果顺序很重要,你可能想看看有序的 字典 in 集合< /a>.

You could try a dictionary? In other languages this may be known as a hash-map, hash-table, associative array, or some other term which means the same thing. Of course it depends on how you intend to access your data.

Instead of:

v=[1,4,4,4,23,4]
t=[1,2,3,4,5,6]

you'd have:

v_with_t_as_key = {1:1,  # excuse the name...
                   2:4, 
                   3:4, 
                   4:4, 
                   5:23, 
                   6:4} 

This is a fairly standard construct in python, although if order is important you might want to look at the ordered dictionary in collections.

月寒剑心 2024-11-07 17:55:05

我发现,对于探索和原型设计,存储为列表/锯齿状列数组更方便,其中第一列是观察索引,之后的每一列都是变量。

data=[(1,2,3,4,5,6),(1,4,4,4,23,4)]

大多数时候我会加载许多带有许多变量的观察结果,然后执行排序,格式化,或显示一个或多个变量,甚至以列作为参数连接两组数据。当我需要提取一部分观察结果时,这种情况就很少见了。即使我这样做了,使用一个返回给定观察索引列的数据子集的函数会更方便。

话虽如此,我仍然使用函数将锯齿状数组转换为二维数组并转置二维数组。

I've found that for exploring and prototyping, it's more convenient to store as a list/jagged array of columns, where the first column is the observational index and each column after that is a variable.

data=[(1,2,3,4,5,6),(1,4,4,4,23,4)]

Most of the time i'm loading many observations with many variables, and then performing sorting, formatting, or displaying one or more of those variables, or even joining two sets of data with columns as parameters. It's a lot rarer when I need to pull a subset of observations out. Even if I did, it's more convenient to use a function that returns a subset of the data given a column of observation indexes.

Having said that, I still use functions to convert jagged arrays to 2d arrays and to transpose 2d arrays.

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