将字符串空间映射到单词列表?

时间:2019-04-08 11:48:51

标签: python

我有一个字符串

flagged_line = "V. Divakar Botcha1,2, Mengdie Zhang1, Kuilong Li1,2, Hong Gu1,2, Zhonghui Huang1, Jianhui Cai3, Youming Lu1, Wenjie Yu3, and Xinke Liu1*  "

和单词列表为

words = ['V.', 'Divakar', 'Botcha', '1', ',', '2', ',', 'Mengdie', 'Zhang', '1', ',', 'Kuilong', 'Li', '1', ',', '2', ',', 'Hong', 'Gu', '1', ',', '2', ',', 'Zhonghui', 'Huang', '1', ',', 'Jianhui', 'Cai', '3', ',', 'Youming', 'Lu', '1', ',', 'Wenjie', 'Yu', '3', ',', 'and', 'Xinke', 'Liu', '1', '*']

它们都来自2个不同的程序,现在我需要将字符串中的空格映射到列表中的单词,例如:(请注意,单词后面的尾部空格后跟一个空格)

['V. ', 'Divakar ', 'Botcha', '1', ',', '2', ', ', 'Mengdie ', 'Zhang', '1', ', ', 'Kuilong ', 'Li', '1', ',', '2', ', ', 'Hong ', 'Gu', '1', ',', '2', ', ', 'Zhonghui ', 'Huang', '1', ', ', 'Jianhui ', 'Cai', '3', ', ', 'Youming ', 'Lu', '1', ', ', 'Wenjie ', 'Yu', '3', ', ', 'and ', 'Xinke ', 'Liu', '1', '*  ']

我正在尝试按字符检查它们,然后分配空格

index_str = 0
for elem in words:
    for e in elem:
        if e == flagged_line[index_str]:
            index_str+=1
            pass
        else:
            index_str+=1
            elem = elem+' '  # issue not generalized for spaces
            print('"',elem,'"')

更新:

list元素将包含空格以帮助映射,例如

字符串中的

"V. Divakar  "

列表是

['V.','Divakar']

那最后的名单应该是

['V. ','Divakar  ']

稍后,我将在列表上进行迭代并将该元素附加到我的下一个函数中。

最后也可以有多个空格

2 个答案:

答案 0 :(得分:2)

使用str.find()查找子字符串(如果存在)的单线代码,请在空格后加上空格:

flagged_line = "V. Divakar Botcha1,2, Mengdie Zhang1, Kuilong Li1,2, Hong Gu1,2, Zhonghui Huang1, Jianhui Cai3, Youming Lu1, Wenjie Yu3, and Xinke Liu1*  "    
words = ['V.', 'Divakar', 'Botcha', '1', ',', '2', ',', 'Mengdie', 'Zhang', '1', ',', 'Kuilong', 'Li', '1', ',', '2', ',', 'Hong', 'Gu', '1', ',', '2', ',', 'Zhonghui', 'Huang', '1', ',', 'Jianhui', 'Cai', '3', ',', 'Youming', 'Lu', '1', ',', 'Wenjie', 'Yu', '3', ',', 'and', 'Xinke', 'Liu', '1', '*']

print(['{0} '.format(x) if flagged_line.find(x + " ") != -1 else x for x in words ])

输出

['V. ', 'Divakar ', 'Botcha', '1', ', ', '2', ', ', 'Mengdie ', 'Zhang', '1', ', ', 'Kuilong ', 'Li', '1', ', ', '2', ', ', 'Hong ', 'Gu', '1', ', ', '2', ', ', 'Zhonghui ', 'Huang', '1', ', ', 'Jianhui ', 'Cai', '3', ', ', 'Youming ', 'Lu', '1', ', ', 'Wenjie ', 'Yu', '3', ', ', 'and ', 'Xinke ', 'Liu', '1', '* ']

答案 1 :(得分:1)

我假设flagged_linewords完全匹配。您可以通过一次操作,只需保存index的{​​{1}},然后跳过flagged_line来查找一个单词后是否有空格(如果有),将其添加到结果:

len(word)

输出:

flagged_line = "V. Divakar Botcha1,2, Mengdie Zhang1, Kuilong Li1,2, Hong Gu1,2, Zhonghui Huang1, Jianhui Cai3, Youming Lu1, Wenjie Yu3, and Xinke Liu1*  "
words = ['V.', 'Divakar', 'Botcha', '1', ',', '2', ',', 'Mengdie', 'Zhang', '1', ',', 'Kuilong', 'Li', '1', ',',
         '2', ',', 'Hong', 'Gu', '1', ',', '2', ',', 'Zhonghui', 'Huang', '1', ',', 'Jianhui', 'Cai', '3', ',',
         'Youming', 'Lu', '1', ',', 'Wenjie', 'Yu', '3', ',', 'and', 'Xinke', 'Liu', '1', '*']

words_with_spaces = []
idx = 0
for i, word in enumerate(words):
    idx += len(word)
    cur_word = word
    while idx < len(flagged_line) and flagged_line[idx] == ' ':
        cur_word += ' '
        idx += 1
    words_with_spaces.append(cur_word)

print(words_with_spaces)

希望对您有所帮助,如果还有其他问题,请发表评论。 :)