1

I have a list of lists with a certain range:

l = [["this", "is", "a"], ["list", "of"], ["lists", "that", "i", "want"], ["to", "copy"]] 

And a list of words:

words = ["lorem", "ipsum", "dolor", "sit", "amet", "id", "sint", "risus", "per", "ut", "enim", "velit", "nunc", "ultricies"] 

I need to create an exact replica of the list of lists, but with random terms picked from the other list.

This was the first thing that came to mind, but no dice.

for random.choice in words: for x in list: for y in x: y = random.choice 

Any ideas? Thank you in advance!

9
  • 5
    Please show your two input lists, plus some sample output. Right now it's a little difficult to understand what you want to do. Commented Jul 6, 2017 at 16:58
  • Isn't this just shuffling? Commented Jul 6, 2017 at 17:00
  • you do realize that == is an equality check, not an assignment, right? Commented Jul 6, 2017 at 17:01
  • Do you mean you want to shuffle your list? If you pick random items from your original list, and put them in another lists, it sounds like they will not be exact replicas. Commented Jul 6, 2017 at 17:01
  • @Rawing, sorry, done! Commented Jul 6, 2017 at 17:20

3 Answers 3

4

You can use list comprehensions for this:

import random my_list = [[1, 2, 3], [5, 6]] words = ['hello', 'Python'] new_list = [[random.choice(words) for y in x] for x in my_list] print(new_list) 

Output:

[['Python', 'Python', 'hello'], ['Python', 'hello']] 

This is equivalent to:

new_list = [] for x in my_list: subl = [] for y in x: subl.append(random.choice(words)) new_list.append(subl) 

With your example data:

my_list = [['this', 'is', 'a'], ['list', 'of'], ['lists', 'that', 'i', 'want'], ['to', 'copy']] words = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'id', 'sint', 'risus', 'per', 'ut', 'enim', 'velit', 'nunc', 'ultricies'] new_list = [[random.choice(words) for y in x] for x in my_list] print(new_list) 

Output:

[['enim', 'risus', 'sint'], ['dolor', 'lorem'], ['sint', 'nunc', 'ut', 'lorem'], ['ipsum', 'amet']] 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mike, works like a charm. I had the right direction, but with poor execution :)
1

You're not storing the values back into your lists. Try:

for i in range(0, len(list)): subl = list[i] for n in range(0, len(subl)): list[i][n] = random.choice(words) 

Comments

1

You should flatten your list of lists, then shuffle, then rebuild. Example:

import random def super_shuffle(lol): sublist_lengths = [len(sublist) for sublist in lol] flat = [item for sublist in lol for item in sublist] random.shuffle(flat) pos = 0 shuffled_lol = [] for length in sublist_lengths: shuffled_lol.append(flat[pos:pos+length]) pos += length return shuffled_lol print super_shuffle([[1,2,3,4],[5,6,7],[8,9]]) 

Prints:

[[7, 8, 5, 6], [9, 1, 3], [2, 4]] 

This randomizes across ALL the lists, not just within a single sublist and guarantees no dups.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.