Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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_Python 3.x_String_List_Text - Fatal编程技术网

Python 在多个条件下拆分字符串列表

Python 在多个条件下拆分字符串列表,python,python-3.x,string,list,text,Python,Python 3.x,String,List,Text,我有许多句子,我想根据具体的单词(例如和)进行拆分。然而,当拆分句子时,有时我想在一个句子中拆分两个或两个以上的单词组合 例句: ['i', 'am', 'just', 'hoping', 'for', 'strength', 'and', 'guidance', 'because', 'i', 'have', 'no', 'idea', 'why'] ['maybe', 'that', 'is', 'why', 'he', 'does', 'not', 'come', 'home', 'and

我有许多句子,我想根据具体的单词(例如和)进行拆分。然而,当拆分句子时,有时我想在一个句子中拆分两个或两个以上的单词组合

例句:

['i', 'am', 'just', 'hoping', 'for', 'strength', 'and', 'guidance', 'because', 'i', 'have', 'no', 'idea', 'why']
['maybe', 'that', 'is', 'why', 'he', 'does', 'not', 'come', 'home', 'and', 'tell', 'you', 'how', 'good', 'his', 'day', 'at', 'work', 'was', 'because', 'he', 'is', 'been', 'told', 'not', 'to', 'talk']
所以我写了一些代码来拆分一个句子:

split_on_word = []
no_splitting = []
indexPosList = [ i for i in range(len(kth)) if kth[i] == 'and'] # check if word is in sentence
for e in example: 
  kth = e.split() # split strings into list so it looks like example sentence
  for n in indexPosList:
    if n > 4: # only split when the word's position is 4 or more 
      h = e.split("and")
      for i in h: 
        split_on_word.append(i)# append split sentences 
    else:
      no_splitting.append(kth) #append sentences that don't need to be split
但是,您可以看到,当多次使用此代码(例如:将要拆分的单词替换为另一个)时,我将创建附加到新列表中的句子的副本或部分副本

有没有办法检查多个条件,这样如果一个句子同时包含这两个条件或其他条件的组合,我就可以一次拆分这个句子

示例的输出应如下所示:

['i', 'am', 'just', 'hoping', 'for', 'strength']
['guidance', 'because']
['i', 'have', 'no', 'idea', 'why']

['maybe', 'that', 'is', 'why', 'he', 'does', 'not', 'come', 'home']
[ 'tell', 'you', 'how', 'good', 'his', 'day', 'at', 'work', 'was']
['he', 'is', 'been', 'told', 'not', 'to', 'talk']
可以与检查单词是否为拆分单词的函数一起使用:

In [11]: split_words = {'and', 'because'}                                                     

In [12]: [list(g) for k, g in it.groupby(example, key=lambda x: x not in split_words) if k]   
Out[12]: 
[['maybe', 'that', 'is', 'why', 'he', 'does', 'not', 'come', 'home'],
 ['tell', 'you', 'how', 'good', 'his', 'day', 'at', 'work', 'was'],
 ['he', 'is', 'been', 'told', 'not', 'to', 'talk']]
可以与检查单词是否为拆分单词的函数一起使用:

In [11]: split_words = {'and', 'because'}                                                     

In [12]: [list(g) for k, g in it.groupby(example, key=lambda x: x not in split_words) if k]   
Out[12]: 
[['maybe', 'that', 'is', 'why', 'he', 'does', 'not', 'come', 'home'],
 ['tell', 'you', 'how', 'good', 'his', 'day', 'at', 'work', 'was'],
 ['he', 'is', 'been', 'told', 'not', 'to', 'talk']]

你有两个单词列表,那是你的句子已经拆分了介绍词吗?@kederrac是的,所以我只想在我要拆分的单词的索引位置在位置4之后时拆分这些句子,否则这个句子只是附加在后面。@kederrac你指的是“示例句子”下的两个列表吗,请添加
kth
e
variables@kederrac这是两个独立的示例句子,我更新了输出以使其更清晰。您有两个单词列表,这是您的句子已经拆分介绍词了吗?@kederrac是的,所以我只想拆分句子,如果我要拆分的单词的索引位置在位置4之后,否则,只需添加句子。@kederrac您是指“示例句子”下的两个列表吗?另外,请添加
kth
e
variables@kederrac这是两个独立的例句,我更新了输出以使其更清晰