Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何基于已创建的现有阵列创建阵列?_Python_Arrays_Numpy - Fatal编程技术网

Python 如何基于已创建的现有阵列创建阵列?

Python 如何基于已创建的现有阵列创建阵列?,python,arrays,numpy,Python,Arrays,Numpy,我通过将一个句子拆分为字符(包括标点和空格)创建了一个数组。我想根据其中的字符创建另一个数组。这是我写的代码 def split(arr, size): arrs = [] while len(arr) > size: pice = arr[:size] arrs.append(pice) arr = arr[1:] arrs.append(arr) return arrs def main(): x_mat =

我通过将一个句子拆分为字符(包括标点和空格)创建了一个数组。我想根据其中的字符创建另一个数组。这是我写的代码

def split(arr, size):
    arrs = []
    while len(arr) > size:
       pice = arr[:size]
       arrs.append(pice)
       arr   = arr[1:]
   arrs.append(arr)
   return arrs

def main(): 
x_mat = [list(word.rstrip()) for word in result] 

for rows in x_mat: 
    if len(rows) == 3 :
        rows = rows.append(' ')

myarray = np.asarray(x_mat)
m = len(myarray)
n = len(myarray[0])

numx_mat = np.zeros([m, n])

for j in range(len(myarray)) :  
    if( 97 <= ord(myarray([j,1])) <= 122):  
        numx_mat([j,1])  == 1 
    elif( myarray([j,2]) == '.' or '?' or '!') :
        numx_mat([j,2]) == 1 
    elif( myarray([j,3]) == ' ') :
        numx_mat([j,3]) == 1 
    elif(65 <= ord(myarray([j,4])) <= 90 ): 
            numx_mat([j,4]) == 1 
    else :
            continue 
main()
def拆分(arr,大小):
arrs=[]
而len(arr)>尺寸:
pice=arr[:尺寸]
附件(pice)
arr=arr[1:]
arrs.append(arr)
返回arrs
def main():
x_mat=[结果中单词的列表(word.rstrip())]
对于x_mat中的行:
如果len(行)==3:
行=行。追加(“”)
myarray=np.asarray(x_mat)
m=len(myarray)
n=len(myarray[0])
numx_mat=np.零([m,n])
对于范围内的j(len(myarray)):

如果(97<P><代码> NoMPy一段时间,还有其他方法可以考虑。

使用re.match解析字符串 这将生成1和0的数组:

>>> string
'This is a sentence. Followed by a bang! And a Question?'
>>> gen_array(string)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
“1”在“句子”中的“e”处,“bang”中的“g”处

使用re.finditer以数组形式获取偏移匹配
>>字符串
“这是一个句子。紧接着是一声巨响!还有一个问题?”
>>>结果=列表(关于finditer(r'[a-z][?.!]\s[a-z]',字符串))
>>>结果
[, ]
>>>对于匹配结果:
…打印(match.group(),“at”,match.span())
... 
e、 F在(17、21)
g!A at(37,41)

可以根据需要调整Unicode或其他标点字符的字符类(集)。

该错误意味着
ord
的参数不是
可调用的
。它必须是一个单字符字符串(在终端中运行
python-c“打印帮助(ord)”
)。你到底想完成什么?另外,关于提出明确问题的一些建议:1)抽象问题:问题很少需要对应用程序进行详细解释。一个剥落的样品通常就足够了。2) 确保您的代码格式正确:否则我们必须猜测缩进发生的位置。我想看看我创建的数组的每一行的格式是否为:小写字母、句尾标点符号、空格和大写字母,帮助我检测一个句子的结尾和下一个句子的开头。我将编辑这个问题,使它更形象。你能清理缩进吗<代码>主
似乎不正确。行
defmain():
和调用
main()
之间的所有内容都应该在函数
main()
中吗?
>>> string
'This is a sentence. Followed by a bang! And a Question?'
>>> gen_array(string)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> string
'This is a sentence. Followed by a bang! And a Question?'
>>> results = list(re.finditer(r'[a-z][?.!]\s[A-Z]', string))
>>> results
[<_sre.SRE_Match object; span=(17, 21), match='e. F'>, <_sre.SRE_Match object; span=(37, 41), match='g! A'>]

>>> for match in results:
...     print(match.group(), "at", match.span())
... 
e. F at (17, 21)
g! A at (37, 41)