0

我是编程新手,在简化代码时遇到了一些问题。我对前面的部分没有问题,只需要知道如何简化这段代码。我希望下面的代码也可以在“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]
4

2 回答 2

5

无论no1-no30是什么,您都需要将它放在一个序列对象中,例如 a list,然后遍历该序列对象以产生输出,您还将将其放入一个序列对象中(在这种情况下,我认为 adict将是最好的)。

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],而不是访问 ,依此类推。

于 2013-08-19T03:30:28.527 回答
0

似乎您的“simrecnoXXindsYY”变量可以表示为二维数组simrec[n, i](n->no,i->inds)。以同样的方式,“recnoXXindsYY”可以变成 just 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) 的所有可能值循环。

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]
于 2013-08-19T03:35:48.213 回答