0

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.

4

3 回答 3

1

我认为这可以满足您的需求。它虽然有点笨重。您的输入是否总是相同的长度?

def depluralise(plurals, plural_endings):
    new_list = []
    for plural, plural_ending in zip(plurals, plural_endings):
        if plural.endswith(plural_ending):
            if plural_ending == '':
                new_list.append(plural)
            else:
                new_list.append(plural[:-len(plural_ending)])
    return new_list 


plural_string = "cell phones|sheep|oxen"
plurals = plural_string.split('|')
plural_endings_string = "s,,en"
plural_endings = plural_endings_string.split(',')

print depluralise(plurals, plural_endings)

plurals = ['children', 'brothers', 'fish', 'penguins']
plural_endings = ['ren', 's', '', 's']

print depluralise(plurals, plural_endings)
于 2013-10-08T20:45:50.627 回答
1
>>> p = 'children'
>>> e = 'ren'
>>> if p.endswith(e):
...   print p[:-len(e)]
... 
child
于 2013-10-08T20:25:09.033 回答
0

这可能不是您想要的答案,但为了获得最佳的去复数化,您需要一本名词及其复数字典。

搜索复数,并将其替换为单数。

我认为与尝试处理遇到的所有异常相比,这样做可以减少头发:

  • 女性
  • 现象
  • 数据
  • 骰子
  • ...
于 2013-10-08T20:28:01.627 回答