如何简化这个糟糕的代码?

时间:2013-08-19 03:21:16

标签: python numpy

我是编程的新手,并且在简化代码方面遇到了一些问题。我对此之前的部分没有任何问题,只需要知道如何简化这段代码。我希望下面的代码也可以在“11”的位置绘制12,18,20,27和28。 我非常感谢你的帮助!

simrecno1inds11 = nonzero(datasim11[:,1]==no1)[0]
simrecno2inds11 = nonzero(datasim11[:,1]==no2)[0]
simrecno3inds11 = nonzero(datasim11[:,1]==no3)[0]
simrecno4inds11 = nonzero(datasim11[:,1]==no4)[0]
simrecno5inds11 = nonzero(datasim11[:,1]==no5)[0]

simrecno7inds11 = nonzero(datasim11[:,1]==no7)[0]
simrecno8inds11 = nonzero(datasim11[:,1]==no8)[0]
simrecno9inds11 = nonzero(datasim11[:,1]==no9)[0]
simrecno10inds11 = nonzero(datasim11[:,1]==no10)[0]
simrecno11inds11 = nonzero(datasim11[:,1]==no11)[0]
simrecno12inds11 = nonzero(datasim11[:,1]==no12)[0]
simrecno13inds11 = nonzero(datasim11[:,1]==no13)[0]
simrecno14inds11 = nonzero(datasim11[:,1]==no14)[0]
simrecno15inds11 = nonzero(datasim11[:,1]==no15)[0]
simrecno16inds11 = nonzero(datasim11[:,1]==no16)[0]
simrecno17inds11 = nonzero(datasim11[:,1]==no17)[0]
simrecno18inds11 = nonzero(datasim11[:,1]==no18)[0]
simrecno19inds11 = nonzero(datasim11[:,1]==no19)[0]
simrecno20inds11 = nonzero(datasim11[:,1]==no20)[0]
simrecno21inds11 = nonzero(datasim11[:,1]==no21)[0]
simrecno22inds11 = nonzero(datasim11[:,1]==no22)[0]
simrecno23inds11 = nonzero(datasim11[:,1]==no23)[0]
simrecno24inds11 = nonzero(datasim11[:,1]==no24)[0]
simrecno25inds11 = nonzero(datasim11[:,1]==no25)[0]
simrecno26inds11 = nonzero(datasim11[:,1]==no26)[0]
simrecno27inds11 = nonzero(datasim11[:,1]==no27)[0]
simrecno28inds11 = nonzero(datasim11[:,1]==no28)[0]
simrecno29inds11 = nonzero(datasim11[:,1]==no29)[0]
simrecno30inds11 = nonzero(datasim11[:,1]==no30)[0]

recno1inds11 = nonzero(data11[:,1]==no1)[0]
recno2inds11 = nonzero(data11[:,1]==no2)[0]
recno3inds11 = nonzero(data11[:,1]==no3)[0]
recno4inds11 = nonzero(data11[:,1]==no4)[0]
recno5inds11 = nonzero(data11[:,1]==no5)[0]

recno7inds11 = nonzero(data11[:,1]==no7)[0]
recno8inds11 = nonzero(data11[:,1]==no8)[0]
recno9inds11 = nonzero(data11[:,1]==no9)[0]
recno10inds11 = nonzero(data11[:,1]==no10)[0]
recno11inds11 = nonzero(data11[:,1]==no11)[0]
recno12inds11 = nonzero(data11[:,1]==no12)[0]
recno13inds11 = nonzero(data11[:,1]==no13)[0]
recno14inds11 = nonzero(data11[:,1]==no14)[0]
recno15inds11 = nonzero(data11[:,1]==no15)[0] 
recno16inds11 = nonzero(data11[:,1]==no16)[0]
recno17inds11 = nonzero(data11[:,1]==no17)[0]
recno18inds11 = nonzero(data11[:,1]==no18)[0]
recno19inds11 = nonzero(data11[:,1]==no19)[0]
recno20inds11 = nonzero(data11[:,1]==no20)[0]
recno21inds11 = nonzero(data11[:,1]==no21)[0] 
recno22inds11 = nonzero(data11[:,1]==no22)[0]
recno23inds11 = nonzero(data11[:,1]==no23)[0]
recno24inds11 = nonzero(data11[:,1]==no24)[0]
recno25inds11 = nonzero(data11[:,1]==no25)[0]
recno26inds11 = nonzero(data11[:,1]==no26)[0]
recno27inds11 = nonzero(data11[:,1]==no27)[0]
recno28inds11 = nonzero(data11[:,1]==no28)[0]
recno29inds11 = nonzero(data11[:,1]==no29)[0]
recno30inds11 = nonzero(data11[:,1]==no30)[0]

2 个答案:

答案 0 :(得分:5)

无论no1 - no30是什么,你需要把它放在一个序列对象中,比如list,然后遍历那个序列对象来产生输出,你也会放入序列对象(在这种情况下,我认为dict最好)。

nos = [no1, no2, ..., no30]
simrecno_inds11 = {}
recno_inds11 = {}
exclude_nums = [6]
for k, no in enumerate(nos):
    if k in exclude_nums:
        continue
    simrecno_inds11[k] = nonzero(datasim11[:,1]==no)[0]
    recno28inds11[k] = nonzero(data11[:,1]==no)[0]

现在,您将访问simrecno17inds11,而不是访问simrecno_inds11[17],等等。

答案 1 :(得分:0)

您的“simrecnoXXindsYY”变量似乎可以在2D数组simrec[n, i](n-> no,i-> inds)中表示。以同样的方式,“recnoXXindsYY”可以变为rec[n,i]。最后,“noXX”可以是一维数组no[n]。所以你的作业有以下形式:

simrec[n, i] = nonzero(datasim[i][:,1]==no[n])[0]
rec[n, i] = nonzero(data[i][:,1]==no[n])[0]

因此,您需要循环使用n(nos)和i(索引)的所有可能值。

indexes = [11, 12, 18, 20, 27, 28]
nos = range(1,31)

for i in index:
    for n in nos:
        simrec[n, i] = nonzero(datasim[i][:,1]==no[n])[0]
        rec[n, i] = nonzero(data[i][:,1]==no[n])[0]