Jump to content
def classify(input_string):    split_string = None    numbers = []    strings = []        split_string = input_string.split("  ")        for x in split_string:        try:            numbers.append(int(x))        except:            strings.append(x)        return (numbers, strings)

Basically this function goes through a sentence and separates integers and strings. When the parameter is an empty string though (classify("")), it still adds the "" to the string list. Can someone show me how to fix this? I would really appreciate it. Thanks! Let me know if you need anything specified.

Link to comment
https://linustechtips.com/topic/482575-python-function-help/
Share on other sites

Link to post
Share on other sites

def classify(input_string):    split_string = None    numbers = []    strings = []        split_string = input_string.split("  ")        for x in split_string:        try:            numbers.append(int(x))        except:            strings.append(x)        return (numbers, strings)
Basically this function goes through a sentence and separates integers and strings. When the parameter is an empty string though (classify("")), it still adds the "" to the string list. Can someone show me how to fix this? I would really appreciate it. Thanks! Let me know if you need anything specified.

Can't you just add a special case, like:

if input_string == "":

return

or something like that?

Link to comment
https://linustechtips.com/topic/482575-python-function-help/#findComment-6473887
Share on other sites

Link to post
Share on other sites

I'm guessing you just need to check if the string is empty and then throw it away. An empty string is still a string, and can get split just like any string.

 

"x y" gets split at the space into split_string[0] "x" and split_string[1] "y"

"xy" gets split, since there's no space it will just be split into 1 piece at index [0] "xy"

"" gets split, since there's no space it goes into index [0] like the last one.

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
https://linustechtips.com/topic/482575-python-function-help/#findComment-6473908
Share on other sites

Link to post
Share on other sites

Can't you just add a special case, like:

if input_string == "":

return

or something like that?

 

I'm guessing you just need to check if the string is empty and then throw it away. An empty string is still a string, and can get split just like any string.

 

"x y" gets split at the space into [0] "x" and [1] "y"

"xy" gets split, since there's no space it will just be split into 1 piece at index [0] "xy"

"" gets split, since there's no space it goes into index [0] like the last one.

 

Oh right, yeah a conditional would work yeah...woops. How about if the string has two spaces in between the words instead of one??  How could I handle that??

Link to comment
https://linustechtips.com/topic/482575-python-function-help/#findComment-6473923
Share on other sites

Link to post
Share on other sites

Oh right, yeah a conditional would work yeah...woops. How about if the string has two spaces in between the words instead of one??  How could I handle that??

I don't do python, but i'd look for a function that removes whitespace from a string.

possibly this:

string.replace(" ", "")
 
Although rereading what you said, as long as the string split is one space, then it doesn't matter how many spaces there are between words if you throw away empty strings
 
look at this:
 
"hello my  name is squirrl"
that would get split into
"hello" "my" "" "name" "is" "squirrl"
the third string would get thrown away since it's empty and you're left with the sentence you wanted without any spaces.
Unless you want to handle it differently?

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
https://linustechtips.com/topic/482575-python-function-help/#findComment-6473937
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×