python - 根据分数重新排列列表项以适应函数曲线

标签 python list sorting curve

鉴于我有:

  • 单词列表
  • 表示每个单词“简单”的分数/分数
  • 每个单词的难度等级:

例如

>>> 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/

相关文章:

python - 如何用Python实现字典列表的多级排序?

javascript - 如何将第二次点击添加到按钮纯javascript

C# 快速排除排序列表的方法

javascript - jQuery - 将列表项的元素 move 到同一项目的另一个元素内+对列表中的每个项目执行此操作

php - 动态评分表

python - 我有这个代码。它应该使用 sys.argv[5] 中的评级对段文件进行分类。但它一直有错误

Python:根据特定条件对列表进行重复数据删除

python - 如何创建形状为 (0, 2) 的 numpy 数组?

带有 Stripe 的 Python : How do I get valid JSON from a charge response?

Python - 搜索字典