输入字符串Python列表

时间:2013-05-08 01:46:35

标签: python string similarity

我修改了一个Python代码来创建一个简单的字符串相似性。

但是,我正在尝试做的是用户输入,我希望第二个用户输入(单词)包含单词列表,以便我可以在单词之间进行比较。

    '''
Input the English words in w1, and
the translated Malay words in the list
'''
w1 = raw_input("Enter the English word: ")
words = raw_input("Enter all the Malay words: ")
## The bits im not sure what to code
wordslist = list(words)

for w2 in wordslist:
    print(w1 + ' --- ' + w2)
    print(string_similarity(w1, w2))
    print

当我进入时,它似乎与整个'w1'输入形成字符串相似性,并且'words'输入中的所有单个字符。我想要的只是例如

w1 =英国 words =英国,联合王国,美国,Kingdmo。

然后它做了一个措施

United Kingdom --- United Kingdom
United Kingdom --- United Kingdoms
United Kingdom --- United Sates
United Kingdom --- Kingdmo

等等。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以使用str.split获取字词列表:

>>> strs = "United Kingdom, United Kingdoms, United States, Kingdmo"
>>> strs.split(",")
['United Kingdom', ' United Kingdoms', ' United States', ' Kingdmo']
str.split上的

帮助

>>> str.split?
Namespace:  Python builtin
Docstring:
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.