Skip to main content
本文から 4742 文字削除
ソース リンク
peta
  • 13
  • 3

エラーメッセージ

listが二つあって、それぞれをdatalist1、datalist2としています。今私は、while文を使ってdatalist2[i]の中の要素をhとしたときに,datalist[h]を得ようとしています。しかし、このようにすると、"list indices must be integers or slices, not str"と返されます。どうすればよいでしょうか

IndexError                                Traceback (most recent call last)
<ipython-input-49-7b8715759b73> in <cell line: 73>()
     78       rot3_perm = datalist[k]
     79       enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
---> 80       cipher_tentative = enigma.get_text(plane)
     81       decrypt = enigma.get_text(cipher_tentative)
     82       if 'ABC' in decrypt == True:

2 frames
<ipython-input-49-7b8715759b73> in get_text(self, text)
     53                 if cnt2 % self.size == 0:
     54                     cnt3 += 1
---> 55                 m = self.get_matrix(cnt1, cnt2, cnt3)
     56                 veci = (vec * m).A[0]
     57                 for n, x in enumerate(vec):

<ipython-input-49-7b8715759b73> in get_matrix(self, cnt1, cnt2, cnt3)
     20         plugbord = np.mat([self.identity[n] for n in self.plug_perm])
     21         reflector = np.mat([self.identity[n] for n in self.ref_perm])
---> 22         rotor1 = np.mat([self.identity[n] for n in rot1_perm])
     23         rotor2 = np.mat([self.identity[n] for n in rot2_perm])
     24         rotor3 = np.mat([self.identity[n] for n in rot3_perm])

<ipython-input-49-7b8715759b73> inwhile i<;listcomp>;(.0)
     20         plugbord = np.mat([self.identity[n] for n in self.plug_perm])
     21         reflector = np.mat([self.identity[n] for n in self.ref_perm])
---> 22         rotor1 = np.mat([self.identity[n] for n in rot1_perm])
     23         rotor2 = np.mat([self.identity[n] for n in rot2_perm])
     24         rotor3 = np.mat([self.identity[n] for n in rot3_perm])

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

プログラム

import numpy as np
import random
import itertools

l = [0,1,2,3,4]
with open('file.txt', 'w') as f:
  for v in itertools.permutations(l):
    print(v, file=f)
with open('file.txt') as f:
  datalist = f.readlines()

k = []
for i in range(0,120):
  k.append(i)

with open('file.txt', 'w') as f2:
  for v in itertools.permutations(l,3):
    print(v, file=f2)
with open('file.txt') as f2:
  datalist2 = f2.readlines()

class Enigma():
    def __init__(self, plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm):
        self.alphabet = alphabet
        self.char_dict = {x: n for n, x in enumerate(alphabet)}
        self.size = len(alphabet)
        self.identity = np.eye(self.size)
        self.plug_perm = plug_perm
        self.ref_perm = ref_perm
        self.plane = plane

    def get_matrix(self, cnt1, cnt2, cnt3):
        rot_rotor_perm = list(range(self.size))

        col_perm_mat = np.mat([self.identity[n - 1] for n in rot_rotor_perm])
        row_perm_mat = col_perm_mat.T

        plugbord = np.mat([self.identity[n] for n in self.plug_perm])
        reflector = np.mat([self.identity[n] for n in self.ref_perm])
        rotor1 = np.mat([self.identity[n] for n in rot1_perm])
        rotor2 = np.mat([self.identity[n] for n in rot2_perm])
        rotor3 = np.mat([self.identity[n] for n in rot3_perm])

        m = (plugbord *
             (row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1) *
             (row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2) *
             (row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3) *
             reflector *
             ((row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3)).I *
             ((row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2)).I *
             ((row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1)).I *
             plugbord)
        return m

    def get_text(self, text):
        after = ''
        tmp = ''
        cnt1 = 0
        cnt2 = 0
        cnt3 = 0
        for char in text:
            tmp = ''
            if char not in self.alphabet:
                tmp += char
            else:
                vec = [0] * self.size
                vec[self.char_dict[char]] = 1
                cnt1 += 1
                if cnt1 % self.size == 0:
                    cnt2 += 1
                if cnt2 % self.size == 0:
                    cnt3 i+= 1
                m = self.get_matrix(cnt1, cnt2, cnt3)
                vec = (vec * m).A[0]
                for n, x in enumerate(vec):
                    if int(x) == 1:
                        tmp = self.alphabet[n]
                        break
            after += tmp
        return after

