我修改了 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 = 英国 字数 = 英国、英国、美国、Kingdmo。
然后它会进行测量
United Kingdom --- United Kingdom
United Kingdom --- United Kingdoms
United Kingdom --- United Sates
United Kingdom --- Kingdmo
等等。
感谢您的帮助!
最佳答案
您可以使用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.
关于python - 输入字符串列表 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16431354/