lundi 20 avril 2015

Dynamically splitting and joining strings python [duplicate]

This question already has an answer here:

stringlist = somestring.split()
splice = []
for word in stringlist[:-1]:
    splice.append(word + " " + stringlist[stringlist.index(word)+1])

Currently, the above code splits and joins a string into bunches of two. For example, for a string: "this is a string", the splice list will look like: ["this is","is a","a string"].

What I want to do, is to be able to split and join the string dynamically on the basis of a number (for the number of bunches). For 3, the splicelist would be ["this is a","is a string"]

Aucun commentaire:

Enregistrer un commentaire