【腾讯云】亏本大甩卖，服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!

MySQL 1核1G 19元/年

I need to separate all possible suffixes (about 1000) from a given word. I am thinking about using a dict.

In doing so I would have suffixes as keys (and some additional information about the suffixes as values needed in the further process). If the longest possible suffix is 4 letters long I would search the dict for all possible combinations. For example: Given a word: 'abcdefg' I would search the dict for 'g','fg','efg' and 'defg'.

I have done some research and haven't found much similar uses of the dict. Could this be a viable solution or am I missing something here? Help much appriciated.

#### #0

If the suffixes aren't too long, your solution sounds fine -- it's only a few dictionary look-ups per word, and dictionary look-ups are fast. I don't think any more complex solution (like using a trie) would be worth it here. For only removing the suffix, you could also use a set instead of a dictionary, but since you need additional information for each suffix, a dictionary seems to be the natural choice.

#### #1

The simplest (probably not fastest) way would be to find all matches in a list. With 1000 items, you shouldn't have much trouble with performance.

``````>>> sufx = ['foo', 'bar']
>>> [s for s in sufx if 'bazbar'.endswith(s)]
['bar']
>>>[s for s in sufx if 'bazbaz'.endswith(s)]
[]
>>> [s for s in sufx if 'bazfoo'.endswith(s)]
['foo']
``````

#### #2

See Time Complexity of a dict. Lookup times for a dict are quite fast (O(1) on average!). For this implementation, your average time complexity for finding the longest suffix would be O(k^2), with k being the length of your word. It is k^2 due to the `''.join` operation (a similar O(n) operation like reversed or string slicing would be required, as strings do not support an O(1) appendleft operation).

Simple way of doing it (tested for python 3):

``````>>> from collections import deque
>>> word = "antidisestablishmentarianism"
>>> suffixes = {'ism': 3, 'anism': 6, 'ment': 4, 'arianism': 12}
>>> suffix = deque()
>>> longest = None
>>> for char in reversed(word):
...     suffix.appendleft(char)
...     suf = ''.join(suffix)
...     if suf in suffixes:
...         longest = suf
...
>>> longest
'arianism'
``````

#### #3

I'm not sure I understand your usecase correctly. I guess it is about the fact that you are handling suffixes and they are hard to detect.

A typical approach (typically in indexing situations) would be to turn your string around and handle the suffix as a prefix. Then you can do a simple binary search in a sorted list of your reversed suffixes (thus prefixes).

#### #4

If I understand what you want to do, you should be using the re module in the standard lib.

Docs are here:

http://docs.python.org/library/re.html#module-re

There's an example regarding adverbs here:

As for storing them as keys in a dict, seems fine to me. Especially, if you want to do some other processing for words that have the suffixes you care about.

#### 阅读全文

PHP/MySQLi：将lc_time_names and DATE_FORMAT（）设置为MySQLi查询？

[Translation] Introduction to ASP.NET Core

Visual studio 2008只运行一个web应用程序，而不是解决方案中的所有应用程序