Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list to string
#1
"""
Create a function called list2str, that takes two arguments - a list of
strings L, and a delimeter D. The function must:
  1. Return a single string, containing all of the input strings joined together
  with D as a delimeter between them. The result should not start or end with
  the delimeter D - D should be used only between the substrings.
  2. When inspected, would return the following help string:
    Join the list L using the delimeter D


For example:
  list2str( ['a','b','c'], ',') -> 'a,b,c'
  list2str('We','are','the','location','cloud','.') -> 'We are the location cloud.'
"""
Reply
#2
Same applies as to your other post, this is your homework assignment that you copy pasted.
What is your question ?
Reply
#3
You really should try before posting. We don't do people's work for them, only help when they have an issue.

That said, the second test case doesn't match the explanation given. Why isn't the list of strings a single list, instead of individual arguments? And why isn't the output "We.are.the.location.cloud"?
Reply
#4
def list2str(*args):
    x = args[0]
    y = args[1]
    p = len(x)
    z = ''
    for i in range(p):
        q = x[i] + y
        z+=str(q)
    R = str(z)[:-1]
    print ("'"+R+"'")

a = ['We','are','the','location','cloud']
b = '.'

list2str(a,b)
Reply
#5
Stop posting code with no details. Give a natural language explanation of the problem. Say your expected vs. actual output and describe what's wrong. You've also been told in another thread that using *args that way is not in line with your assignment, but it's also much less readable. Lastly, using single-letter variable names, especially as many as you are here, is very difficult to read - please use meaningful names (it's ok to use names prescribed by your assignment, as in another thread, but that's not what's happening here).
Reply
#6
That fails the example output, though:
>>> def list2str(*args):
...     x = args[0]
...     y = args[1]
...     p = len(x)
...     z = ''
...     for i in range(p):
...         q = x[i] + y
...         z+=str(q)
...     R = str(z)[:-1]
...     print ("'"+R+"'")
...
>>> a = ['We','are','the','location','cloud']
>>> b='.'
>>> list2str(a,b)
'We.are.the.location.cloud'
The output of yours is "We.are.the.location.cloud" when it should be "We are the location cloud.".  Also, you're calling it differently than the example usage, as you're doing list2str(['We', 'are', 'the', 'location', 'cloud'], '.'), while the example usage is list2str('We', 'are', 'the', 'location', 'cloud', '.').  That said, yours does work fine for the first test case:
>>> list2str( ['a','b','c'], ',')
'a,b,c'
You should either contact your professor to see if that expected input/output is correct for the second case, or rework your function to handle both situations.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020