如何从k长度字符串列表中生成k + 1个长度字符串? [英] How to generate k+1 length strings from a list of k length strings?

查看:86
本文介绍了如何从k长度字符串列表中生成k + 1个长度字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个长度为k的字符串列表。对于每对k长度

字符串共有k-1个字符,我想生成一个k + 1

长度的字符串(k-1个普通字符+ 2不常见的字符。)

例如我想加入''abcd''与bcde''获得''abcde''但我不想要

加入' 'abcd''与''cdef''

目前我正在加入每2个字符串,然后从每个连接的字符串中删除重复的字符

,最后删除所有这些字符串其中

长度!= k + 1.这是我写的代码:


for i in range(0,len(prunedK) ) - 1,1):

如果k在范围内(1,len(prunedK),1)& i + k< = len(prunedK)-1:

colocn = prunedK [i] + prunedK [i + k]

prunedNew1.append(colocn)

继续

for prunedNew1中的字符串:

stringNew = withoutDup(string)

prunedNew.append(stringNew)

继续


但是在时间方面这个很糟糕:(。

提前谢谢,

girish


I have a list of strings all of length k. For every pair of k length
strings which have k-1 characters in common, i want to generate a k+1
length string(the k-1 common characters + 2 not common characters).
e.g i want to join ''abcd'' with bcde'' to get ''abcde'' but i dont want to
join ''abcd'' with ''cdef''
Currently i''m joining every 2 strings, then removing duplicate characters
from every joined string and finally removing all those strings whose
length != k+1.Here''s the code i''ve written:

for i in range(0,len(prunedK) - 1,1):
if k in range(1,len(prunedK),1) & i+k <= len(prunedK) -1:
colocn = prunedK[i] + prunedK[i+k]
prunedNew1.append(colocn)
continue
for string in prunedNew1:
stringNew = withoutDup(string)
prunedNew.append(stringNew)
continue

But this one is quite bad in the time aspect :(.
Thanks in advance,
girish

推荐答案

试试这个:


def k2k1(string1,string2):

for string1:

string2 = string2.replace(c,"",1)


if len(string2) == 1:

string1 + = string2


返回string1


print k2k1(" abcd", " ebcd")

Try this:

def k2k1(string1, string2):
for c in string1:
string2 = string2.replace(c,"",1)

if len(string2) == 1:
string1 += string2

return string1

print k2k1("abcd", "ebcd")


实际上,小修复:


MTD写道:
actually, minor fix:

MTD wrote:
试试这个:

def k2k1(string1,string2):
对于string1中的c:
string2 = string2.replace(c,"&q uot;,1)

如果len(string2)== 1:
string1 + = string2
else:

string1 =""

返回string1

打印k2k1(" abcd"," ebcd")
Try this:

def k2k1(string1, string2):
for c in string1:
string2 = string2.replace(c,"",1)

if len(string2) == 1:
string1 += string2 else:
string1 = ""

return string1

print k2k1("abcd", "ebcd")





所以是的,只是把它们放在一起,试试这个。从你的两个Ks,它可以返回K + 1(如果可以)或空字符串。


def k2k1(string1,string2):

for string1:

string2 = string2.replace(c,"",1)


if len(string2) == 1:

string1 + = string2

else:

string1 =""


返回string1

测试:


打印k2k1(" abcdadd"," abceadd")


给出:

abcdadde

So yeah, just to put it all together, try this. From your two Ks, it
either returns K+1 if it can or an empty string.

def k2k1(string1, string2):
for c in string1:
string2 = string2.replace(c,"",1)

if len(string2) == 1:
string1 += string2
else:
string1 = ""

return string1
Testing:

print k2k1("abcdadd", "abceadd")

gives:
abcdadde


这篇关于如何从k长度字符串列表中生成k + 1个长度字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