I have a list like this:
['Ww','Aa','Bb','Cc','ww','AA','BB','CC']
And continuing in such a pattern, with varying capitals and lowercases. What I want to do is join every four items in this list together. So, the resulting new list (given the one above) would look like this:
['WwAaBbCc', "wwAABBCC']
How would I go about this?
>>> L = ['Ww','Aa','Bb','Cc','ww','AA','BB','CC']
>>> [''.join(x) for x in zip(*[iter(L)] * 4)]
['WwAaBbCc', 'wwAABBCC']
my_list = ['Ww','Aa','Bb','Cc','ww','AA','BB','CC']
[''.join(my_list[i:i + 4]) for i in range(0, len(my_list), 4)]
You can use something like this:
def _get_chunks(lVals, size):
for i in range(0, len(lVals), size):
yield lVals[i: i + size]
data = ['Ww','Aa','Bb','Cc','ww','AA','BB','CC']
output = [''.join(chunk) for chunk in _get_chunks(data, 4)]
>>> ['WwAaBbCc', 'wwAABBCC']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.