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

flvlnr44  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(604)

在你们告诉我去搜索之前,让我告诉你们“我有”。
我找了不少。论坛、教育/培训网站、stackexchange和所有常客。我没能得到我需要的东西。我也不会一有麻烦就跑向se。我已经在se上注册很久了,这是我的第一个问题。
我找到了一些回复(建议,而不是解决方案)和一些答案(某种程度上)。然而,我发现最有用的是https://stackoverflow.com/a/6181978/15065496
我知道什么是字典,我知道如何使用它们。我也试过上面的答案,但这并不是我想要的。
我是一名机械工程师,学习python来解决我遇到的一些工程问题。请读到最后,以便更好地了解我正在努力实现的目标。
我需要创建一个带有名字的列表。我已经能够创建一个列表列表,如下所示:

  1. list_xyz = []
  2. for i in range(1,5,1):
  3. temp_array_name = "list_" + str(i)
  4. list_xyz.append(temp_array_name)
  5. >>> list_xyz
  6. ['list_1', 'list_2', 'list_3', 'list_4']

现在,我想用每个元素创建一个新列表。
基本上,我想要的是创建名为list_1、list_2、list_3、list_4的列表

  1. list_1 = []
  2. list_2 = []
  3. list_3 = []
  4. list_4 = []

我已经尝试过的是:

  1. for i in list_xyz:
  2. i = list()
  3. * this gives:*
  4. >>> list_xyz
  5. ['list_1', 'list_2', 'list_3', 'list_4']
  6. * I thought good*. Then I typed:
  7. >>> list_1
  8. * I was expecting the following *
  9. []
  10. * Instead, I got:*
  11. Traceback (most recent call last):
  12. File "<pyshell#93>", line 1, in <module>
  13. list_1
  14. NameError: name 'list_1' is not defined
  15. * Then I tried the following and understood what had happened.*
  16. >>> i
  17. []
  18. * So then, I tried:*
  19. for i in range(0,len(list_xyz),1):
  20. list_xyz[i] = list()
  21. * this gave:*
  22. >>> list_xyz
  23. [[], [], [], []]

现在,从技术上讲,我可以使用list_xyz的元素,但是调用它们会很痛苦。如果我需要list_xyz的第二个元素中的第三个元素,那么我必须说 list_xyz[1][2] 相反,如果他们被命名,我只能说 list_2[2] 可以得到我想要的元素。
我也尝试过使用字典,正如https://stackoverflow.com/users/455276/the-wolf 在里面https://stackoverflow.com/a/6181978/15065496.
我尝试的是:

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

这接近我想要的,但不是我想要的。请阅读以下内容,以准确了解我想要什么。
我之所以需要这个特别的列表,原因是:我试图计算不同时间、不同位置的一些温度。我想创建一个字典,以时间为键,以不同位置的温度列表为值。
还有一本字典,以位置为键,列出不同时间的温度值。
我想要的是:

  1. dict1 = {time1: temps_at_time_1
  2. time2: temps_at_time_2
  3. time3: temps_at_time_3
  4. time4: temps_at_time_4
  5. time5: temps_at_time_5
  6. }
  7. dict2 = {loc1: temps_at_times_L1
  8. loc2: temps_at_times_L2
  9. loc3: temps_at_times_L3
  10. loc4: temps_at_times_L4
  11. loc5: temps_at_times_L5
  12. }

其中,时间点温度是位置1不同时间的温度列表。其中,temp_at_time_1是不同位置时间1的温度列表。
假设有25个位置和100个时间步,那么:

  1. len(temps_at_times_L1) = 100
  2. len(temp_at_time_1) = 25

现在,如果我可以调用时间为1的temp_和时间为1的temp_变量,那么我可以很容易地绘制温度等。
基本上,在使用wolf的建议后,我希望与使用字典实现的一样,但是在那里创建的列表应该有我可以调用的名称,以便在计算温度时,我可以通过调用append()方法来更新列表。如果我在计算timestep1,我想说:

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

如果我能做到以下几点,那就更好了:

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

我敢肯定最后一行行不通,为此我将不得不再次绞尽脑汁。我试过阅读vars()的工作原理,但它让我头疼。
如果你们不能帮助我,和/或告诉我我的代码的最后一行是Stoooopid,那么我就只能勉强应付基本字典和可笑的复杂(无论如何对于我的级别)索引、调用和直接分配给值字典。
我以前在使用openpyxl编辑电子表格时也遇到过同样的需求,但无法从另一个列表中的变量名创建列表。我放弃了,用另一种方法完成了任务。然而,这一次我想知道怎么做:如果能做到的话。
谢谢大家费心通读我冗长的解释。我这样做是为了让那些回复我的人能够准确地理解我需要什么,并且不会给出不符合我目的的建议。不过,我会阅读所有建议,并考虑所有有用的建议。我不是想冒犯/侮辱任何人。

yvgpqqbh

yvgpqqbh1#

抱歉,这是否定的,但在变量中添加数字与任何程序员应该做的恰恰相反- nested_lists[1][2] 是处理嵌套列表的正确方法, nested_list_1[2] 事实并非如此。不要将数据写入变量的名称中,只是不要这样做,它不会很好地结束并且 globals() 这是严重的误用。
您需要的是一些适当的结构,使您的数据具有意义。我推荐 pandas 图书馆。这就像是python的excel。
让我们以以下两个位置的5次测量为例 foobar 在不同的时间。

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

产量:

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

我不能列举你能用一个 Dataframe 做的所有事情,但它可以被排序、过滤、Map、索引。。。请参阅文档或在此处搜索答案。
您可能对按位置筛选数据感兴趣 data[data['location']=='foo'] ```
time temperature location
0 1 12 foo
3 10 23 foo
4 3 12 foo

  1. 或者将 Dataframe 的索引设置为 `time` 列和使用它的索引:

new_data = data.set_index('time')
print(new_data.loc[3])

temperature location
time
3 15 bar
3 12 foo

展开查看全部
wlzqhblo

wlzqhblo2#

正如我前面提到的几个评论员所说的那样,你实际上可以通过编程方式创建变量,但这是一种非常丑陋的解决问题的方法。
无论如何,您可以使用 globals() 详情如下:

  1. >>> list_xyz
  2. ['list_1', 'list_2', 'list_3', 'list_4']
  3. >>> for n in list_xyz:
  4. ... globals()[n] = list()
  5. ...
  6. >>> list_1
  7. []
  8. >>> list_2
  9. []
  10. >>> list_3
  11. []
  12. >>> list_4
  13. []
  14. >>>

相关问题