简体   繁体   English

如何将值从一个数组存储到另一个数组?

[英]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.appendnp.concatenate

np.append uses np.concatenate internally. np.append np.concatenate内部使用np.concatenate

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以从一个数组到另一个数组的 append 特定值吗? - Can I append specific values from one array to another? 如何根据索引更有效地将一个数组中的值分配给另一个数组? - How can I assign values from one array to another according to the index more efficiently? 我怎样才能让一个数组只返回另一个数组定义的掩码值? - How can I get one array to return only the masked values define by another array with Numpy / PyTorch? 如何获取一个指示另一个数组的索引的数组,并将这些值存储在另一个数组中? - How do I take an array indicating the indicies of another array and store those values in yet another array? 如何一次存储数组中的两个值 - How to store two values from an array in one pass 如何用另一个数组中唯一值的索引替换numpy数组中的重复值? - How can I replace recurring values in a numpy array by the index of the unique value from another array? 如何从二维数组中选择值并添加到另一个二维数组 python - How can i select values from 2d array and add to another 2d array python 如何用另一个数组的相同索引中的值替换一个数组中的值? - How can I replace a value from one array with a value in the same index of another array? 如何在numpy数组中存储带尾随空值的二进制值? - How can I store binary values with trailing nulls in a numpy array? 一个数组中的 Select 值使用 numpy 中另一个数组的值 - Select values from one array using values of another array in numpy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM