patternpythonMinor
Apply one random deduction pattern onto different lists with same index in Python
Viewed 0 times
randomsamewithapplydeductiondifferentoneontopythonlists
Problem
I am trying to apply certain random deduction pattern to different lists - different in items but having the same numbers of items. I wrote this code - I know it is pretty messy- but it seems it does the work. Please help me to make it better. To explain, better the base list changes each time the random pattern applied to the list. But the indexes that are deducted each time are from the same previous deducted list.
For example the index list I get is the following each time I deduct:
-
Index 1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
-
Index 2 = [0, 2, 5, 8, 9, 10, 12, 16, 19, 20, 21, 22, 24, 26, 28]
-
Index 3 = [0, 2, 5, 12, 19, 20, 21, 22, 24, 26]
-
Index 4 = [2, 5, 19, 20, 21, 22, 24]
-
Index 5 = [5, 19, 20, 21, 22, 24]
-
Index 6 = [19, 20, 21, 22, 24]
List (
import random as rm
num = 30
n = 7
slp = 1
def main():
indxLst = []
L = [x for x in xrange(num)]
index = [x for x in xrange(num)]
indexLst = []
for e in range(1,n):
lChng = [ L[y]+e for y in xrange(num)]
get = int((num/e)* slp)
index = rm.sample(index,get)
index.sort()
print "Index %d ="%e,index
indexLst.append(index)
lChng = [lChng[x] for y, x in enumerate(indexLst[e-1]) ]
lChng.sort()
print "List %d ="%e,lChng
main()For example the index list I get is the following each time I deduct:
-
Index 1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
-
Index 2 = [0, 2, 5, 8, 9, 10, 12, 16, 19, 20, 21, 22, 24, 26, 28]
-
Index 3 = [0, 2, 5, 12, 19, 20, 21, 22, 24, 26]
-
Index 4 = [2, 5, 19, 20, 21, 22, 24]
-
Index 5 = [5, 19, 20, 21, 22, 24]
-
Index 6 = [19, 20, 21, 22, 24]
List (
n) is deducted from list (n-1)Solution
The code is not bad, but is a little redundant and messy.
Some comments:
-
There is no need to shorten the included module
-
If you are going to use global variables (module level variables) at least they should be in uppercase. It makes more easy to understand the code when reading it because you don't have to look for the variable in the code. You know they're up there. Another option is reading those variables from, say, a file if you plan to apply the script to multiples instances of the problem.
-
-
-
The initialization for
-
The code should contain "some kind of logic flow". In the revised code I order it like: "calculate index; show index", "calculate Lchg; show Lchg".
-
The name of
-
-
Last. This two lines:
Can be replaced by:
Since you are puting
Revised code:
Some comments:
-
There is no need to shorten the included module
random because it's already short and legible.-
If you are going to use global variables (module level variables) at least they should be in uppercase. It makes more easy to understand the code when reading it because you don't have to look for the variable in the code. You know they're up there. Another option is reading those variables from, say, a file if you plan to apply the script to multiples instances of the problem.
-
indxLst = [] it's declared but not used.-
xrange() it's good for looping because it generates the new value on demand, but there is no point in using it if you're going to exhaust it right away (i.e. L = [x for x in xrange(num)] that is equivalent to L = range(num)). On the other hand, in the only place you are using range(1, n) that's the place you should be using xrange() because you're on a loop and there is no need to generate the whole range right away, you have time until you reach the last element.-
The initialization for
L and index it's the same. In this case you could initialize them both to the same object using the syntax: x = y = obj. But it's a minor thing and can be troublesome since both variables are pointing to the same obj.-
The code should contain "some kind of logic flow". In the revised code I order it like: "calculate index; show index", "calculate Lchg; show Lchg".
-
The name of
lChng should be something that relates to L, so possible names could be LChng, L_chng, L_tmp, etc. But the l in upper case. Also maybe you could improve the name of the variables. num it's generic and it doesn't tell you much, also index. But if the names are specified that way then it's okay.-
indexLst seems to be unuseful because you put list in there only to use the same list you put 2 lines afterwards. If you don't need to whole indexes lists stored there's no point in having it there.-
Last. This two lines:
lChng = [ L[y]+e for y in xrange(num)]
lChng = [lChng[x] for y, x in enumerate(indexLst[e-1]) ]Can be replaced by:
Lchg = [L[i]+e for i in index]Since you are puting
index in indexLst only to pick it up again by indexLst[e-1].Revised code:
import random
NUM = 30
N = 7
SLP = 1
def main():
L = index = range(NUM)
for e in xrange(1, N):
get = (NUM/e) * SLP
index = random.sample(index, get)
index.sort()
print "Index %d =" % e, index
Lchg = [L[i]+e for i in index]
Lchg.sort()
print "List %d =" % e, Lchg
main()Code Snippets
lChng = [ L[y]+e for y in xrange(num)]
lChng = [lChng[x] for y, x in enumerate(indexLst[e-1]) ]Lchg = [L[i]+e for i in index]import random
NUM = 30
N = 7
SLP = 1
def main():
L = index = range(NUM)
for e in xrange(1, N):
get = (NUM/e) * SLP
index = random.sample(index, get)
index.sort()
print "Index %d =" % e, index
Lchg = [L[i]+e for i in index]
Lchg.sort()
print "List %d =" % e, Lchg
main()Context
StackExchange Code Review Q#69033, answer score: 3
Revisions (0)
No revisions yet.