我有以下字符串和列表:
myString = "a:::b:::c:::d ..... " where ':::' is sort of delimiter
myList = [1,2,3,4.......]
我知道如何遍历列表值,但是我如何用列表中的下一个值替换每次出现的':::',这样我的最终输出如下:
myString = "a1b2c3d ...."
答案 0 :(得分:2)
快速&脏:
myString = "a:::b:::c:::d"
myList = [1,2,3,4]
s = iter(myString.split(":::"))
print(next(s) + "".join(str(y)+x for x,y in zip(s,myList)))
打印a1b2c3d
答案 1 :(得分:2)
l = myString.split(":::")
result = "".join([x + str(y) for (x,y) in zip(l, myList)])
答案 2 :(得分:0)
以下是我如何解决它。
def join_string_and_list( string, list ):
string = string.split(":::")
for i in range(0, len(string)):
if string[i] != '' and len(list) > 0:
string.append("{}{}".format(string.pop(0),list.pop(0)))
return str("".join(string))
myString = "a:::b:::c:::d:::" #where ':::' is sort of delimiter
myList = [1,2,3,4]
print(join_string_and_list( myString, myList ))