首页 > 解决方案 > Python:创建一个列表,其中每个元素都是一个有名称的列表

问题描述

在你们都告诉我搜索东西之前,让我告诉你们“我有”。

我已经搜索了很多。论坛、教育/培训网站、stackexchange 和所有常客。我无法得到我需要的东西。我也不会在遇到麻烦的第一个迹象时就跑去 SE。我很久以前在 SE 上注册了,这是我的第一个问题。

我也找到了一些回复(建议,而不是解决方案)和一些答案(有点)。但是,我发现的最有用的是https://stackoverflow.com/a/6181978/15065496

我确实知道字典是什么,我知道如何使用它们。我也尝试过上面的答案,但这并不是我想要的。

我是一名机械工程师,学习 python 来解决我遇到的一些工程问题。请阅读到最后,以更好地了解我想要实现的目标。

我需要创建一个带有名称的列表列表。我已经能够创建一个列表列表,如下所示:

list_xyz = []
for i in range(1,5,1):
    temp_array_name = "list_" + str(i)
    list_xyz.append(temp_array_name)

>>> list_xyz
['list_1', 'list_2', 'list_3', 'list_4']

现在,我想从每个元素中创建一个新列表。

基本上,我想要的是创建名为 list_1、list_2、list_3、list_4 的列表。我想自动化做的任务

list_1 = []
list_2 = []
list_3 = []
list_4 = []

我已经尝试过的是:

for i in list_xyz:
    i = list()

*this gives:*

>>> list_xyz
['list_1', 'list_2', 'list_3', 'list_4']

*I thought good*. Then I typed:

>>> list_1

*I was expecting the following *
[]

*Instead, I got:*
Traceback (most recent call last):
  File "<pyshell#93>", line 1, in <module>
    list_1
NameError: name 'list_1' is not defined


*Then I tried the following and understood what had happened.*
>>> i
[]

*So then, I tried:*

for i in range(0,len(list_xyz),1):
    list_xyz[i] = list()

*this gave:*

>>> list_xyz
[[], [], [], []]


现在,从技术上讲,我可以使用 list_xyz 的元素,但调用它们会很痛苦。如果我需要 list_xyz 的第二个元素中的第三个元素,那么我不得不说 list_xyz[1][2]

相反,如果他们被命名,我可以说list_2[2]并且可以获得我正在寻找的元素。

我也尝试过使用字典,正如https://stackoverflow.com/users/455276/the-wolfhttps://stackoverflow.com/a/6181978/15065496中所建议的那样。

我尝试过的是:

>>> dict1 = {}
>>> list_pqr = ['list_1', 'list_2', 'list_3', 'list_4']
>>> for i in range(0,len(list_pqr),1):
    dict1[list_pqr[i]] = list()

    
>>> dict1
{'list_3': [], 'list_2': [], 'list_1': [], 'list_4': []}

这接近我想要的,但不是我想要的。请阅读以下内容以准确了解我想要的内容。

我需要这个特定列表的原因是: 我试图在不同位置计算不同时间的一些温度。我想创建一个字典,其中时间作为键,不同位置的温度列表作为值。

和另一个以位置为键的字典,并将不同时间的温度列为值。

我想要的是:

dict1 = {time1: temps_at_time_1
time2: temps_at_time_2
time3: temps_at_time_3
time4: temps_at_time_4
time5: temps_at_time_5
}

dict2 = {loc1: temps_at_times_L1
loc2: temps_at_times_L2
loc3: temps_at_times_L3
loc4: temps_at_times_L4
loc5: temps_at_times_L5
}

其中temps_at_times_L1是位置 1 在不同时间的温度列表。其中temp_at_time_1是时间 1 不同位置的温度列表。

假设有 25 个位置和 100 个时间步长,那么:

len(temps_at_times_L1) = 100
len(temp_at_time_1) = 25

现在,如果temps_at_times_L1temp_at_time_1是我可以调用的变量,那么我可以很容易地绘制温度等。

基本上,我想要在使用wolf的建议后用字典实现的相同的东西,但是在那里创建的列表应该有我可以调用的名称,以便在计算温度时,我可以通过调用 append( ) 方法。如果我正在计算 timestep1,我只想说:

while(timestep == 1):
   for i in temperature_location_array:
       temps_at_time_1.append(i)

如果我能够做到以下几点会更好:

for a in range(startTime, endTime, stepSize):
while(timestep == a):
   for i in temperature_location_array:
       temps_at_time_a.append(i)       or      vars("temps_at_time_" + str(a)).append(i)

我很确定最后一行行不通,为此我将不得不再次绞尽脑汁。我曾尝试阅读 vars() 的工作原理,但这让我很头疼。

如果你们不能帮助我,和/或告诉我我的代码的最后一行是stooooopid,那么我只需要使用基本字典和可笑的复杂(无论如何对于我的水平)索引和调用和直接分配给值字典。

我以前在使用 openpyxl 编辑电子表格时遇到过同样的需求,但无法从另一个列表中的变量名称创建列表。我放弃并使用了另一种完成工作的方法。但是,这一次我想知道该怎么做:如果可以做到的话。

感谢大家阅读我冗长的解释的麻烦。我这样做是为了让回复的人确切地了解我的需要,并且不会给出不符合我目的的建议。我会阅读所有建议并考虑所有有用的建议。我不是想冒犯/侮辱任何人。

标签: pythonlistdictionary

解决方案


抱歉,给变量添加数字与任何程序员应该做的完全相反——nested_lists[1][2]这是处理嵌套列表的正确方法,nested_list_1[2]不是。不要将数据写入变量的名称,只是不要,它不会很好地结束并且globals()在这里是可怕的误用。

您需要的是一些适当的结构,可以为您的数据赋予意义。我会推荐pandas图书馆。这就像 Python 的 Excel。

让我们以以下示例在两个位置foobar不同时间进行 5 次测量。

# Assuming the raw data is in format:
#  (time, temperature, location)
import pandas as pd
data=pd.DataFrame({
    (1,12, 'foo'),
    (2,12,'bar'),
    (10,23,'foo'),
    (3,12,'foo'),
    (3,15,'bar')
    },columns=['time','temperature','location'])
print(data)

产量:

   time  temperature location
0     1           12      foo
1     2           12      bar
2     3           15      bar
3    10           23      foo
4     3           12      foo

我无法列举您可以对数据框执行的所有操作,但可以对其进行排序、过滤、映射、索引...请参阅文档或在此处搜索答案。

您可能对例如按位置过滤数据感兴趣data[data['location']=='foo']

   time  temperature location
0     1           12      foo
3    10           23      foo
4     3           12      foo

或将数据框的索引设置为time列并使用它进行索引:

new_data = data.set_index('time')
print(new_data.loc[3])
      temperature location
time                      
3              15      bar
3              12      foo

推荐阅读