["a", "b", ... ,"z"]
When there is a list like ↑, I want to divide it into 5 characters such as a to e and f to j. Also, I would like to include the last remaining z in the list.
In other words
["a to e", "f to j", ..., "z"]
I would like to split it into sublists like.
Queue all strings and retrieve every five. Also, by retrieving only non-empty cues, the last remaining element and beyond are ignored.
split.py
import queue
#Create a character list from a to z
chars = [chr(ord('a') + i) for i in range(26)]
#Create queue
q = queue.Queue()
#Insert character list in queue
for s in chars:
q.put(s)
#List for storing split characters
subList = []
#Split character list
while not q.empty():
#Temporarily save the split list
splittedList = []
#Separate every 5
for i in range(5):
if not q.empty():
splittedList.append(q.get())
#Store in sublist
subList.append(splittedList)
#output
print(subList)
>python split.py
[['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j'], ['k', 'l', 'm', 'n', 'o'], ['p', 'q', 'r', 's', 't'], ['u', 'v', 'w', 'x', 'y'], ['z']]
We received a comment from @shiracamus. Thank you very much.
Recommended Posts