简体   繁体   English

Python-遍历并提取字典类型列表的元素

[英]Python - Iterate through, and extract, elements of a dictionary type list

I have a list containing many elements and some of these elements have their own sub lists. 我有一个包含许多元素的列表,其中一些元素具有自己的子列表。 A sample list is follows (my actual list has >100 elements!): 以下是一个示例列表(我的实际列表包含> 100个元素!):

data_all = [{u'heatindexm': u'-9999', u'windchillm': u'-999', u'wdire': u'ENE', u'wdird': u'70', u'windchilli': u'-999', u'hail': u'0', u'heatindexi': u'-9999', u'precipi': u'', u'thunder': u'0', u'pressurei': u'29.95', u'snow': u'0', u'pressurem': u'1014', u'fog': u'0', u'icon': u'', u'precipm': u'', u'conds': u'', u'tornado': u'0', u'hum': u'44', u'tempi': u'71', u'tempm': u'22', u'dewptm': u'12', u'rain': u'0', u'dewpti': u'54', u'date': {u'mday': u'01', u'hour': u'00', u'min': u'00', u'mon': u'07', u'pretty': u'12:00 AM BST on July 01, 2015', u'year': u'2015', u'tzname': u'Europe/London'}, u'visi': u'12', u'vism': u'19', u'utcdate': {u'mday': u'30', u'hour': u'23', u'min': u'00', u'mon': u'06', u'pretty': u'11:00 PM GMT on June 30, 2015', u'year': u'2015', u'tzname': u'UTC'}, u'wgusti': u'', u'metar': u'AAXX 30234 03772 45/69 /0710 10215 20120 30111 40140 58018 333 55300 20000', u'wgustm': u'', u'wspdi': u'11.5', u'wspdm': u'18.5'}, {u'heatindexm': u'-9999', u'windchillm': u'-999', u'wdire': u'East', u'wdird': u'90', u'windchilli': u'-999', u'hail': u'0', u'heatindexi': u'-9999', u'precipi': u'-9999.00', u'thunder': u'0', u'pressurei': u'29.95', u'snow': u'0', u'pressurem': u'1014', u'fog': u'0', u'icon': u'clear', u'precipm': u'-9999.00', u'conds': u'Clear', u'tornado': u'0', u'hum': u'56', u'tempi': u'69.8', u'tempm': u'21.0', u'dewptm': u'12.0', u'rain': u'0', u'dewpti': u'53.6', u'date': {u'mday': u'01', u'hour': u'00', u'min': u'20', u'mon': u'07', u'pretty': u'12:20 AM BST on July 01, 2015', u'year': u'2015', u'tzname': u'Europe/London'}, u'visi': u'-9999.0', u'vism': u'-9999.0', u'utcdate': {u'mday': u'30', u'hour': u'23', u'min': u'20', u'mon': u'06', u'pretty': u'11:20 PM GMT on June 30, 2015', u'year': u'2015', u'tzname': u'UTC'}, u'wgusti': u'-9999.0', u'metar': u'METAR EGLL 302320Z 09008KT CAVOK 21/12 Q1014 NOSIG', u'wgustm': u'-9999.0', u'wspdi': u'9.2', u'wspdm': u'14.8'}]

