python - 从字符串列表中,如何获得 python 中最奇怪的单词/字符串

标签 python

我有一个字符串列表:

['twas', 'brillig', 'and', 'the', 'slithy', 'toves', 'did', 'gyre', 'and', 'gimble', 'in', 'the', 'wabe', 'all', 'mimsy', 'were', 'the', 'borogoves', 'and', 'the', 'mome', 'raths', 'outgrabe', '"beware', 'the', 'jabberwock', 'my', 'son', 'the', 'jaws', 'that', 'bite', 'the', 'claws', 'that', 'catch', 'beware', 'the', 'jubjub', 'bird', 'and', 'shun', 'the', 'frumious', 'bandersnatch', 'he', 'took', 'his', 'vorpal', 'sword', 'in', 'hand', 'long', 'time', 'the', 'manxome', 'foe', 'he', 'sought', 'so', 'rested', 'he', 'by', 'the', 'tumtum', 'tree', 'and', 'stood', 'awhile', 'in', 'thought', 'and', 'as', 'in', 'uffish', 'thought', 'he', 'stood', 'the', 'jabberwock', 'with', 'eyes', 'of', 'flame', 'came', 'whiffling', 'through', 'the', 'tulgey', 'wood', 'and', 'burbled', 'as', 'it', 'came', 'one', 'two', 'one', 'two', 'and', 'through', 'and', 'through', 'the', 'vorpal', 'blade', 'went', 'snicker-snack', 'he', 'left', 'it', 'dead', 'and', 'with', 'its', 'head', 'he', 'went', 'galumphing', 'back', '"and', 'has', 'thou', 'slain', 'the', 'jabberwock', 'come', 'to', 'my', 'arms', 'my', 'beamish', 'boy', 'o', 'frabjous', 'day', 'callooh', 'callay', 'he', 'chortled', 'in', 'his', 'joy', '`twas', 'brillig', 'and', 'the', 'slithy', 'toves', 'did', 'gyre', 'and', 'gimble', 'in', 'the', 'wabe', 'all', 'mimsy', 'were', 'the', 'borogoves', 'and', 'the', 'mome', 'raths', 'outgrabe']

如何返回与字符串中其他单词最不同的单词列表 - 基于与列表中所有其他单词的最小相似度和平均相似度值(作为 float )。

我完全不知道该怎么做。我想我需要使用 cossim(word1,word2) 函数来计算 'word1' 和 'word2' 之间的相似度,因为我们的讲师给了我们这个函数,但我不知道如何使用它。

def cossim(word1,word2):
"""Calculate the cosine similarity between the two words"""

# sub-function for constructing a letter vector from argument `word`
# which returns the tuple `(vec,veclen)`, where `vec` is a dictionary of
# characters in `word`, and `veclen` is the length of the vector
def wordvec(word):
    vec = defaultdict(int)  # letter vector

    # count the letters in the word
    for char in word:
        vec[char] += 1

    # calculate the length of the letter vector
    len = 0.0
    for char in vec:
        len += vec[char]**2

    # return the letter vector and vector length
    return vec,math.sqrt(len)

# calculate a vector,length tuple for each of `word1` and `word2`
vec1,len1 = wordvec(word1)
vec2,len2 = wordvec(word2)

# calculate the dot product between the letter vectors for the two words
dotprod = 0.0
for char in vec1:
    dotprod += vec1[char]*vec2[char]

# divide by the lengths of the two vectors
if dotprod:
    dotprod /= len1*len2

return dotprod

我应该从上面的列表中得到的答案应该是:

({'my'], 0.088487238234566931)

非常感谢任何帮助,

谢谢,

吉利

最佳答案

在使用像 Robert Rossney 建议的方法之前,需要先对单词列表进行重复数据删除。否则,结果数字将略有偏差,因为相同的 w 可以在一个 d[word] 中出现多次。

一种可能的方法是从列表中创建一个集合:

set_of_words = set(mylist)
differences = {}
for word in set_of_words:
    differences[word] = [cossim(word, word2) for word2 in set_of_words if word != word2]

这会创建一个字典,为每个单词分配一个列表,列出彼此之间的差异。

除了将这些列表直接分配给字典条目之外,您还可以将它们保存在循环内的一个变量中,并使用该变量计算 avg,就像 Robert 解决方案中建议的 afg 一样。

字典功能iteritems让你遍历 (key, value)-pairs 和 min函数有一个特殊的参数 key 来指定要最小化的内容,例如 key=lambda x: x[1] 按元组或列表的第二个元素排序。

关于python - 从字符串列表中,如何获得 python 中最奇怪的单词/字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10117386/

相关文章:

python - 扩展 Django 用户模型 OneToOne - 用户配置文件未保存

python - 最大路径三角形

python - 使用 Python 从列中获取唯一值

python - matplotlib 散点图中每个集合的不同颜色

python - 使用 numpy 进行下采样

python - 更新 ConfigParser 中的部分(或替代方案)

python - Cython 字符串连接超慢;它还有什么不好的地方?

python - Python中Powerset的时间复杂度

python - 如果特定字符位于与 Python 中的模式匹配的字符串中,则替换该特定字符

python - py2exe Bundle=1 应用程序崩溃。 Tkinter