Random to get a list from the file.



  • Got it. txt Content file:

    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.

    One of the variables should be selected at home early, and four words should be chosen from him, too, so as not to be repeated, and the first word should be at the beginning of the list. If you choose a variable, there's no repetition.

    What's the challenge?



  • This is the function that will return the generator, a list or a corrigendum of lists with the lines (or what you need to choose) and the number of elements that need to be selected:

    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 such as:

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


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2