鉴于我有:
- 单词列表
- 表示每个单词“简单”的分数/分数
- 每个单词的难度等级:
例如
>>> words = ['apple', 'pear', 'car', 'man', 'average', 'older', 'values', 'coefficient', 'exponential']
>>> points = ['9999', '9231', '8231', '5123', '4712', '3242', '500', '10', '5']
>>> bins = [0, 0, 0, 0, 1, 1, 1, 2, 2]
目前,单词列表按简单点
排序。
如果我想将简单性建模为“二次曲线”怎么办?,即从最高点到最低点然后再回到最高点,即生成一个看起来像这样的单词列表对应点:
['apple', 'pear', 'average', 'coefficient', 'exponential', 'older', 'values', 'apple', 'pear']
我已经试过了,但它太疯狂了:
>>> from collections import Counter
>>> Counter(bins)[0]
4
>>> num_easy, num_mid, num_hard = Counter(bins)[0], Counter(bins)[1], Counter(bins)[2]
>>> num_easy
4
>>> easy_words = words[:num_easy]
>>> mid_words = words[num_easy:num_easy+num_mid]
>>> hard_words = words[-num_hard:]
>>> easy_words, mid_words, hard_words
(['apple', 'pear', 'car', 'man'], ['average', 'older', 'values'], ['coefficient', 'exponential'])
>>> easy_1 = easy_words[:int(num_easy/2)]
>>> easy_2 = easy_words[len(easy_1):]
>>> mid_1 = mid_words[:int(num_mid/2)]
>>> mid_2 = mid_words[len(mid_1):]
>>> new_words = easy_1 + mid_1 + hard_words + mid_2 + easy_1
>>> new_words
['apple', 'pear', 'average', 'coefficient', 'exponential', 'older', 'values', 'apple', 'pear']
想象一下。 bins > 3 或者我想“点”单词以适应正弦曲线。
请注意,这不完全是一个 nlp
问题,也与“zipf”分布和创建匹配或重新排序单词排名的内容无关。
假设有一个整数列表,您有一个对象(在本例中是一个词)映射到每个整数,并且您想要重新排序对象列表以适合二次曲线。
最佳答案
我会按照这些思路做某事。按点对单词进行排序,取出每一秒,将那一半反转并连接两部分:
>>> s = sorted(zip(map(int, points), words))
>>> new_words = [word for p, word in list(reversed(s[::2])) + s[1::2]]
# If you have lots of words you'll be better off using some
# itertools like islice and chain, but the principle becomes evident
>>> new_words
['apple', 'car', 'older', 'values', 'exponential', 'coefficient', 'average', 'man', 'pear']
顺序如下:
[(9999, 'apple'), (8231, 'car'), (4712, 'older'), (500, 'values'), (5, 'exponential'), (10, 'coefficient'), (3242, 'average'), (5123, 'man'), (9231, 'pear')]
关于python - 根据分数重新排列列表项以适应函数曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42549212/