Oct-04-2019, 08:01 PM
There are a couple options. You could write a small closure and use map() to apply that function to each substring in split.
You could also use a list comprehension.
The easiest way though would be to make a list and call list.append to add the value to it.
You could also use a list comprehension.
The easiest way though would be to make a list and call list.append to add the value to it.
def convert_ceiling(text): split = text.split() ceiling = [] for s in split: if 'FEW' in s or 'OVC' in s or 'BKN' in s or 'SCT' in s: ceiling.append(s.replace('FEW','').replace('OVC','').replace('BKN','').replace('SCT',''))That last line is pretty gnarly. If you put those strings in a tuple and use str.find(), you can simplify:
def convert_ceiling(text): subs = ("FEW", "OVC", "BKN", "SCT") split = text.split() ceiling = [] for s in split: for each in subs: if s.find(each) >= 0: ceiling.append(s.replace(each,''))Based on the data provided in the earlier post, that should do the trick.