how do I shuffle the index of a variable list?
本问题已经有最佳答案,请猛点这里访问。
如何无序排列索引列表?使用此代码和变量列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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) |
编辑:这可能会使它成为一个重复的问题,如评论中所提到的。