2016-05-10 41 views
-2

如何降低索引列表?用此代碼和變量列表我該如何洗牌變量列表的索引?

#turns string to list 
list=[] 
#gets the sentence to count and what to count and deletes punctuation 
punctuations = '''!()-[]{};:'"\,<>./[email protected]#$%^&*_~''' 
sentence=input("Enter sentence to count: ") 
sentence=sentence.lower() 

no_punct = "" 
for char in sentence: 
    if char not in punctuations: 
     no_punct = no_punct + char 

list=no_punct.split() 

#this shows whether it is there or not 
index=[index+1 for index,list in enumerate(list)] 
print("the indices are ",index) 
+0

它不是一個重複的,因爲這沒有工作,但謝謝 –

回答

0

以下代碼適用於我。 random.shuffle有問題,但我在這裏工作。記住它不會返回任何東西,所以你必須先使用它,然後在洗牌後打印。

import random 

#turns string to list 
list=[] 
#gets the sentence to count and what to count and deletes punctuation 
punctuations = '''!()-[]{};:'"\,<>./[email protected]#$%^&*_~''' 
sentence=input("Enter sentence to count: ") 
sentence=sentence.lower() 

no_punct = "" 
for char in sentence: 
    if char not in punctuations: 
     no_punct = no_punct + char 

list=no_punct.split() 

#this shows whether it is there or not 
index=[index+1 for index,list in enumerate(list)] 
print("the indices are ",index) 

print(index) 
random.shuffle(index) 
print(index) 

編輯:這可能會使它成爲一個重複的問題,如評論中所述。

+0

謝謝,我現在要嘗試它 –

+0

非常感謝你,它的工作 –