我有两条线,我想找一条它们的联合体。在这样做的同时,我想维持秩序。我这样做的目的是,我尝试几种方法来OCR图像并得到不同的结果。我想把所有不同的结果合并成一个结果,这将有最多的内容。在
这至少是我想要的:
#example1
string1 = "This is a test trees are green roses are red"
string2 = "This iS a TEST trees 12.48.1952 anthony gonzalez"
finalstring = "this is a test trees are green roses are red 12.48.1952 anthony gonzalez"
#example2
string2 = "This is a test trees are green roses are red"
string1 = "This iS a TEST trees 12.48.1952 anthony gonzalez"
finalstring = "this is a test trees are green roses are red 12.48.1952 anthony gonzalez"
#example3
string1 = "telephone conversation in some place big image on screen"
String2 = "roses are red telephone conversation in some place big image on screen"
finalstring = "roses are red telephone conversation in some place big image on screen"
#or the following - both are fine in this scenario.
finalstring = "telephone conversation in some place big image on screen roses are red "
这就是我尝试过的:
^{pr2}$
您可以使用生成器理解和
join
来完成您想要的。这会将i
设置为string1
中某个单词的索引,x
则设置为该单词。然后检查该单词是否在string2
中,如果不在,则将i
处的string2
中的单词添加到x
中,以将这两个单词放入最后的字符串中。在不要为此使用集合。您一定已经注意到,由于
set()
保持唯一的对象,因此只有一个被获得最终结果。在您可以使用^{} 进行此操作:
这样使用:
^{pr2}$如果要在字符级别执行合并,只需修改调用即可直接对字符串进行操作(而不是单词列表):
此解决方案正确地维护了字符串各个部分的顺序。因此,如果两个字符串都以公共部分结束,但在结束之前有一个不同的段,那么这两个不同的段仍将出现在结果的公共结束之前。例如,合并
A B D
和A C D
将得到A B C D
。在因此,您可以通过简单地删除结果字符串的部分,以正确的顺序找到每个原始字符串。如果从该示例结果中删除
C
,则会获得第一个字符串;如果删除B
,则会获得第二个字符串。这也适用于更复杂的合并。在相关问题 更多 >
编程相关推荐