I have this list:
l=['abcdef', 'abcdt', 'neft', 'ryr', 'yyyyy', 'u', 'aaaaaaaaaa']
and, the length of each elements in the above list is 6,5,4,3,5,1,10
respectively.
I wish to combine it in a way such that it satisfies a condition: Each element in the newly created list should be at least of length 10, that means, consider the next elements for combining until the desire length is reached. Spaces will be added at every joining point.
Thus, the list now becomes:
l=['abcdef abcdt', 'neft ryr yyyyy', 'u aaaaaaaaaa']
I tried combining it up based on iterations and other ways but nothing seems to work. Any suggestions?
答案 0 :(得分:3)
You could use a generator that takes items from the iterable as long as the length requirement isn't fulfilled:
def join_while_too_short(it, length):
it = iter(it)
while True:
current = next(it)
while len(current) < length:
current += ' ' + next(it)
yield current
When running this on your input it produces the correct result:
>>> l = ['abcdef', 'abcdt', 'neft', 'ryr', 'yyyyy', 'u', 'aaaaaaaaaa']
>>> list(join_while_too_short(l, 10))
['abcdef abcdt', 'neft ryr yyyyy', 'u aaaaaaaaaa']
It won't be really efficient because it constantly formats the strings, you could also collect them as list and join them before yielding, but this version should be clearer about the principle.
Note that the requirement may not always be fulfilled because there might not be enough items at the end to create a string of the desired length. However you said that you want to "consider the next elements for combining until the desire length is reached". And the presented approach does exactly that.
答案 1 :(得分:1)
Just running through once and appending until you reach the condition should work fine. As far as I know you can't list comprehend your way into multi row operations for a list, but if Pandas is an option let us know by editing your question. Pandas Dataframe shift method will let you examine multiple rows at a time in a lambda function and solve this problem in a non for loop way.
l=[ 'abcdef', 'abcdt', 'neft', 'ryr', 'yyyyy', 'u', 'aaaaaaaaaa' ]
newlist = list()
newitem = ''
for item in l:
if len(newitem) == 0:
newitem = item
else:
newitem = newitem +" "+ item
if len(newitem) > 9:
newlist.append(newitem)
newitem=''
if len(newitem)>0: # grab any left over stuff that was <10 digits at the end
newlist.append(newitem)
print (newlist)
the output from jupyter running Python 3.6 is as you expect
['abcdef abcdt', 'neft ryr yyyyy', 'u aaaaaaaaaa']
答案 2 :(得分:0)
This should work, note that the list is modified, if you don't want that, make a copy before. Tested with the case you gave.
def combine(a, n):
i = 0
while i < len(a):
if len(a[i]) >= n:
i += 1
elif i + 1 < len(a):
a[i:i + 2] = [a[i] + " " + a[i + 1]]
elif len(a) > 1:
a[i - 1:i + 1] = [a[i - 1] + " " + a[i]]
break
else:
break