Sorry, I asked the wrong question on my earlier post
Earlier Post. When I run this, I'm getting
018
002
024
200
005
def convert_ceiling(text):
split = text.split()
for s in split:
if 'FEW' in s or 'OVC' in s or 'BKN' in s or 'SCT' in s:
ceiling = s.replace('FEW','').replace('OVC','').replace('BKN','').replace('SCT','')
print(ceiling) # <------ NEED TO CHANGE THIS TO AN LIST
ceiling = int(ceiling) * 100
#print(ceiling)
#array = [ceiling]
#return array
#array.sort()
#return (array[0])
How can I change that to a list so I can sort it?
How can I get this?
[018,002,024,200,005]
split is already a list --- NOTE --- bad naming you override python split so that next time you try to use split, the reference will be to the list named split, and not split method.
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.
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.
don't forget to rename split!
After reading previous thread and this one I have understanding that the task is (correct me if I am wrong):
'KORD 031551Z 28011G18KT 10SM FEW017 OVC009 BKN024 BKN200 17/13 SCT005 A3003 RMK AO2 SLP168 T01720133' -> 1700 900 2400 20000 500
If only output needed then one can observe the structure of the string and code following:
>>> w = 'KORD 031551Z 28011G18KT 10SM FEW017 OVC009 BKN024 BKN200 17/13 SCT005 A3003 RMK AO2 SLP168 T01720133'
>>> print(*(int(chunk[3:]) * 100 for chunk in w.split() if chunk.startswith(('FEW', 'OVC', 'BKN', 'SCT'))))
1700 900 2400 20000 500
If list (or sorted list) required instead of printed output then one can skip the print part:
>>> coords = [int(chunk[3:]) * 100 for chunk in w.split() if chunk.startswith(('FEW', 'OVC', 'BKN', 'SCT'))]
>>> coords
>>> [1700, 900, 2400, 20000, 500]
>>> sorted(coords)
[500, 900, 1700, 2400, 20000]
If one want to have one string with spaces between coordinates then:
>>> ' '.join((str(coord) for coord in coords))
'1700 900 2400 20000 500'
>>> ' '.join((str(coord) for coord in sorted(coords)))
'500 900 1700 2400 20000'
Thank you for the solution, and it works. I don't understand the
int(chunk[3:]
. What does that do? Is chunk a reserved word in python?
(Oct-07-2019, 01:55 PM)tantony Wrote: [ -> ]What does that do? Is chunk a reserved word in python?
No, chunk is not keyword in Python. If one splits w on whitespace the resulting items in list are not words nor numbers hence name chunk. Alternative names could have been item or el (element).
In English (using item instead of chunk): “give me slice of item starting at index 3 converted to integer and multiplied with 100 for every item in list created by splitting w if item starts with one of specific three letter combinations”