python - 输入字符串列表 Python

标签 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 = 英国 字数 = 英国、英国、美国、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/

相关文章:

Python ArgParse Subparsers 并链接到正确的函数

C++ 更快地进行字符串加法?

Java - 字符串输入异常处理

algorithm - 高效的模式匹配/字符串合并算法

algorithm - 相似矩阵 -> 特征向量算法?

python - 如何使用Python中的套接字库将服务器连接到其他计算机?

python - R 和 Numpy 的 QR 分解之间的差异

python - fnmatch 未找到 "&"字符

string - 使文本编辑器动态高度 SwiftUI

nlp - 计算两个副词或两个形容词的相似度