2017-03-02 22 views
4

考慮到我有:重新排列列表項目,以適應一個函數曲線

  • 點/分表示「簡約」爲每個單詞
  • 難度級別的列表每個字的:

例如

>>> 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'] 

我都試過,但它的痛苦瘋狂:

>>> 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'] 

想象一下沒有。的箱子大於3,或者我想「點」這些詞來適應正弦曲線。

請注意,這並不完全是一個nlp的問題,也沒有任何與'zipf'分配和創建的東西來匹配或重新排序單詞的排序。

想象一下,您有一個整數列表,您有一個對象(在這種情況下是一個單詞)映射到每個整數,並且您想重新排列對象列表以適合二次曲線。

+0

是'points'無關或爲'bin'值派生的點? – schwobaseggl

+0

箱子是從點派生的。 – alvas

+0

你提到「單詞列表是按照簡單的'points'排序的,」但是'points'在你的例子中看起來沒有排序,因爲它有子序列'5123','3242','4712'。我的理解有什麼不對嗎? – Rohanil

回答

2

我會這樣做。排序的點的話,採取一切秒掉,扭轉半Concat的兩個:

>>> 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')] 
2

排序成根據您的自定義標準,檢查其長度是否是奇數還是偶數,然後壓縮它,在兩個塊和反向後半:

>>> def peak(s): 
...  return s[::2]+s[-1-(len(s)%2)::-2] 
... 
>>> peak('112233445566778') 
'123456787654321' 
>>> peak('1122334455667788') 
'1234567887654321' 

注意凹凸形狀的信息可能會產生不對稱的結果:

>>> peak('11111123') 
'11123111' 
相關問題