简体   繁体   中英

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!):

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.

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_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. 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; basically I am trying to use a for loop (or similar) in the manner below:

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.

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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