Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python编程中,如何从列表中提取一个特定物种的值?_Python_List - Fatal编程技术网

在python编程中,如何从列表中提取一个特定物种的值?

在python编程中,如何从列表中提取一个特定物种的值?,python,list,Python,List,我有一个Python数据列表。所有值均对应于数据中的每个物种。我想提取特定物种的值。每个列表中都有相应的值。 假设我有三种铜,银,金和铂。在三种不同温度下,其频率如下所示: Frequency = [[{1}, {2}, {3}, {4}], [{1},{22}, {44}, {54}], [{23}, {43}, {52}] 请记住,每个列表的第三个值代表“Au”,即3,44,43。所以,我需要像[3,44,43]这样的答案,我自由地使用它,这样我们就可以根据元素名称对结果进行索引 由于我们

我有一个Python数据列表。所有值均对应于数据中的每个物种。我想提取特定物种的值。每个列表中都有相应的值。 假设我有三种铜,银,金和铂。在三种不同温度下,其频率如下所示:

Frequency = [[{1}, {2}, {3}, {4}], [{1},{22}, {44}, {54}], [{23}, {43}, {52}]
请记住,每个列表的第三个值代表“Au”,即3,44,43。所以,我需要像
[3,44,43]

这样的答案,我自由地使用它,这样我们就可以根据元素名称对结果进行索引

由于我们从一个集合中提取结果(例如,
{1}
),我们可以使用
iter
返回值的迭代器,然后调用
next
来提取第一个值

from collections import defaultdict
frequencies = [[{1}, {2}, {3}, {4}], [{1}, {22}, {44}, {54}], [{23}, {99}, {43}, {52}]]
res = defaultdict(list)
for i, element in enumerate(['Cu','Ag','Au','Pt']):
  for j in range(len(frequencies)):
    res[element].append(next(iter(frequencies[j][i])))
for element in res:
  print(f'{element} temperatures was {res[element]}')
输出:

Cu temperatures was [1, 1, 23]
Ag temperatures was [2, 22, 99]
Au temperatures was [3, 44, 43]
Pt temperatures was [4, 54, 52]

首先,你写的不是一个列表的列表。正如wjandrea所说:

这是集合列表的列表,而不是列表

其次,由于我不能清楚地理解您的问题,我想您需要一种从每个列表中提取特定值的方法。 为此,您可以使用NumpyPandas(假设您熟悉它们,否则请阅读和了解更多信息)

我这样做的方式可以是:

# in order to make a list of *temperatures* for *each* element we should first
# take the given array -- yours in this case -- and convert it into an array:
 
freq = np.array([[1, 2, 3, 4], [1,22, 44, 54], [np.nan, 23, 43, 52]]).T

# then we would create a DataFrame out of our array using our elements' names as 
# the indexing name:

data_frame = pd.DataFrame(freq, index=["Cu", "Ag", "Au", "Pt"], columns=["temp_1", "temp_2", "temp_3"])
结果:

    temp_1  temp_2  temp_3
Cu      1      1      0
Ag      2     22     23
Au      3     44     43
Pt      4     54     52
p、 美国。 例如,您还可以通过以下方式获得“Ag”元素的温度:

data_frame.loc["Ag"]
或者,如果您想要一列的整个温度,只需写下:

data_frame["temp_1"]

不清楚你想要什么。请解释清楚一点,以便我们能提供帮助。这是一个集合列表列表,而不是列表。另外,43不是“第三个”值。最后一个列表仅包含3项。“三种铜、银、金和铂”——是三种还是四种?您还缺少一个右括号[[-1.8620689655172413,298.0],…:[mpf('-395.1338625658188'),…:mpf('197.5669312894'),…:mpf('395.1338625658188'),…:mpf('-395.1338625658188'),…:[-2.0298.0],…:[强积金('-5795.4864909536747'),…:强积金('-2897.7432454768373'),…:强积金('-5795.4864909536747'),…:强积金('-5795.4864909536747')],…:[-1.862068965517241332413298.0],…:[强积金('-395.13386256258188'),…:强积金('-197.5669312894'),:强积金('-395.1338625558188.),…:强积金('('-395.1338625658188'),…:[-2.0298.0],…:[强积金('-5795.4864909536747'),…:强积金('2897.7432454768373'),…:强积金('5795.4864909536747'),…:强积金('-5795.4864909536747')]