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
#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


Messages In This Thread
Converting to a list and sort - by tantony - Oct-04-2019, 07:37 PM
RE: Converting to a list and sort - by Larz60+ - Oct-04-2019, 07:49 PM
RE: Converting to a list and sort - by stullis - Oct-04-2019, 08:01 PM
RE: Converting to a list and sort - by Larz60+ - Oct-04-2019, 10:59 PM
RE: Converting to a list and sort - by perfringo - Oct-05-2019, 04:28 PM
RE: Converting to a list and sort - by tantony - Oct-07-2019, 01:55 PM
RE: Converting to a list and sort - by perfringo - Oct-07-2019, 03:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 685 Apr-29-2024, 04:38 PM
Last Post: Calab
  list.sort() returning None SmallCoder14 8 708 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,368 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,237 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  [solved] Sort list paul18fr 5 2,961 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  Converting a list to dictinary tester_V 8 2,800 Jul-02-2021, 09:04 PM
Last Post: tester_V
  Sort List of Lists by Column Nju 1 12,681 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Converting tkinter listbox into list BigSwiggy 6 3,732 Feb-07-2021, 02:01 PM
Last Post: BigSwiggy
  How to sort os.walk list? Denial 6 11,792 Oct-10-2020, 05:28 AM
Last Post: Denial
  Converting list to variables Palves 1 1,805 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