在Python中重新组织列表的列表

huangapple go评论66阅读模式
英文:

Re-organizing lists of lists in python

问题

我是新手学习Python尝试读取一个包含对象位置数据的大型文件涵盖了一段时间序列类似于
```python
for a in dum_un.file:
    
    FD = dum_un.select_object("type 1")
    r = FD.positions

每个 r 对应于特定的时间点,列表内的列表对应于该时间点物体的x、y和z位置。整个时间序列中的顺序保持一致。两个物体的示例:

for循环的第一次迭代:r = [[1.11, 2.64, 3.3],[4.0, 5.12, 6.32]]

for循环的第二次迭代:r = [[5.7, 4.44, 1.8],[6.3, 8.9, 4.7]]

我想要能够访问每个对象在整个时间序列内所有x(或y或z)值的列表,例如:

对于对象1,x = [1.11, 5.7]

对于对象2,x = [4.0, 6.3]


<details>
<summary>英文:</summary>

I am new to python and trying to read a large file with position data for objects over a time series. Something like:
~~~python
for a in dum_un.file:
    
    FD = dum_un.select_object(&quot;type 1&quot;)
    r = FD.positions
~~~

Each `r` corresponds to a specific time point, and each list-within-the-list corresponds to the x, y, and z positions of an object at that time point. The ordering is the same throughout the time series. Example for 2 objects:

First iteration of the for loop: `r = [[1.11, 2.64, 3.3],[4.0, 5.12, 6.32]]`

Second iteration of the for loop: `r = [[5.7, 4.44, 1.8],[6.3, 8.9, 4.7]]`

I want to be able to access a list of all x (or y or z) values for each object over the entire time series, e.g., here:

`x = [1.11, 5.7]` for object 1

`x = [4.0, 6.3]` for object 2

</details>


# 答案1
**得分**: 1

这里是一种方法。

解析创建了一个名为`objects`的字典,看起来类似于这样:

```json
{
  "1": {
    "x": [1.11, 5.7],
    "y": [...],
    "z": [...]
  },
  "2": {
    "x": [...],
    "y": [...],
    "z": [...]
  },
  // 其他对象的数据...
}

以下是代码:

# 这个列表表示每次迭代中 `r` 的值
all_r = [
    [[1.11, 2.64, 3.3], [4.0, 5.12, 6.32]],
    [[5.7, 4.44, 1.8], [6.3, 8.9, 4.7]]
]

# 初始化一个字典来收集所有结果
# 每个条目的键将是对象的“ID”
# 例如,`r` 中的第一个项目代表“对象 1”。
objects = dict()

for r in all_r:
    for i, coordinates in enumerate(r):
        # Python 索引从零开始。因此,加一以匹配您的定义
        object_id = str(i + 1)
    
        # 如果对象的字典键不存在,则初始化
        if object_id not in objects.keys():
            objects[object_id] = {
                "x": [],
                "y": [],
                "z": []
            }

        # 将每个坐标添加到相应的X、Y或Z列表中
        objects[object_id]["x"].append(coordinates[0])
        objects[object_id]["y"].append(coordinates[1])
        objects[object_id]["z"].append(coordinates[2])

expected_x_object_1 = [1.11, 5.7]
expected_x_object_2 = [4.0, 6.3]

actual_x_object_1 = objects.get("1", dict()).get("x", None)
actual_x_object_2 = objects.get("2", dict()).get("x", None)

assert expected_x_object_1 == actual_x_object_1
assert expected_x_object_2 == actual_x_object_2
英文:

Here's one way.

The parsing creates a dictionary called objects that looks something like this:

{
  &quot;1&quot;: {
    &quot;x&quot;: [1.11, 5.7],
    &quot;y&quot;: [...],
    &quot;z&quot;: [...],
  },
  &quot;2&quot;: {
    &quot;x&quot;: [...],
    &quot;y&quot;: [...],
    &quot;z&quot;: [...],
  },
  ...
}

Here's the code:

# This list represents the value of `r` in each iteration
all_r = [
    [[1.11, 2.64, 3.3],[4.0, 5.12, 6.32]],
    [[5.7, 4.44, 1.8],[6.3, 8.9, 4.7]]
]

# Initialise a dictionary to collect all the results
# The key for each entry will be the object &quot;ID&quot;
# e.g. the first item in `r` represents &quot;object 1&quot;.
objects = dict()

for r in all_r:
    for i, coordinates in enumerate(r):
        # Python indexes start at zero. So increment by one to match your definition
        object_id = str(i + 1)
    
        # Initialise the dictionary key for our object if it doesn&#39;t exist already
        if object_id not in objects.keys():
            objects[object_id] = {
                &quot;x&quot;: [],
                &quot;y&quot;: [],
                &quot;z&quot;: [],
            }

        # Add each co-ordinate to their relevant X, Y or Z list
        objects[object_id][&quot;x&quot;].append(coordinates[0])
        objects[object_id][&quot;y&quot;].append(coordinates[1])
        objects[object_id][&quot;z&quot;].append(coordinates[2])

expected_x_object_1 = [1.11, 5.7]
expected_x_object_2 = [4.0, 6.3]

actual_x_object_1 = objects.get(&quot;1&quot;, dict()).get(&quot;x&quot;, None)
actual_x_object_2 = objects.get(&quot;2&quot;, dict()).get(&quot;x&quot;, None)

assert expected_x_object_1 == actual_x_object_1
assert expected_x_object_2 == actual_x_object_2

huangapple
  • 本文由 发表于 2023年1月9日 16:49:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定