Hi I was wondering if someone could help me to figure out how to write a code for :

Write a function print_table which consumes two parameters, a list of list of strings,
and an integer for the length of the list. The function should print out the strings from
each sublist on each line. Commas and spaces are used to separate strings on the same
line. If the list is empty, then an empty string will be printed out. You may use the
above defined function print_row as helper function.
As an example, if the input list is defined as [[”Jack”, ”Smith”, ”CS”, ”ON”], [”
Alice”, ”Adam”, ”Math”, ”SK”], [”Jean”, ”Guy”, ”PHY”, ”QC”], [”Kim”, ”King
”, ”ENGL”, ”NS”]], the function should have the following printed out,
Jack, Smith, CS, ON
Alice, Adam, Math, SK
Jean, Guy, PHY, QC
Kim, King, ENGL, NS


I did manage to write the code for the print_row function:
def print_row(row):
x= ' '
print helper(row,x)

def helper(l,s):
if len(l) > 1 :
s = s + l[0] + ', '
return helper(l[1:],s)
else:
return s + l[0]

oh, and we are suppose to use lambda, and abstract list functions if we can. thanks, ^^

I'm not going to solve this for you, but I'm going to give you some hints.

You can use the string method

join

to combine a list of strings with a separator to one string.

I'm not going to solve this for you, but I'm going to give you some hints.

1. You can use the string method join to combine a list of strings with a separator to one string:

>>> ', '.join(['Jack', 'Smith', 'CS', 'ON'])
'Jack, Smith, CS, ON'

2. You can use the map function in conjunction with lambda expressions to transform a list of items in one go:

>>> map(lambda s: s.upper(), ['Jack', 'Smith', 'CS', 'ON'])
['JACK', 'SMITH', 'CS', 'ON']

3. Given the above hints, you should be able to write a function that receives a 2D list of strings as argument and prints it the way you need it that is a single line of code only. :)

def printStringTable(data):
    # one line to rule it all :-)

Your turn :)

Thanks a lot!! it was really helpful ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.