简体   繁体   中英

How can i store values from one array to another array?

I have two text files, which contain Bangla language plain text. I read the two files and store each file's text line by line in two arrays. Now I am trying to store one array value into another array but I found an error. Code is bellow with the error message.

f = open("doc1.txt", encoding = 'utf-8')
a1 = f.read()
f = open("doc2.txt", encoding = 'utf-8')
a2 = f.read()
sentence1 = a1.split("।")
sentence2 = a2.split("।")

np_sent1 = np.array(sentence1)
np_sent2 = np.array(sentence2)

len_np_sent1 = len(np_sent1)
len_np_sent2 = len(np_sent2)

rint(np_sent1.shape)

for x in range(len_np_sent2):
    len_np_sent1 = len_np_sent1 + 1;
    np_sent1[len_np_sent1] = np_sent2[x]
print(np_sent1,len(np_sent1))

Error message:

IndexError: index 11 is out of bounds for axis 0 with size 10

 15     np_sent1[len_np_sent1] = np_sent2[x]

You can use either np.append or np.concatenate .

np.append uses np.concatenate internally.

Change

for x in range(len_np_sent2):
    len_np_sent1 = len_np_sent1 + 1;
    np_sent1[len_np_sent1] = np_sent2[x]

To

np_sent1 = np.append(np_sent1, np_sent2)

OR

np_sent1 = np.concatenate((np_sent1, np_sent2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM