将字符串分成两个Python数组';s

将字符串分成两个Python数组';s,python,Python,我试图使用下面的查询将一串单词拆分为两个单词列表。直到'a'的字符串应进入begin,其余的应进入余数。但是while循环以某种方式保持运行,而不考虑begin已经包含“a”的事实。非常感谢你的帮助 random_string = 'hello this is a test string' split = {} split = random_string.split() begin = [] remainder = [] while 'a' not in begin: for word in

我试图使用下面的查询将一串单词拆分为两个单词列表。直到'a'的字符串应进入begin,其余的应进入余数。但是while循环以某种方式保持运行,而不考虑begin已经包含“a”的事实。非常感谢你的帮助

random_string = 'hello this is a test string'
split = {}
split = random_string.split()
begin = []
remainder = []

while 'a' not in begin:
for word in split:
    storage = word
    begin.append(storage)


print(begin)

试试这个,只要两行。一个是带“a”的单词,另一个是不带“a”的单词

random_string = 'hello this is a test string, tap task'

begin = [x for x in random_string.split(' ') if 'a' in x]
remainder = [x for x in random_string.split(' ') if 'a' not in x]

print begin, remainder
将打印

['a', 'tap', 'task']
['hello', 'this', 'is', 'test', 'string,']

尝试使用切片和索引,不需要运行循环来捕获“a”:

random_string = 'hello this is a test string'
split = random_string.split(' ')
index = split.index('a') + 1
array_1 = split[:index]
array_2 = split[index:]

print(array_1, array_2)

使用内置字符串例程:

>>> str = 'hello this is a test string'
>>> begin, end str.split('a')
['hello this is ', ' test string']
>>> begin_words = begin.split()
['hello', 'this', 'is']
>>> end_words = end.split()
['test', 'string']

split
的默认值是在空白处分割,但正如您所看到的,它也适用于其他字符串。

因此,这里的问题是在for循环完成后检查while循环条件。本质上就是这样

  • “a”不在begin中
  • 循环拆分并将每个单词添加到开头
  • 检查“a”是否在“开始”中
  • 您可以尝试以下方法:

    for word in split:
        if 'a' in begin:
            remainder.append(word)
        else:
            begin.append(word)
    
    在循环的每次迭代中检查“a”条件,或遵循其他答案中列出的切片技术,只需拆分字符串即可:

    将句子理解为一个特定的单词: 减半: (我觉得你的帖子对你想要的东西模棱两可。)


    您应该查看数组的
    .index
    方法,不需要循环

    random_string = 'hello this is a test string'
    
    split = random_string.split()
    
    begin = []
    remainder = []
    index = split.index('a')
    
    begin = split[:index] 
    remainder = split[index:]
    
    print(begin)
    print(remainder)
    
    上面的代码段将打印:

    ['hello', 'this', 'is']
    ['a', 'test', 'string']
    

    当“a”不在begin中时,仅在该外部循环的每次迭代中检查条件
    (因此在本例中,在
    for
    循环之前和之后检查两次)。当您在split中为word嵌套
    时:
    整个循环将在
    之前完成,同时再次检查“a”
    条件。您应该添加条件来检查
    'a'
    是否在for循环的
    开始中。您可以添加预期的输出吗?你还会遇到什么其他情况?(如果有几个分隔符元素,您会想要什么?)。我正在尝试使用两个for循环将字符串拆分为三部分,如下所示:
    random\u string='您好,这是一个测试字符串,现在我将使它变长'split={}split=random\u string.split()begin=[]part\u 2=[]余数=[]用于拆分中的单词:如果begin中的'a':rements.append(单词)else:begin.append(单词)对于余数中的单词:如果余数中有“”:余数。追加(word)其他:部分2。追加(word)打印(开始)打印(部分2)打印(余数)
    Errr这将创建字母列表:(我的坏,使用
    split(“”)
    如果不转换为列表,一开始就可以做到这一点。有没有办法将单词保留在数组中?这将返回两个数组,其中所有字符都被分割成碎片。只需修复它(请参阅我的文章):-P很抱歉上次的onenice拆分,但它没有将字符串的每个部分都添加到列表中。我该怎么做?我的朋友,它是这样做的,因为它已经在一个列表中。列表的内容是由拆分生成的。拆分的每个元素都会检查条件。我将尝试为您获取一些文档。这一个非常完美!这正是我想要的。只是一个跟进。如果我想将一个较大的文本段拆分为3或4,我可以这样运行吗?对于拆分中的word:如果开始:余数中的“a”。追加(word)其他:开始。追加(word)我没有完成输入,我的意思是使用一个额外的for循环来拆分剩余部分,如下所示:
    random\u string='hello这是一个测试字符串,现在我将使它变长'split={}split=random\u string.split()begin=[]part\u 2=[]restins=[]用于拆分中的单词:如果begin中的'a':rements.append(word)else:begin.append(word)for word in rements:if“”in rements:rements.append(word)else:part_2.append(word)print(begin)print(part_2)print(rements)
    此解决方案不保留分隔符。
    random_string = 'hello this is a test string'
    
    split = random_string.split()
    
    begin = []
    remainder = []
    index = split.index('a')
    
    begin = split[:index] 
    remainder = split[index:]
    
    print(begin)
    print(remainder)
    
    ['hello', 'this', 'is']
    ['a', 'test', 'string']