Reputation: 1295
I have a list that looks like this:
important stuff = [1, 1, '539287_640214358329_682457984_n.jpg',
1, 2, '10273290_745672065239_6510327149011099172_o.jpg',
1, 3,'196453_640214498049_2103349152_n.jpg',
1, 4, '1277816_699439470729_877539164_o.jpg',
1, 5, '10682279_777090163119_2043260231272895742_o.jpg',
1, 6,'736323_656687181659_1199237329_o.jpg',
1, 7, '185184_640214403239_1313590472_n.jpg',
1, 8, '1898786_730004488189_837817798_o.jpg']
I need a way to shuffle it keeping the "rows" (aka every 3 values) constant. The two numbers need to stay associated with the same .jpg.
Is the best way to do this to create a list of lists? how would that work? I found answers to creating a flat list from a list of lists, but I need to go in the opposite direction.
Upvotes: 1
Views: 63
Reputation: 4580
import random
# list of lists
data = [data[x:x+3] for x in range(0, len(data),3)]
# shuffle.
random.shuffle(data,random.random)
# OR keep them in dictionary with filename as key.
foo = {}
for i in data:
foo[i[2]] = i[:2]
print foo
It's really your choice.
Personally I would keep it as dictionary for fast look up and organization.
Upvotes: 1
Reputation: 392
A list of lists is probably one of the easier ways of handling this. Assuming your initial list is properly formatted according to your description (two numbers to one string), you could do something like this:
from random import shuffle
listOfLists = []
for i in range(0,len(importantStuff)/3):
listOfLists.append([importantStuff[i*3+0],importantStuff[i*3+1],importantStuff[i*3+2]])
shuffle(listOfLists)
singleList = listOfLists[0]
singleItem = listOfLists[0][2]
For more generic cases use variables instead of hardcoded values
Upvotes: 1