繁体   English   中英

如何在列表项 python 中保留分隔符

[英]How to retain delimiter within list item python

我正在编写一个程序,该程序使用标点符号作为何时拆分文本的分隔符来混淆文本中的子句。

目前我的代码有一个很大的列表,其中每个项目都是一组子句。

import re
from random import shuffle
clause_split_content = []

text = ["this, is. a test?", "this: is; also. a test!"]

for i in text:
        clause_split = re.split('[,;:".?!]', i)
        clause_split.remove(clause_split[len(clause_split)-1])
        for x in range(0, len(clause_split)):
                clause_split_content.append(clause_split[x])
shuffle(clause_split_content)
print(*content, sep='')

目前结果使文本混乱而不保留用作分隔符的标点符号来拆分它。 output 是这样的:

a test this also this is a test is

我想在最后的 output 中保留标点符号,所以它看起来像这样:

a test! this, also. this: is. a test? is;

选项1:将每个索引中的单词打乱并组合成句子。

from random import shuffle

count = 0
sentence = ''
new_text = []
text = ["this, is. a test?", "this: is; also. a test!"]

while count < len(text):
    new_text.append(text[count].split())
    shuffle(new_text[count])
    count += 1

for i in new_text:
    for j in i:
        sentence += j + ' '

print(sentence)    

样本洗牌输出:

test? this, a is. is; test! this: a also. 
test? a is. this, is; test! a this: also. 
is. test? a this, test! a this: also. is; 

选项2:将列表中的所有元素组合成一个元素,然后打乱单词并组合成一个句子。

import random
from random import shuffle

count = 0
sentence = ''
new_text = []
text_combined = []
text = ["this, is. a test?", "this: is; also. a test!"]

while count < len(text):
    new_text.append(text[count].split())
    count += 1

for i in new_text:
    for j in i:
        text_combined.append(j)

shuffled_list = random.sample(text_combined, len(text_combined))        

for i in shuffled_list:
    sentence += i + ' '
     
print(sentence)

样本输出:

this, is; also. a this: is. a test? test! 
test! is. this: test? a this, a also. is; 
is. a a is; also. test! test? this, this:

我认为您只是出于您的目的使用了错误的re功能。 split()不包括您的分隔符,但您可以使用另一个函数,例如findall()手动选择您想要的所有单词。 例如,使用以下代码,我可以创建您想要的输出:

import re
from random import shuffle

clause_split_content = []

text = ["this, is. a test?", "this: is; also. a test!"]

for i in text:
    words_with_seperator = re.findall(r'([^,;:".?!]*[,;:".?!])\s?', i)
    clause_split_content.extend(words_with_seperator)
    
shuffle(clause_split_content)
print(*clause_split_content, sep=' ')

输出:

this, this: is. also. a test! a test? is;

模式([^,;:".?!]*[,;:".?!])\s? 只取所有不是分隔符的字符,直到看到分隔符。 这些字符都在匹配组中,这会创建您的结果。 \s? 只是为了摆脱单词之间的空格字符。

这是一种执行您所要求的方法:

import re
from random import shuffle
text = ["this, is. a test?", "this: is; also. a test!"]
content = [y for x in text for y in re.findall(r'([^,;:".?!]*[,;:".?!])', x)]
shuffle(content)
print(*content, sep=' ')

输出:

 is;  is.  also.  a test? this,  a test! this:

解释:

  • 正则表达式模式r'([^,;:".?!]*[,;:".?!])'匹配 0 个或多个非分隔符后跟一个分隔符,并且findall()返回一个列表所有此类非重叠匹配
  • 列表推导迭代列表text中的输入字符串,并有一个内部循环迭代每个输入字符串的findall结果,因此我们为每个字符串中的每个匹配模式创建一个列表。
  • shuffleprint与您的原始代码一样。

暂无
暂无

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

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