alphabet = 'ABCDE'
cipher = 'ABCDE'
plug_perm = [1, 2, 3, 4, 5]
ref_permh = [5, 4, 3, datalist2, 1]
plug_perm = [x - 1 for x in plug_perm]
ref_perm = [x - 1 for x in ref_perm[i]
C = []

def Checking_enigma(e,f,g):
  rot1_perm = datalist[e][h[1]]
  rot2_perm = datalist[f][h[4]]
  rot3_perm = datalist[g][h[7]]
  enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
  decrypt = enigma.get_text(cipher)
  if 'ABC' in decrypt == True:
    C.append(decrypt)

i = 0
while i<1685039:
  h = datalist2[i]
  r = []
  r.append(h[1])
  r.append(h[4])
  r.append(h[7])
  Checking_enigma(h[1:1],h[4:4],h[7:7])
  i = i+1

decrypt = enigma.get_text(cipher)
print(cipher)
print(decrypt)

エラーメッセージ

IndexError                                Traceback (most recent call last)
<ipython-input-49-7b8715759b73> in <cell line: 73>()
     78       rot3_perm = datalist[k]
     79       enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
---> 80       cipher_tentative = enigma.get_text(plane)
     81       decrypt = enigma.get_text(cipher_tentative)
     82       if 'ABC' in decrypt == True:

2 frames
<ipython-input-49-7b8715759b73> in get_text(self, text)
     53                 if cnt2 % self.size == 0:
     54                     cnt3 += 1
---> 55                 m = self.get_matrix(cnt1, cnt2, cnt3)
     56                 vec = (vec * m).A[0]
     57                 for n, x in enumerate(vec):

<ipython-input-49-7b8715759b73> in get_matrix(self, cnt1, cnt2, cnt3)
     20         plugbord = np.mat([self.identity[n] for n in self.plug_perm])
     21         reflector = np.mat([self.identity[n] for n in self.ref_perm])
---> 22         rotor1 = np.mat([self.identity[n] for n in rot1_perm])
     23         rotor2 = np.mat([self.identity[n] for n in rot2_perm])
     24         rotor3 = np.mat([self.identity[n] for n in rot3_perm])

<ipython-input-49-7b8715759b73> in <;listcomp>;(.0)
     20         plugbord = np.mat([self.identity[n] for n in self.plug_perm])
     21         reflector = np.mat([self.identity[n] for n in self.ref_perm])
---> 22         rotor1 = np.mat([self.identity[n] for n in rot1_perm])
     23         rotor2 = np.mat([self.identity[n] for n in rot2_perm])
     24         rotor3 = np.mat([self.identity[n] for n in rot3_perm])

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

プログラム

import numpy as np
import random
import itertools

l = [0,1,2,3,4]
with open('file.txt', 'w') as f:
  for v in itertools.permutations(l):
    print(v, file=f)
with open('file.txt') as f:
  datalist = f.readlines()

k = []
for i in range(0,120):
  k.append(i)

with open('file.txt', 'w') as f2:
  for v in itertools.permutations(l,3):
    print(v, file=f2)
with open('file.txt') as f2:
  datalist2 = f2.readlines()

class Enigma():
    def __init__(self, plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm):
        self.alphabet = alphabet
        self.char_dict = {x: n for n, x in enumerate(alphabet)}
        self.size = len(alphabet)
        self.identity = np.eye(self.size)
        self.plug_perm = plug_perm
        self.ref_perm = ref_perm
        self.plane = plane

    def get_matrix(self, cnt1, cnt2, cnt3):
        rot_rotor_perm = list(range(self.size))

        col_perm_mat = np.mat([self.identity[n - 1] for n in rot_rotor_perm])
        row_perm_mat = col_perm_mat.T

        plugbord = np.mat([self.identity[n] for n in self.plug_perm])
        reflector = np.mat([self.identity[n] for n in self.ref_perm])
        rotor1 = np.mat([self.identity[n] for n in rot1_perm])
        rotor2 = np.mat([self.identity[n] for n in rot2_perm])
        rotor3 = np.mat([self.identity[n] for n in rot3_perm])

        m = (plugbord *
             (row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1) *
             (row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2) *
             (row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3) *
             reflector *
             ((row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3)).I *
             ((row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2)).I *
             ((row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1)).I *
             plugbord)
        return m

    def get_text(self, text):
        after = ''
        tmp = ''
        cnt1 = 0
        cnt2 = 0
        cnt3 = 0
        for char in text:
            tmp = ''
            if char not in self.alphabet:
                tmp += char
            else:
                vec = [0] * self.size
                vec[self.char_dict[char]] = 1
                cnt1 += 1
                if cnt1 % self.size == 0:
                    cnt2 += 1
                if cnt2 % self.size == 0:
                    cnt3 += 1
                m = self.get_matrix(cnt1, cnt2, cnt3)
                vec = (vec * m).A[0]
                for n, x in enumerate(vec):
                    if int(x) == 1:
                        tmp = self.alphabet[n]
                        break
            after += tmp
        return after

alphabet = 'ABCDE'
cipher = 'ABCDE'
plug_perm = [1, 2, 3, 4, 5]
ref_perm = [5, 4, 3, 2, 1]
plug_perm = [x - 1 for x in plug_perm]
ref_perm = [x - 1 for x in ref_perm]
C = []

def Checking_enigma(e,f,g):
  rot1_perm = datalist[e]
  rot2_perm = datalist[f]
  rot3_perm = datalist[g]
  enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
  decrypt = enigma.get_text(cipher)
  if 'ABC' in decrypt == True:
    C.append(decrypt)

i = 0
while i<1685039:
  h = datalist2[i]
  r = []
  r.append(h[1])
  r.append(h[4])
  r.append(h[7])
  Checking_enigma(h[1:1],h[4:4],h[7:7])
  i = i+1

decrypt = enigma.get_text(cipher)
print(cipher)
print(decrypt)

listが二つあって、それぞれをdatalist1、datalist2としています。今私は、while文を使ってdatalist2[i]の中の要素をhとしたときに,datalist[h]を得ようとしています。しかし、このようにすると、"list indices must be integers or slices, not str"と返されます。どうすればよいでしょうか

i = 0
while i<100:
  i = i+1
  h = datalist2[i]
  rot1_perm = datalist[h[1]]
  rot2_perm = datalist[h[4]]
  rot3_perm = datalist[h[7]]
  enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
  decrypt = enigma.get_text(cipher)
  if 'ABC' in decrypt == True:
    C.append(decrypt)
タグを編集
リンク
nekketsuuu
  • 2.4万
  • 12
  • 53
  • 117
本文に 312 文字追加; タイトルを編集
ソース リンク
peta
  • 13
  • 3

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indicesというエラーの修正案を募集したいです list indexがうまく使えません

import numpy as np
import random
import itertools

l = [0,1,2,3,4]
with open('file.txt', 'w') as f:
  for v in itertools.permutations(l):
    print(v, file=f)
with open('file.txt') as f:
  datalist = f.readlines()

k = []
for i in range(0,120):
  k.append(i)

with open('file.txt', 'w') as f2:
  for v in itertools.permutations(l,3):
    print(v, file=f2)
with open('file.txt') as f2:
  datalist2 = f2.readlines()

class Enigma():
    def __init__(self, plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm):
        self.alphabet = alphabet
        self.char_dict = {x: n for n, x in enumerate(alphabet)}
        self.size = len(alphabet)
        self.identity = np.eye(self.size)
        self.plug_perm = plug_perm
        self.ref_perm = ref_perm
        self.plane = plane

    def get_matrix(self, cnt1, cnt2, cnt3):
        rot_rotor_perm = list(range(self.size))

        col_perm_mat = np.mat([self.identity[n - 1] for n in rot_rotor_perm])
        row_perm_mat = col_perm_mat.T

        plugbord = np.mat([self.identity[n] for n in self.plug_perm])
        reflector = np.mat([self.identity[n] for n in self.ref_perm])
        rotor1 = np.mat([self.identity[n] for n in rot1_perm])
        rotor2 = np.mat([self.identity[n] for n in rot2_perm])
        rotor3 = np.mat([self.identity[n] for n in rot3_perm])

        m = (plugbord *
             (row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1) *
             (row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2) *
             (row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3) *
             reflector *
             ((row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3)).I *
             ((row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2)).I *
             ((row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1)).I *
             plugbord)
        return m

    def get_text(self, text):
        after = ''
        tmp = ''
        cnt1 = 0
        cnt2 = 0
        cnt3 = 0
        for char in text:
            tmp = ''
            if char not in self.alphabet:
                tmp += char
            else:
                vec = [0] * self.size
                vec[self.char_dict[char]] = 1
                cnt1 += 1
                if cnt1 % self.size == 0:
                    cnt2 += 1
                if cnt2 % self.size == 0:
                    cnt3 += 1
                m = self.get_matrix(cnt1, cnt2, cnt3)
                vec = (vec * m).A[0]
                for n, x in enumerate(vec):
                    if int(x) == 1:
                        tmp = self.alphabet[n]
                        break
            after += tmp
        return after

alphabet = 'ABCDEFGH'ABCDE'
planecipher = 'DGHABCH'ABCDE'
plug_perm = [1, 2, 3, 4, 5, 6, 7, 8]
ref_perm = [8, 7, 6, 5, 4, 3, 2, 1]
plug_perm = [x - 1 for x in plug_perm]
ref_perm = [x - 1 for x in ref_perm]
I = []
J = []
KC = []
for i in range (0,40319):
  for j in rangedef Checking_enigma(0e,40319):
    for k in range (0f,40319g):
      rot1_perm = datalist[i[e]
      rot2_perm = datalist[j[f]
      rot3_perm = datalist[k[g]
      enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
      cipher_tentative = enigma.get_text(plane)
      decrypt = enigma.get_text(cipher_tentative)
      if 'ABC' in decrypt == True:
        IC.append(idecrypt)
 
i = 0
while i<1685039:
  h = J.append(j)datalist2[i]
  r = []
  r.append(h[1])
  Kr.append(kh[4])
 
cipher = enigmar.get_textappend(planeh[7])
  Checking_enigma(h[1:1],h[4:4],h[7:7])
  i = i+1

decrypt = enigma.get_text(cipher)
print(cipher)
print(decrypt)

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indicesというエラーの修正案を募集したいです

import numpy as np
import random

class Enigma():
    def __init__(self, plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm):
        self.alphabet = alphabet
        self.char_dict = {x: n for n, x in enumerate(alphabet)}
        self.size = len(alphabet)
        self.identity = np.eye(self.size)
        self.plug_perm = plug_perm
        self.ref_perm = ref_perm
        self.plane = plane

    def get_matrix(self, cnt1, cnt2, cnt3):
        rot_rotor_perm = list(range(self.size))

        col_perm_mat = np.mat([self.identity[n - 1] for n in rot_rotor_perm])
        row_perm_mat = col_perm_mat.T

        plugbord = np.mat([self.identity[n] for n in self.plug_perm])
        reflector = np.mat([self.identity[n] for n in self.ref_perm])
        rotor1 = np.mat([self.identity[n] for n in rot1_perm])
        rotor2 = np.mat([self.identity[n] for n in rot2_perm])
        rotor3 = np.mat([self.identity[n] for n in rot3_perm])

        m = (plugbord *
             (row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1) *
             (row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2) *
             (row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3) *
             reflector *
             ((row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3)).I *
             ((row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2)).I *
             ((row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1)).I *
             plugbord)
        return m

    def get_text(self, text):
        after = ''
        tmp = ''
        cnt1 = 0
        cnt2 = 0
        cnt3 = 0
        for char in text:
            tmp = ''
            if char not in self.alphabet:
                tmp += char
            else:
                vec = [0] * self.size
                vec[self.char_dict[char]] = 1
                cnt1 += 1
                if cnt1 % self.size == 0:
                    cnt2 += 1
                if cnt2 % self.size == 0:
                    cnt3 += 1
                m = self.get_matrix(cnt1, cnt2, cnt3)
                vec = (vec * m).A[0]
                for n, x in enumerate(vec):
                    if int(x) == 1:
                        tmp = self.alphabet[n]
                        break
            after += tmp
        return after

alphabet = 'ABCDEFGH'
plane = 'DGHABCH'
plug_perm = [1, 2, 3, 4, 5, 6, 7, 8]
ref_perm = [8, 7, 6, 5, 4, 3, 2, 1]
plug_perm = [x - 1 for x in plug_perm]
ref_perm = [x - 1 for x in ref_perm]
I = []
J = []
K = []
for i in range (0,40319):
  for j in range (0,40319):
    for k in range (0,40319):
      rot1_perm = datalist[i]
      rot2_perm = datalist[j]
      rot3_perm = datalist[k]
      enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
      cipher_tentative = enigma.get_text(plane)
      decrypt = enigma.get_text(cipher_tentative)
      if 'ABC' in decrypt == True:
        I.append(i)
        J.append(j)
        K.append(k)
 
cipher = enigma.get_text(plane)
decrypt = enigma.get_text(cipher)
print(cipher)
print(decrypt)

list indexがうまく使えません

import numpy as np
import random
import itertools

l = [0,1,2,3,4]
with open('file.txt', 'w') as f:
  for v in itertools.permutations(l):
    print(v, file=f)
with open('file.txt') as f:
  datalist = f.readlines()

k = []
for i in range(0,120):
  k.append(i)

with open('file.txt', 'w') as f2:
  for v in itertools.permutations(l,3):
    print(v, file=f2)
with open('file.txt') as f2:
  datalist2 = f2.readlines()

class Enigma():
    def __init__(self, plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm):
        self.alphabet = alphabet
        self.char_dict = {x: n for n, x in enumerate(alphabet)}
        self.size = len(alphabet)
        self.identity = np.eye(self.size)
        self.plug_perm = plug_perm
        self.ref_perm = ref_perm
        self.plane = plane

    def get_matrix(self, cnt1, cnt2, cnt3):
        rot_rotor_perm = list(range(self.size))

        col_perm_mat = np.mat([self.identity[n - 1] for n in rot_rotor_perm])
        row_perm_mat = col_perm_mat.T

        plugbord = np.mat([self.identity[n] for n in self.plug_perm])
        reflector = np.mat([self.identity[n] for n in self.ref_perm])
        rotor1 = np.mat([self.identity[n] for n in rot1_perm])
        rotor2 = np.mat([self.identity[n] for n in rot2_perm])
        rotor3 = np.mat([self.identity[n] for n in rot3_perm])

        m = (plugbord *
             (row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1) *
             (row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2) *
             (row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3) *
             reflector *
             ((row_perm_mat ** cnt3) * rotor3 * (col_perm_mat ** cnt3)).I *
             ((row_perm_mat ** cnt2) * rotor2 * (col_perm_mat ** cnt2)).I *
             ((row_perm_mat ** cnt1) * rotor1 * (col_perm_mat ** cnt1)).I *
             plugbord)
        return m

    def get_text(self, text):
        after = ''
        tmp = ''
        cnt1 = 0
        cnt2 = 0
        cnt3 = 0
        for char in text:
            tmp = ''
            if char not in self.alphabet:
                tmp += char
            else:
                vec = [0] * self.size
                vec[self.char_dict[char]] = 1
                cnt1 += 1
                if cnt1 % self.size == 0:
                    cnt2 += 1
                if cnt2 % self.size == 0:
                    cnt3 += 1
                m = self.get_matrix(cnt1, cnt2, cnt3)
                vec = (vec * m).A[0]
                for n, x in enumerate(vec):
                    if int(x) == 1:
                        tmp = self.alphabet[n]
                        break
            after += tmp
        return after

alphabet = 'ABCDE'
cipher = 'ABCDE'
plug_perm = [1, 2, 3, 4, 5]
ref_perm = [5, 4, 3, 2, 1]
plug_perm = [x - 1 for x in plug_perm]
ref_perm = [x - 1 for x in ref_perm]
C = []

def Checking_enigma(e,f,g):
  rot1_perm = datalist[e]
  rot2_perm = datalist[f]
  rot3_perm = datalist[g]
  enigma = Enigma(plane, alphabet, plug_perm, ref_perm, rot1_perm, rot2_perm, rot3_perm)
  decrypt = enigma.get_text(cipher)
  if 'ABC' in decrypt == True:
    C.append(decrypt)

i = 0
while i<1685039:
  h = datalist2[i]
  r = []
  r.append(h[1])
  r.append(h[4])
  r.append(h[7])
  Checking_enigma(h[1:1],h[4:4],h[7:7])
  i = i+1

decrypt = enigma.get_text(cipher)
print(cipher)
print(decrypt)
ソース リンク
peta
  • 13
  • 3
読み込んでいます