I would like to iterate through the list and extract certain information from each element (for example ['pressurem'] and ['tempm'], but also information within the element's sublists such as ['utcdate']['hour'] and ['utcdate']['min'] in such a manner so the output of each iteration is placed into a new list. These new lists will, in turn, be placed (as elements) within a macro list. 我想遍历列表并从每个元素中提取某些信息(例如['pressurem']和['tempm'],以及元素的子列表中的信息,例如['utcdate'] ['hour']和['utcdate'] ['min']的方式是将每次迭代的输出放置到新列表中,然后将这些新列表(作为元素)放置在宏列表中。

I know how to get the information/values of interest I would like manually ie I can pull the values of interest for each element as seen in the code below. 我知道如何手动获取感兴趣的信息/值,即我可以拉出每个元素的感兴趣值,如下面的代码所示。 This code returns the values (including information from the sub-lists) that I am interested in finding from the first element within the data_all list. 这段代码返回了我想从data_all列表中的第一个元素中找到的值(包括子列表中的信息)。

data_string_sample=((data_all[0]['utcdate']['mday']),(data_all[0]['utcdate']['mon']),(data_all[0]['utcdate']['year']),(data_all[0]['utcdate']['hour']),(data_all[0]['utcdate']['min']),(data_all[0]['tempm']),(data_all[0]['hum']),(data_all[0]['pressurem']))
data_string_list=list(data_string_sample)
print(data_string_list)

However the above only works for the first element of the list and I cannot figure out the correct syntax for a for loop that will iterate through each element of the data_all list and produce the same output for every element within the data_all list. 但是,以上内容仅适用于列表的第一个元素,我无法找出for循环的正确语法,该循环将遍历data_all列表的每个元素并为data_all列表中的每个元素产生相同的输出。 The output from each element of the data_all list can be captured and put into a new list...... I hope the above isn't too confusing; data_all列表的每个元素的输出都可以捕获并放入新列表中……我希望以上内容不要太混乱; basically I am trying to use a for loop (or similar) in the manner below: 基本上我试图以下面的方式使用for循环(或类似方法):

for i in data_all:
    generate i number of data_strings
    convert each data_string into a list
    have each mini list as an element within a new list (list_of_elements)   
    print (list_of_elements) to show:
          #1 [u'30', u'06', u'2015', u'23', u'00', u'22', u'44', u'1014']
          #2 [u'11:20 PM GMT on June 30, 2015', u'21.0', u'56', u'1014']
          #3 etc...... 

Apologies for the newbie question - there is probably a function or library that can do the above for me but I am only learning to code so I would like to be able to write the above in the correct Python syntax. 为新手问题道歉-可能有一个函数或库可以为我完成上述操作,但我只是在学习编码,因此我希望能够以正确的Python语法编写以上内容。

You show this code already: 您已经显示了以下代码:

data_string_sample=((data_all[0]['utcdate']['mday']),(data_all[0]['utcdate']['mon']),(data_all[0]['utcdate']['year']),(data_all[0]['utcdate']['hour']),(data_all[0]['utcdate']['min']),(data_all[0]['tempm']),(data_all[0]['hum']),(data_all[0]['pressurem']))
data_string_list=list(data_string_sample)
print(data_string_list)

Where you specifically referenced element 0, instead use a variable. 在专门引用元素0的地方,请改用变量。 You could use a number, such as: 您可以使用数字,例如:

for i in range(len(data_all)):
    data_string_sample=((data_all[i]['utcdate']['mday']),(data_all[i]['utcdate']['mon']),(data_all[i]['utcdate']['year']),(data_all[i]['utcdate']['hour']),(data_all[i]['utcdate']['min']),(data_all[i]['tempm']),(data_all[i]['hum']),(data_all[i]['pressurem']))

It is more natural, however, to let the loop handle the indexing for you: 但是,让循环为您处理索引更为自然:

for data in data_all:
    data_string_sample=((data['utcdate']['mday']),(data['utcdate']['mon']),(data['utcdate']['year']),(data['utcdate']['hour']),(data['utcdate']['min']),(data['tempm']),(data['hum']),(data['pressurem']))

To collect each of this in a list, make a list and append your data: 要将这些收集在列表中,请列出并附加数据:

interesting_data = []
for data in data_all:
    data_string_sample=((data['utcdate']['mday']),(data['utcdate']['mon']),(data['utcdate']['year']),(data['utcdate']['hour']),(data['utcdate']['min']),(data['tempm']),(data['hum']),(data['pressurem']))
    interesting_data.append(data_string_sample)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM