2016-05-10 34 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

非常感谢你,它的工作 –