There is a txt file with this content: (lists)

x = ['python', 'c++', 'JavaScript', 'c#', 'Pascal', 'bash', 'shell', 'AutoIt'] y = ['windows', 'ubuntu', 'mac', 'android', 'vista', 'xp', 'win2000'] z = ['fara', 'rama', 'goga', 'google', 'zaza', 'faza', 'dura', 'puma'] 

etc. (there are many lists)

It is necessary to randomly select one of the variables, from it, also choose randomly 4 words so that they do not repeat between themselves, and the first word should be as in the beginning of the list. When choosing a variable so that there are no repetitions too

How to solve the problem?

  • 2
    And what can not be done? Did you try to write something to solve the problem? - Avernial
  • one
    In this form, your question is not very useful: either break it into subtasks that are more universal (if you don’t understand what it is, ask how you can break up your question into subtasks) or give an attempt to solve (code) that almost works. - jfs
  • at least indicate how the data is stored in the file, if there are many lists, it may make sense to make a list of lists, and not to store everything in separate variables? - Flowneee
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

 import random x = ['python', 'c++', 'JavaScript', 'c#', 'Pascal', 'bash', 'shell', 'AutoIt'] y = ['windows', 'ubuntu', 'mac', 'android', 'vista', 'xp', 'win2000'] z = ['fara', 'rama', 'goga', 'google', 'zaza', 'faza', 'dura', 'puma'] all_data = [x, y, z] for i in random.sample(all_data, 1): data = i[0] data1 = i[1:] q = random.sample(data1, 3) print(data + '\n' + '\n'.join(q)) 

    Here is the function that will return the generator, a list or a tuple of lists with strings (or whatever you need to choose there), as well as the number of elements you need to select are input:

     import random as r def get_random(lists, k): choice = r.choice(lists) if (k <= len(choice)): indexes = r.sample(range(0, len(choice)) , k) else: raise IndexError indexes.sort() for i in indexes: yield c[i] 

    Use for example:

     a = [i for i in get_random([x, y, z], 4)] 

      Easy

       x = ['python', 'c++', 'JavaScript', 'c#', 'Pascal', 'bash', 'shell', 'AutoIt'] y = ['windows', 'ubuntu', 'mac', 'android', 'vista', 'xp', 'win2000'] z = ['fara', 'rama', 'goga', 'google', 'zaza', 'faza', 'dura', 'puma'] import random a = random.choice(["x","y","z"]) value = [] if a == "x": a = random.sample(['python', 'c++', 'JavaScript', 'c#', 'Pascal', 'bash', 'shell', 'AutoIt'],4) value.append(x[0]) value.append(a) print("Выбран массив x") print("Возвращаю массив", value) elif a == "y": a = random.sample(['windows', 'ubuntu', 'mac', 'android', 'vista', 'xp', 'win2000'],4) value.append(y[0]) value.append(a) print("Выбран массив y") print("Возвращаю массив", value) elif a == "z": a = random.sample(['fara', 'rama', 'goga', 'google', 'zaza', 'faza', 'dura', 'puma'],4) value.append(z[0]) value.append(a) print("Выбран массив z") print("Возвращаю массив", value) a = input() 

      If you want to take lists from a separate file, I think you add it yourself, and in order not to repeat, you can use sets, but here from the fact that you gave no repetitions. (Although my code is very ugly, but if I understood the task correctly, it works ( 3.5), I would be grateful if someone writes beautifully)