+ 5
Is there a way to split a string into a list without a seperator?
I want to convert a string of unknown length into a list where every object has 6 chars (except the last one, if not len(string)%6==0) How do I do it?
4 Respostas
+ 5
@Luca Garrera nice!
0
def my_splitter(astr,size):
return [astr[i*size:(i*size)+size] for i,x in enumerate(astr)if i*size<len(astr)]
print(my_splitter("here is a string example too",6))
print(my_splitter("here is a string example too",3))
- 2
There is probably an easy way to do it, but for fun I wrote a function that will remove anything in the list that is more than 6 chars in length.
myList="me you television hello balloon people peoples".split()
def fixIt(x):
tempList=[]
for i in myList:
if len(i)<=6:
tempList.append(i)
return tempList
myList = fixIt(myList)