如何改变变量列表的索引?

时间:2016-05-10 08:09:52

标签: python

如何使用此代码和变量列表来清除索引列表?

#turns string to list
list=[]
#gets the sentence to count and what to count and deletes punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
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)

1 个答案:

答案 0 :(得分:0)

以下代码对我有用。你在使用random.shuffle时遇到了麻烦,但我在这里工作了。请记住,它不会返回任何内容,因此您必须先使用它,然后在洗牌后打印。

import random

#turns string to list
list=[]
#gets the sentence to count and what to count and deletes punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
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)

编辑:这可能会使其成为一个重复的问题,如评论中所述。