Python:创建一个列表,其中每个元素都是一个带有名称的列表

时间:2021-07-27 09:59:11

标签: python list dictionary

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

我已经搜索了很多。论坛、教育/培训网站、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-wolf 中的 https://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': []}

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

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

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

我想要的是:

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 是我可以调用的变量,这将使我很容易绘制温度等。

基本上,我想要在使用的建议后用字典实现的相同的事情,但是在那里创建的列表应该有我可以调用的名称,以便在计算温度时,我可以通过调用 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 编辑电子表格时遇到过同样的需求,但无法从另一个列表中的变量名称创建列表。我放弃了并使用了另一种完成工作的方法。不过,这次我想知道怎么做:如果可以做到的话。

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

2 个答案:

答案 0 :(得分:3)

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

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

让我们以以下示例为例,在不同时间在两个位置 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

答案 1 :(得分:1)

正如我之前提到的几个评论者,您实际上可以通过编程方式创建变量,但这是解决问题的一种非常丑陋的方式。

无论如何,您可以使用 globals() 将变量添加到全局命名空间,如下所示:

>>> list_xyz
['list_1', 'list_2', 'list_3', 'list_4']
>>> for n in list_xyz:
...     globals()[n] = list()
... 
>>> list_1
[]
>>> list_2
[]
>>> list_3
[]
>>> list_4
[]
>>>