Reputation: 105
I've browsed related questions but couldn't find one that exactly matched my use case.
I have created two lists (plist
, rlist
):
list1 = [str(x).zfill(2) for x in range(101)]
plist = ['p{0}'.format(element) for element in list1]
rlist = [str(x).zfill(2) for x in range(51)]
rlist = ['r{0}'.format(element) for element in rlist]
Each list looks like this:
>>> plist
['p00', 'p01', 'p02', 'p03'...'p100']
>>> rlist
['r00', 'r01', 'r02', 'r03'...'r50']
I have set up some nested for loops to create permutations of the list elements. The idea is to take each element of the plist
and combine it with each and every element of the rlist
. The process repeats until the plist
is exhausted.
for pitem in range(len(plist)):
vsdata = []
for ritem in range(pitem, len(rlist)):
if pitem == ritem:
print("skipped")
else:
item1 = plist[pitem]
item2 = rlist[ritem]
vsdata.append([str(item1),str(item2)])
print(item1 + " and " + item2)
Which yields:
skipped
p00 and r01
p00 and r02
p00 and r03
p00 and r04
p00 and r05
p00 and r06
p00 and r07
p00 and r08
p00 and r09
p00 and r10
...
p01 and r02
p01 and r03
p01 and r04
p01 and r05
p01 and r06
p01 and r07
p01 and r08
p01 and r09
p01 and r10
You'll notice I've also declared the empty list vsdata
before the nested loop. This list should be appended with sets of permutations (eg. the first plist
element combined with all the rlist
elements) and saved, before the nested loop begins again and vsdata
is overwritten with all the combinations involving the second element of plist
.
Thus, I need to 'export' vsdata
before this happens and name it using the relevant element of plist
.
For example, after the second loop has run twice I should have two vsdata
lists;
>>> vsdata_p00
[['p00', 'r02'], ['p00', 'r03'], ['p00', 'r04'], ['p00', 'r05'], ['p00', 'r06'], ['p00', 'r07'], ['p00', 'r08'], ['p00', 'r09'], ['p00', 'r10'] ...
>>> vsdata_p01
[['p01', 'r02'], ['p01', 'r03'], ['p01', 'r04'], ['p01', 'r05'], ['p01', 'r06'], ['p01', 'r07'], ['p01', 'r08'], ['p01', 'r09'], ['p01', 'r10'] ...
I've read about dictionaries and sets but can't fully understand how they might be implemented here. Ultimately I'd like all this data exported into a csv. Any help appreciated.
Upvotes: 1
Views: 77
Reputation: 15433
As suggested before, you can use a dictionary. Also, everything can be done in one line (sacrificing a bit of readability):
vsdata = {'p'+str(p).zfill(2):[['p'+str(p).zfill(2), 'r'+str(r).zfill(2)] for r in range(p+1,51)] for p in range(101)}
print vsdata['p37']
# [['p37', 'r38'], ['p37', 'r39'], ['p37', 'r40'], ['p37', 'r41'], ['p37', 'r42'], ['p37', 'r43'], ['p37', 'r44'], ['p37', 'r45'], ['p37', 'r46'], ['p37', 'r47'], ['p37', 'r48'], ['p37', 'r49'], ['p37', 'r50']]
Upvotes: 0
Reputation: 35803
Something like this with a dictionary might work:
vsdatas = {}
for pitem in range(len(plist)):
vsdata = []
for ritem in range(pitem, len(rlist)):
if pitem == ritem:
print("skipped")
else:
item1 = plist[pitem]
item2 = rlist[ritem]
vsdata.append([str(item1),str(item2)])
print(item1 + " and " + item2)
vsdatas[plist[pitem]] = vsdata
To get the list of permutations for "p01"
, for example, just access vdatas["p01"]
.
In general, you don't want to dynamically create and name variables.
Upvotes: 2