python未定义的元组的TypeError

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python未定义的元组的TypeError相关的知识,希望对你有一定的参考价值。

我正在制作我的python程序。下面是我的代码,它返回:

File "relation.py", line 134, in <module>
    k = z1[i][0] = sampling(a[l, :], b[l, :], z1_sum[i], alpha[l, :], k_l)
TypeError: list indices must be integers, not tuple

在我的代码(下面这个)中,我没有定义任何元组。所以我不知道这段代码的问题是什么。

import random
import numpy as np
import math



def sampling(a, b, I, alpha, length):
    summary = 0
    N = len(alpha)
    for i in range(N):
        A = math.gamma(a[i] + I[i]) * math.gamma(b[i] + length - I[i]) / math.gamma(a[i] + b[i] + I[i])
        B = math.gamma(a[i] + b[i]) / (math.gamma(a[i]) * math.gamma(b[i]))
        value = alpha[i] * A * B
        summary += value
        result.append(value)


    choose = random.randomrange(summary)

    num = N - 1

    for i in range(N):
        choose -= result[i]
        if choose < 0:
            num = i
            break

    return num

def delta(x, y):
    if x == y:
        return 1
    return 0

data = []

print("please input (k, l):")
num = map(int, raw_input().split())

k_l = k = num[0]
l_l = l = num[1]

param_p = [[0 for i in range(k)] for j in range(l)]
param_n = [[0 for i in range(k)] for j in range(l)]
param_k = [0] * k
param_l = [0] * l
count = 1000

print("please input (N1, N2):")
number = map(int, raw_input().split())
N1 = number[0]
N2 = number[1]

random.randint(50, 100)
z1 = []
z2 = []
z1_sum = [2 for i in range(N1)]
z2_sum = [2 for i in range(N2)]

value = 0

alpha = [[2 for i in range(k)] for j in range(l)]
a = np.array([[2 for i in range(k)] for j in range(l)])
b = np.array([[2 for i in range(k)] for j in range(l)])

for i in range(N1):
    z1.append([value, i],)
    if i % k == k - 1:
        value = 0
    else:
        value += 1

value = 0

for i in range(N2):
    z2.append([value, i],)
    if i % l == l - 1:
        value = 0
    else:
        value += 1

print(z1)
print(z2)

print("please input relation-data:")

for i in range(N2):
    line = map(int, raw_input().split())
    data.append(line)

print(data)

param_p[0][0] = -1
print(param_p[1][0])
param_p[0][0] = 0


for i in range(N2):
    l = z2[i][0]
    for j in range(N1):
        k = z1[j][0]
        z1_sum[i] += data[i][j]
        z2_sum[j] += data[i][j]
        param_p[l][k] += data[i][j]
        param_n[l][k] += 1 - data[i][j]
        param_k[k] += data[i][j]
        param_l[l] += data[i][j]        

print(param_p)
print(param_n)
print(param_k)
print(param_l)

#sampling(a, b, I, alpha, length)

k = 0
l = 0

for n in range(count):
    for i in range(N1):
        temp = z1[i][0]
        for j in range(l_l):
            alpha[j][temp] += param_l[j]
            a[j][temp] += param_p[j][temp]
            b[j][temp] += param_n[j][temp]

            I = delta(z1[i][0], temp)

            param_p[j][temp] -= z1_sum[i] * I
            param_n[j][temp] -= (k_l - z1_sum[i]) * I
            param_l[j] -= I

        k = z1[i][0] = sampling(a[l, :], b[l, :], z1_sum[i], alpha[l, :], k_l)
        for j in range(l_l):
            alpha[j][temp] -= param_l[j]
            a[j][temp] -= param_p[j][temp]
            b[j][temp] -= param_n[j][temp]

            I = delta(z1[i][0], k)

            param_p[j][k] += z1_sum[i] * z1_sum[i] * I
            param_p[j][k] += (k_l - z1_sum[i]) * z1_sum[i] * I
            param_l[j] += z1_sum[i] * I

    for i in range(N2):
        temp = z2[i][0]
        for j in range(k_l):
            alpha[temp][j] += param_k[j]
            a[temp][j] += param_p[temp][j]
            b[temp][j] += param_n[temp][j]

            I = delta(z2[i][0], temp)

            param_p[temp][j] -= z2_sum[i] * I
            param_n[temp][j] -= (l_l - z2_sum[i]) * I
            param_k[j] -= I

        l = z2[i][0] = sampling(a[:, k], b[:, k], z2_sum[i], alpha[:, k], l_l)
        for j in range(k_l):
            alpha[temp][j] -= param_k[j]
            a[temp][j] -= param_p[temp][j]
            b[temp][j] -= param_n[temp][j]

            I = delta(z2[i][0], l)

            param_p[l][j] += z2_sum[i] * I
            param_n[l][j] += (l_l - z2_sum[i]) * I
            param_k[j] += I

    print("Hello")  

print(z1)
print(z2)

