Q1: Instead of giving 'practice' as max string why is max(word1,max(t0)) giving tanice
Strings are ordered lexicographically. t
> p
so tanice
is greater than practice
Q2: t=max(word1,max(t0)) works but max(word1,nwords) doesn't. Why & Is there a workaround to this?
This is because max
expects either an iterable, or a variable number of arguments. In the latter case, max
is trying to compare a string to a list rather than a string to each element in the list. You could use itertools.chain
:
max(chain([word1],nwords),key=len) #assuming you're comparing based on length.
Q3: ...
I'm not really sure what you're going for here. Based on the description, it looks like you want to chain the strings after zipping them together:
from itertools import chain,izip_longest
''.join(chain.from_iterable(izip_longest(word1,*nwords,fillvalue='')))
Here's an example without the function:
>>> from itertools import chain,izip_longest
>>> words = ('janice','tanice','practice')
>>> ''.join(chain.from_iterable(izip_longest(*words,fillvalue='')))
'jtpaarnnaiicccteeice'
The unpacking operator goes both ways. You can use it to say "I want this function to have a variable number of positional arguments":
def foo(*args): ...
Or, "I want to pass this function a variable number of positional arguments":
foo(*iterable)
Here I've used the second form to pass a variable number of strings to izip_longest
1 which takes an arbitrary number of positional arguments.
The above code is equivalent to:
''.join(chain.from_iterable(izip_longest('janice','tanice','practice',fillvalue='')))
1zip_longest
on python3.x