Python Forum
Converting to a list and sort
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting to a list and sort
#1
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]
Reply
#2
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.
Reply
#3
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.
Reply
#4
don't forget to rename split!
Reply
#5
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'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
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?
Reply
#7
(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”
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list.sort() returning None SmallCoder14 8 530 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  Sort a list of dictionaries by the only dictionary key Calab 1 488 Oct-27-2023, 03:03 PM
Last Post: buran
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,311 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,111 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  [solved] Sort list paul18fr 5 2,857 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  Converting a list to dictinary tester_V 8 2,701 Jul-02-2021, 09:04 PM
Last Post: tester_V
  Sort List of Lists by Column Nju 1 11,152 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Converting tkinter listbox into list BigSwiggy 6 3,605 Feb-07-2021, 02:01 PM
Last Post: BigSwiggy
  How to sort os.walk list? Denial 6 11,515 Oct-10-2020, 05:28 AM
Last Post: Denial
  Converting list to variables Palves 1 1,761 Sep-18-2020, 05:43 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020