-1

I want to split this string:

"Oscar Wilde, Sophocles,  and  Lin-Manuel Miranda"

to get this list ('and' is discarded):

["Oscar Wilde", "Sophocles", "Lin-Manuel Miranda"]
S.B
  • 13,077
  • 10
  • 22
  • 49
SEUN
  • 23
  • 4
  • De you know how to split a string? – Dani Mesejo Oct 04 '21 at 21:57
  • Code example of what you have tried so far? – BoobyTrap Oct 04 '21 at 22:03
  • Welcome to Stack Overflow. What happened when you tried to write code to solve the problem? What part of this is actually the difficulty - when you tried your code, what was the result, and how was the different from what you wanted? Did you try to use a search engine to find ways to solve the problem? What happened when you tried that? Please read [ask] and https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. – Karl Knechtel Oct 04 '21 at 22:19

4 Answers4

1
my_name_str = "Oscar Wilde, Sophocles, and Lin-Manuel Miranda"
cleaned_name_str = my_name_str.replace(", and ", ", ")

print(my_name_list)
# "Oscar Wilde, Sophocles, Lin-Manuel Miranda"

my_name_list = my_name_str.split(",")
print(my_name_list)
# ["Oscar Wilde", " Sophocles", " Lin-Manuel Miranda"]

clean_name_list = [i.strip() for i in my_name_list]

print(clean_name_list)
# ["Oscar Wilde", "Sophocles", "Lin-Manuel Miranda"]

There are several ways you can remove the and, this is just one. The key things here are split which splits the string on a substring and strip which removes trailing and leading whitespace

The [item for item in list] bit is called "list comprehension" and is super powerful in python (there's also "dictionary comprehension") which is basically how filtering is done in python and can often clean up for loops.

Schalton
  • 2,867
  • 2
  • 32
  • 44
  • Nice solution as well, however you might want to use directly `my_name_str.split(",")` in the comprehension, this avoid loading the whole list in memory (not critical here, but a good practice in general) +1 ;) – mozway Oct 04 '21 at 22:10
  • 1
    Also be careful of `i.replace(" and ", "")`, this might replace legitimate bits of text if " and " is in the middle of the text. – mozway Oct 04 '21 at 22:14
  • @Mozway excellent points -- I often try to balance optimized code with readable code when answering questions for new folks; I'll rearrange some things – Schalton Oct 04 '21 at 22:17
  • @SUEN, FWIW I'd most likely use regex for this in production -- it's all preference at that point – Schalton Oct 04 '21 at 22:19
1

You could split using a regex:

s = "Oscar Wilde, Sophocles, and Lin-Manuel Miranda"

import re
out = re.split(',\s*(?:and\s*)?', s)

',\s*(?:and\s*)?' means a comma optionally followed by spaces and and.

Output:

['Oscar Wilde', 'Sophocles', 'Lin-Manuel Miranda']
mozway
  • 194,879
  • 13
  • 39
  • 75
0

you can replace the "and" to a comma and split anywhere it finds a comma, and then iterate over the list to clear any whitespace around with the strip.()

txt = "Oscar Wilde, Sophocles and Lin-Manuel and Miranda"

elem = txt.replace(' and ',',').split(',')
elem = [x.strip() for x in elem]
# print(elem): ['Oscar Wilde', 'Sophocles', 'Lin-Manuel', 'Miranda']

  • thank you, i tried this method but its not working for some elements of the list that looks like this ['Oscar de la Renta and Janet Ruttenberg'], if i replace and with ' ' – SEUN Oct 05 '21 at 02:48
  • caption = caption.replace(' and ', ',').split(',') , replacing and with comma works on ['Oscar de la Renta and Janet Ruttenberg'], but not on ["Oscar Wilde", "Sophocles", "Lin-Manuel Miranda"]). am new to python – SEUN Oct 05 '21 at 02:51
0
my_name_str = "Oscar Wilde, Sophocles, and Lin-Manuel Miranda"
my_name_split = my_name_str.split(' and ')
my_name_str = ''
for i in my_name_split:
    my_name_str += i
my_name_split = my_name_str.split(',')
print(my_name_split)
qwerteee
  • 197
  • 9