Python 如何洗牌变量列表的索引?

Python 如何洗牌变量列表的索引?,python,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()

如何洗牌索引列表?使用此代码和变量列表

#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)
#将字符串转换为列表
列表=[]
#获取要计数的句子以及要计数的内容,并删除标点符号
标点符号=“”!()-[]{};:'"\,./?@#$%^&*_~'''
句子=输入(“输入要计数的句子:”)
句子=句子。较低()
否_punt=“”
对于句子中的字符:
如果字符不在标点符号中:
无点=无点+字符
列表=无点分割()
#这表明它是否存在
索引=[索引+1表示索引,枚举中的列表(列表)]
打印(“索引为”,索引)

以下代码对我有效。您在使用random.shuffle时遇到问题,但我在这里使用了它。请记住,它不会返回任何内容,因此您必须先使用它,然后在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)
随机导入
#将字符串转换为列表
列表=[]
#获取要计数的句子以及要计数的内容,并删除标点符号
标点符号=''!()-[]{};:'',./?@$%^&*.''
句子=输入(“输入要计数的句子:”)
句子=句子。较低()
否_punt=“”
对于句子中的字符:
如果字符不在标点符号中:
无点=无点+字符
列表=无点分割()
#这表明它是否存在
索引=[索引+1表示索引,枚举中的列表(列表)]
打印(“索引为”,索引)
打印(索引)
随机。随机(索引)
打印(索引)

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

这不是重复的问题,因为这不起作用,但谢谢谢谢谢谢谢谢我现在就试试。非常感谢,它起了作用