鉴于我有:
E.g。
HashMap
目前,单词列表按简单>>> 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]
排序。
如果我想将简单性建模为"二次曲线"?,即从最高点到低点然后再回到高点,即产生一个单词列表,该怎么办?看起来像这样有相应的要点:
points
我试过这个,但是它很痛苦:
['apple', 'pear', 'average', 'coefficient', 'exponential', 'older', 'values', 'apple', 'pear']
想象一下没有。箱子是> 3或者我想要"点"这些词适合正弦曲线。
请注意,这并不是>>> 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']
问题,也不是与' zipf'分配和创建匹配或重新排序单词排名的东西。
想象一下,你有一个整数列表,你有一个对象(在本例中是一个单词)映射到每个整数,你想重新排序对象列表以适合二次曲线。
答案 0 :(得分:2)
根据您的自定义标准将其排序为列表,检查其长度是偶数还是奇数,然后将其压缩为两个块并反转后半部分:
>>> def peak(s):
... return s[::2]+s[-1-(len(s)%2)::-2]
...
>>> peak('112233445566778')
'123456787654321'
>>> peak('1122334455667788')
'1234567887654321'
请注意,不均匀的数据可能会产生不对称的结果:
>>> peak('11111123')
'11123111'
答案 1 :(得分:2)
我会沿着这些方向做。按照他们的分数对单词进行排序,每秒取出一半,然后反转这两个单词并将两者结合起来:
>>> 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')]