Random without repetition in Python

前端 未结 3 1234
遥遥无期
遥遥无期 2020-12-12 03:50

I want to write a program that displays all the elements of a list in random order without repetition. It seems to me that it should work, but only prints those elements wi

3条回答
  •  鱼传尺愫
    2020-12-12 04:18

    import random
    items = ["house", "word", "computer", "table", "cat", "enter", "space"]
    tab= []
    while len(tab) < len(items):
        item = random.choice(items)
        if item not in tab:
        tab.append(item)
    print(tab)
    
        
    

提交回复
热议问题