正如你所看到的,我只定义了numpy并且[l,:]根本不是元组。保留所有,没有tuple.only列表元素的商务在这里。为什么python返回关于元组的错误。

答案

谢谢abarnert,我终于知道问题是什么了。这是因为当它实际上是一个numpy时我使用的是numpy-array

这是我的代码编辑

import random
import numpy as np
import math



def sampling(a, b, Ip, In, alpha, elements):
    #print("sampling")

    result = [] 

    print("a:", a)
    print("b:", b)
    print("alpha:", alpha)
    print("elements:", elements)
    print("Ip:", Ip)
    print("In:", In)


    summary = 0
    N = len(alpha)

    for i in range(N):

        A = 0
        B = 0
        value = 0

        '''     
        if i == elements:
            continue
        '''

        for j in range(elements):

            print("A:")
            print("1:", a[i][j] + Ip[j], ",2:", b[i][j] + In[j], ",3:", a[i][j] + b[i][j] + Ip[j] + In[j])
            A += math.lgamma(a[i][j] + Ip[j]) + math.lgamma(b[i][j] + In[j]) - math.lgamma(a[i][j] + b[i][j] + Ip[j] + In[j])
            print("B:")
            print("1:", a[i][j] + b[i][j], ",2:", a[i][j], ",3:", b[i][j])
            B += math.lgamma(a[i][j] + b[i][j]) - math.lgamma(a[i][j]) - math.lgamma(b[i][j])

            print("A:", A)
            print("B:", B)
            print("value:", value)

        value += math.log(alpha[i]) + A + B


        print("i:", i, "value:", value)
        summary += math.exp(value)
        result.append(math.exp(value))

    print("percentage:", np.array(result) / summary)


    #print("summary:", summary)

    choose = random.uniform(0.0, summary)

    #print("choose:", choose)

    num = N - 1

    for i in range(N):
        choose -= result[i]
        if choose <= 0:
            num = i
            break

    '''
    if num >= elements:
        num += 1
    '''

    return num

def delta(x, y):
    if x == y:
        return 1
    return 0

data = []

print("please input (k, l):")
num = map(int, raw_input().split())

k_l = k = num[0]
l_l = l = num[1]

param_p = np.array([[0 for i in range(k)] for j in range(l)])
param_n = np.array([[0 for i in range(k)] for j in range(l)])
param_k = np.array([0] * k)
param_l = np.array([0] * l)
count = 1000

print("please input (N1, N2):")
number = map(int, raw_input().split())
N1 = number[0]
N2 = number[1]

random.randint(50, 100)
z1 = []
z2 = []

z1_sum = np.array([[[0 for i in range(k)] for j in range(l)] for n in range(N1)])
z2_sum = np.array([[[0 for i in range(k)] for j in range(l)] for n in range(N2)])

z1_zero = np.array([[[0 for i in range(k)] for j in range(l)] for n in range(N1)])
z2_zero = np.array([[[0 for i in range(k)] for j in range(l)] for n in range(N2)])

value = 0
valuesL = []

for i in range(N2):
    valuesL.append(value)
    if i % l == l - 1:
        value = 0
    else:
        value += 1


valuesL = np.sort(np.array(valuesL))

for i in range(N2):
    z2.append([valuesL[i], i],)


tempAlpha = 0.5
tempA = 0.5
tempB = 0.5

alpha1 = np.array([tempAlpha for i in range(k)])
alpha2 = np.array([tempAlpha for i in range(l)])

a = np.array([[tempA for i in range(k)] for j in range(l)])
b = np.array([[tempB for i in range(k)] for j in range(l)])

value = 0
valuesK = []

for i in range(N1):
    valuesK.append(value)
    if i % k == k - 1:
        value = 0
    else:
        value += 1

valuesK = np.sort(np.array(valuesK))

for i in range(N1):
    z1.append([valuesK[i], i],)


z1 = np.array(z1)
z2 = np.array(z2)

print(z1)
print(z2)

print("please input relation-data:")

for i in range(N2):
    line = map(int, raw_input().split())
    data.append(line)

#print(data)


for i in range(N2):
    l = z2[i][0]
    for j in range(N1):
        k = z1[j][0]
        z1_sum[j][l][k] += data[i][j]
        z2_sum[i][l][k] += data[i][j]
        z1_zero[j][l][k] += 1 - data[i][j]
        z2_zero[i][l][k] += 1 - data[i][j]
        param_p[l][k] += data[i][j]
        param_n[l][k] += 1 - data[i][j]
        param_k[k] += data[i][j]
        param_l[l] += data[i][j]

#print("param_p:", param_p)
#print("param_n:", param_n)
#print("param_k:", param_k)
#print("param_l:", param_l)

#print("z1-sum:")
#print(z1_sum)
#print("z2-sum:")
#print(z2_sum)

#sampling(a, b, I, alpha, length, elements)

k = 0
l = 0

