又遇到了想不明白的问题,这个list怎么莫名其妙的多了一层


def Z50(inputs:str,w:list=[[1.090,-0.2],[0.725,1],[1.5,-2]]):
    output=[]
    data=[]
    l=len(w)
    r=True
    i=len(inputs)
    while i!=0:
        i=i-1
        data.append(ord(inputs[i]))
        output.append(ord(inputs[i]))
    data.reverse()
    while l!=0:
        l=l-1
        i=len(inputs)
        if r:
            a=i
        while i!=0:
            i=i-1
            if r:
                output[a-i-1]=float((data[a-i-1]+output[a-i-1])/float(2)*float(w[l][0])+float(w[l][1]))
            else:
                output[i]=(data[i]+output[i])/2*w[l][0]+w[l][1]
        output,data=data,output
    del r,i,l,data,w
    i=len(inputs)
    while i!=0:
        i=i-1
        if output[i]>ord(inputs[i]):
            output[i]=True
        else:
            output[i]=False
    print(output)
    l=[]
    i=len(inputs)
    n=output[i-1]
    while i!=0:
        i=i-1
        if n==output[i]:
            pass
        else:
            n=not n
            l.append(i)
    del i,n,output
    return set(l)
def train(TS:str,Tick:int=10,wl:list=[]):
    if wl==[] or len(wl)<100:
        from random import uniform
        i=100
        while i!=0:
            wl.append([[[uniform(0.5,1.5),uniform(-2,2)],[uniform(0.5,1.5),uniform(-2,2)],[uniform(0.5,1.5),uniform(-2,2)]]])
            i=i-1
        del uniform
    ans=[]
    for i in range(len(TS)):
        if TS[i]==" ":
            ans.append(i)
    ans=set(ans)
    del i
    while Tick!=0:
        i=len(wl)
        i2=i
        sc=[]
        while i!=0:
            sc.append(ans&Z50(TS,wl[i2-i]))
            i=i-1
        bl=int(len(wl)/3)
        while bl>0:
            i=sc.count(max(sc))
            l=0
            while i!=0:
                l=sc.index(max(sc),l+1)
                sc[l]=True
                i=i-1
            bl=bl-sc.count(max(sc))
        del l
        i=0
        b=[]
        c=[]
        for a in sc:
            if sc[i]==True:
                b.append(i)
            else:
                c.append(i)
            i=i+1
        i=0
        from random import choice,uniform
        for a in c:
            if choice([True,False]):
                wl[c]=[[[uniform(0.5,1.5),uniform(-2,2)],[uniform(0.5,1.5),uniform(-2,2)],[uniform(0.5,1.5),uniform(-2,2)]]]
            else:
                wl[c]=[wl[choice(b)][0],wl[choice(b)][1],wl[choice(b)][2]]
        del c,b,i,a,l,sc,i2
        Tick=Tick-1
    return wl
def test(TS:str,wl:list):
    ans=[]
    for i in range(len(TS)):
        if TS[i]==" ":
            ans.append(i)
    a=0
    i=len(ans)
    while i!=0:
        i=i-1
        ans[i]=ans[i]-a
        a=a+1
    ans=set(ans)
    del a,i
    return ans&Z50(TS,wl[i2-i])

报错信息:

Microsoft Windows [版本 10.0.19045.3208]
(c) Microsoft Corporation。保留所有权利。

C:\Users\MIN\Desktop>python
Python 3.11.3 (tags/v3.11.3:f3909b8, Apr  4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Z50
>>> Z50.train("a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\MIN\Desktop\Z50.py", line 64, in train
    sc.append(ans&Z50(TS,wl[i2-i]))
                  ^^^^^^^^^^^^^^^^
  File "C:\Users\MIN\Desktop\Z50.py", line 20, in Z50
    output[a-i-1]=float((data[a-i-1]+output[a-i-1])/float(2)*float(w[l][0])+float(w[l][1]))
                                                             ^^^^^^^^^^^^^^
TypeError: float() argument must be a string or a real number, not 'list'
>>>

真令人头大

Z50函数中你使用了float(w[l][0])这样的表达式,但是w[l][0]是一个列表,而不是一个浮点数,不能将一个列表转换为一个浮点数
train函数中,使用了wl[c]=[[[uniform(0.5,1.5),uniform(-2,2)],[uniform(0.5,1.5),uniform(-2,2)],[uniform(0.5,1.5),uniform(-2,2)]]]这样的赋值语句,但是wl[c]是一个列表,不是一个整数。不能用一个列表来作为另一个列表的索引,你修改下