本質上,我必須采用一個預先存在的串列和一個百分比,然后回傳一個新串列,其中包含新串列中第一個串列中給定百分比的專案。我有以下內容:
def select_stop_words(percent, list):
possible_stop_words = []
l = len(list)
new_words_list = l//(percent/100)
x = int(new_words_list - 1)
possible_stop_words = [:x]
return possible_stop_words
但這總是產生與第一個相同的結果。幫助??
uj5u.com熱心網友回復:
代替
new_words_list = l//(percent/100)
和
new_words_list = l * percent/100
給定percent<=100,new_words_list >= len(lst)現在。
uj5u.com熱心網友回復:
您可能要乘 l到percent / 100:
def select_stop_words(percent, lst):
return lst[:len(lst) * percent // 100]
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(select_stop_words(50, lst)) # [1, 2, 3, 4, 5]
print(select_stop_words(20, lst)) # [1, 2]
print(select_stop_words(99, lst)) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(select_stop_words(100, lst)) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/376188.html
