I am python/programming newbie. Using python 2.7
I have been trying to figure out how to 'subtract' the elements in 1 list from the elements in another list. However I put 'subtract' in quotes because I am not working with integers, and could not think of another way to explain.
First up, here is my code:
plural_string = "cell phones|sheep|oxen" # result of user input with raw_input()
plural_endings_string = "s,,en" # result of user input with raw_input() - always comma separated.
plural_list = plural_string.split('|')
plural_endings_list = plural_endings_string.split(',')
# This is pseudo code since '-' does not work with non-integers in a string
# but it expresses what I am trying to achieve
new_list = [a - b for a, b in zip(plural_list, plural_endings_list)]
So, what I actually want the new list to look like is this:
>>> new_list
>>> ['cell phone', 'sheep', 'ox']
I basically want to de-pluralize the words(elements) in the plural_list
variable using the plural-endings(elements) in the plural_endings_list
variable.
A key things to note is: The number of elements (and therefore word choices) in the lists will vary based on user input (me). So, in a different situation, the lists I am working with could look like this:
plural_list = ['children', 'brothers', 'fish', 'penguins']
plural_endings_list = ['ren', 's', '', 's']
I have tried to figure out how to do this using the strings - and not lists - using the '.replace' function, but I come up against a brick wall, given that I don't know what the user input will be in each run of the script. I could not find a '1 fits all' solution. Unsuccessfully tried regex too, and had the same problem of not knowing what the user input will be. It is beyond my newbie brain right now.
Hopefully, I have explained myself clearly! If not I am trying to do the opposite of this other question in SO - How do i add two lists' elements into one list? But, instead of concatenation, I need 'subtraction'
Cheers Darren
EDIT1: In response to @brad comment. I actually feed in the plural endings to the plural_endings_list
via user input (this is part of a larger script). So if a list contains the element "children's"
, then I would choose "'s"
as the ending for the plural_endings_list
. It is always case specific.
EDIT2: In response to @Graeme Stuart comment. Graeme - the input will always vary in length. There could be 2 elements in each list, or there could be 10 elements in each list, or anything in between.