pastK = np.array([0 for i in range(N1)])
pastL = np.array([0 for i in range(N2)])

for n in range(count):

    pastK[:] = z1[:, 0] 

    for i in range(N1):   #sampling z1 objects


        t_k = z1[i][0]

        k = z1[i][0] = sampling((a[:, :] + param_p[:, :] - z1_sum[i, :, :]).T, (b[:, :] + param_n[:, :] - z1_zero[i, :, :]).T, np.sum(z1_sum[i, :, :], axis=1), np.sum(z1_zero[i, :, :], axis=1), alpha1[:] + param_k[:] - np.sum(z1_sum[i, :, :], axis=0), l_l)

        #print("t_k:", t_k, "k:", k)        


    for i in range(N1):

        t_k = pastK[i]
        k = z1[i][0]

        print("t_k:", t_k, "k:", k)

        if t_k != k: 


            #change param_p param_n param_k

            #print("old-z1:", z1)
            print("param_p(past):", param_p)
            print("param_n(past):", param_n)
            print("param_k(past):", param_k)

            z1_sum_temp = z1_sum[i, :, t_k]     #reduce order
            z1_zero_temp = z1_zero[i, :, t_k]
            summary = np.sum(z1_sum_temp)

            param_p[:, t_k] -= z1_sum_temp    #minus old value
            param_n[:, t_k] -= z1_zero_temp
            param_k[t_k] -= summary

            param_p[:, k] += z1_sum_temp        #plus new value
            param_n[:, k] += z1_zero_temp
            param_k[k] += summary


            print("param_p(now):", param_p)
            print("param_n(now):", param_n)
            print("param_k(now):", param_k)

            #print("past-z2-sum:", z2_sum)



            for j in range(N2):
                l_j = z2[j][0]
                z2_sum[j][l_j][t_k] -= data[j][i]
                z2_sum[j][l_j][k] += data[j][i]

                z2_zero[j][l_j][t_k] -= 1 - data[j][i]
                z2_zero[j][l_j][k] += 1 - data[j][i]

            #print("now-z2-sum:", z2_sum)
            #print("past-z1-sum:", z1_sum)


                #change z1_sum
            z1_sum[i, :, k] = z1_sum[i, :, t_k]
            z1_sum[i, :, t_k] = 0

            z1_zero[i, :, k] = z1_zero[i, :, t_k]
            z1_zero[i, :, t_k] = 0

            #print("now-z1-sum:", z1_sum)

            #print("new-z1:", z1)



    pastL[:] = z2[:, 0]


    for i in range(N2):   #sampling z2 objects

        t_l = z2[i][0]  

        l = z2[i][0] = sampling(a[:, :] + param_p[:, :] - z2_sum[i, :, :], b[:, :] + param_n[:, :] - z2_zero[i, :, :], np.sum(z2_sum[i, :, :], axis=0), np.sum(z2_zero[i, :, :], axis=0), alpha2[:] + param_l[:] - np.sum(z2_sum[i, :, :], axis=1), k_l)

        print("t_l:", t_l, "l:", l)



    for i in range(N2):

        t_l = pastL[i]
        l = z2[i][0]

        print("t_l:", t_l, "l:", l)

        if t_l != l:

            #change param_p param_n param_l

            #print("old-z2:", z2)
            z2_sum_temp = z2_sum[i, t_l, :]     #reduce order
            z2_zero_temp = z2_zero[i, t_l, :]
            summary = np.sum(z2_sum_temp)

            param_p[t_l, :] -= z2_sum_temp    #minus old value
            param_n[t_l, :] -= z2_zero_temp
            param_l[t_l] -= summary

            param_p[l, :] += z2_sum_temp        #plus new value
            param_n[l, :] += z2_zero_temp
            param_l[l] += summary


            #print("param_p(past):", param_p)
            #print("param_n(past):", param_n)
            #print("param_l(past):", param_l)


            print("param_p(now):", param_p)
            print("param_n(now):", param_n)
            print("param_l(now):", param_l)

            #print("past-z1-sum(past):", z1_sum)
            #print("past-z1-zero(past):", z1_zero)

            #change z1_sum

            for j in range(N1):
                k_j = z1[j][0]
                z1_sum[j][t_l][k_j] -= data[i][j]
                z1_sum[j][l][k_j] += data[i][j]

                z1_zero[j][t_l][k_j] -= 1 - data[i][j]
                z1_zero[j][l][k_j] += 1 - data[i][j]


            #print("now-z1-sum(now):", z1_sum)
            #print("past-z1-zero(now):", z1_zero)

            #print("past

以上是关于python未定义的元组的TypeError的主要内容,如果未能解决你的问题,请参考以下文章

Python:创建大小为 n^2 的元组的时间和空间复杂度

Python:扁平化包含来自函数的另一个元组的元组的最简单方法

Python-元组

Python:元组列表:比较所有元组并检索元组的元素不等于任何其他元组的元组

关于python的元组操作

关于Python中元组的问题