0

Input :

a = [{'layer': 'OVC', 'ceiling': '020', 'type': None}]
b = [{'layer': 'BKN', 'ceiling': '010', 'type': None}, {'layer': 'OVC', 'ceiling': '020', 'type': None}]
c = [{'layer': 'BKN', 'ceiling': '010', 'type': None}, {'layer': 'BKN', 'ceiling': '020', 'type': None},{'layer': 'OVC', 'ceiling': '030', 'type': None}]
d = [a,b,c] 

results1 = []
results2 = [] 

for i in range(0,len(d)):
 res1=[]
 res2=[]
 cloudstf = d[i]
 if not cloudstf:
  res1 = "-9999"
  res2 = "-9999"
  results1.append(res1)
  results2.append(res2)
 elif len(d[i])==1:
  res1 = d[i]["layer"]
  res2 = d[i]["ceiling"]
  results1.append([res1])
  results2.append([res2])
 elif len(d[i])>1:
  for k in range(0,len(d)):
   res1 = d[i][k]["layer"]
   res2 = d[i][k]["ceiling"]
   results1.append([res1])
   results2.append([res2])
 cloudtf = []

cloud_group_layer = results1
cloud_group_height = results2 
print(cloud_group_height)
print(cloud_group_layer)

Instead of getting:

cloud_group_layer = [['OVC'], ['BKN'], ['OVC'], ['BKN'],['BKN'],['OVC']]
cloud_group_height = [['020'], ['010'], ['020'], ['010'],['020'],['030']]

How can I get:

cloud_group_layer = [['OVC'], ['BKN','OVC'], ['BKN','BKN','OVC']]
cloud_group_height = [['020'], ['010','020'], ['010','020','030']]

Where the layer and height are group by how many. The code is all there just a little confusion in the syntax. Or it could be a Tuple because of the heights.

You might get this error while running it and its because of the indexing:

Traceback (most recent call last):
  File "main.py", line 22, in <module>
    res1 = d[i]["layer"]
TypeError: list indices must be integers or slices, not str 
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
John
  • 157
  • 1
  • 10

2 Answers2

2

You can do that with some comprehensions like:

Code:

cloud_group_layer = [[x['layer'] for x in data] for data in d]
cloud_group_height = [[x['ceiling'] for x in data] for data in d] 

Test Code:

a = [{'layer': 'OVC', 'ceiling': '020', 'type': None}]
b = [{'layer': 'BKN', 'ceiling': '010', 'type': None},
     {'layer': 'OVC', 'ceiling': '020', 'type': None}]
c = [{'layer': 'BKN', 'ceiling': '010', 'type': None},
     {'layer': 'BKN', 'ceiling': '020', 'type': None},
     {'layer': 'OVC', 'ceiling': '030', 'type': None}]

d = [a, b, c]

wanted_cloud_group_layer = [['OVC'], ['BKN', 'OVC'], ['BKN', 'BKN', 'OVC']]
wanted_cloud_group_height = [['020'], ['010', '020'], ['010', '020', '030']]

cloud_group_layer = [[x['layer'] for x in data] for data in d]
cloud_group_height = [[x['ceiling'] for x in data] for data in d]

assert wanted_cloud_group_layer == cloud_group_layer
assert wanted_cloud_group_height == cloud_group_height
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • I think I found it https://stackoverflow.com/questions/5946236/how-to-merge-multiple-dicts-with-same-key – John Dec 13 '18 at 04:45
0

Simple solution might be to obtain the list you want to append instead of the elements of that list. Say you want to append ['BKN','OVC'] then dont append ['BKN'] and ['OVC'], append the entire list on obtaining it.

Hope this helps!

Sumukha Pk
  • 124
  • 1
  • 7