-1

I have a preexisting function that parses the data of an html table .

def parse_table(lines):
       .......

I'd like to be able to reuse this function, but in order to do so I have to provide the 'lines' variable which is the format of multi line text string that looks like:

a
b
c
d
e
f
.....

where all letters are text strings.

So far I have been able to parse the table into a list of lists ( which each list representing a row ) that looks like:

  [[u'a',u'b',u'c'],[u'd',u'e',u'f'],...]

How can I turn my list of lists into the needed format?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • what format do you need, what is your expected output? – Padraic Cunningham Aug 25 '15 at 19:14
  • So, you want to turn a 2D `list` into a formatted HTML table? – TigerhawkT3 Aug 25 '15 at 19:14
  • 2
    It's not clear from your example what the expected output is. Does this boil down to flattening a list of lists and then joining the contents with a newline? If so, just read any of the many questions on flattening lists ([1](https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python), [2](https://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python), etc.), and use [join](https://stackoverflow.com/questions/4284648/converting-lists-of-tuples-to-strings-python). – Two-Bit Alchemist Aug 25 '15 at 19:15

1 Answers1

3

A quick one-liner will produce your string.

table = [[u'a1',u'b2',u'c3'],[u'd4',u'e5',u'f6'],...]
lines = "\n".join(sum(table, [])) + "\n" # if you want a trailing newline

If you don't like the idea of using sum to flatten a list of lists by "adding" them up, you can use a list comprehension instead. (This is also far faster than using sum.)

lines = "\n".join([item for row in table for item in row])

The fastest technique seems to be

lines = "\n".join(list(itertools.chain.from_iterable(table))

which is almost twice as fast as the list comprehension, but not available in Python 2.6 (although itertools.chain(*table)) will work in its place).

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you, I've read over https://docs.python.org/2/library/functions.html#sum , but could I ask you to discuss the "sum(table, []) " portion of your answer? – user1592380 Aug 25 '15 at 19:40
  • All that does is connect all the individual `list`s in the `table`. `sum()` doesn't automatically detect what you're adding together, so you have to explicitly tell it to start with an empty `list` (`[]`) instead of the default (`0`). – TigerhawkT3 Aug 25 '15 at 19:49
  • Use of `sum` is a border-line hack; it works since `+` is defined as concatenation for lists, but you don't really think lists as something you can sum up. – chepner Aug 25 '15 at 19:56
  • 1
    Since @ryachza answer got criticized for performance, I feel that the `sum` part of this answer deserves such criticism as well: https://mathieularose.com/how-not-to-flatten-a-list-of-lists-in-python/ Either way it's a cool hack which I learned today. – Audrius Kažukauskas Aug 25 '15 at 20:05
  • The other answer sacrifices performance just to reduce readability. This answer does not. (However, I'd probably use `itertools.chain` for this.) – TigerhawkT3 Aug 25 '15 at 20:06
  • Thanks guys, I'm also getting some good info from your comments – user1592380 Aug 25 '15 at 20:08
  • BTW, the second example can avoid creating intermediate list by using generator expression: `'\n'.join(item for row in table for item in row)` – Audrius Kažukauskas Aug 26 '15 at 06:21
  • `join` has to fully consume the generator in order to allocate storage before it performs the actual join, so passing an explicit list doesn't save much. – chepner Aug 26 '15 at 15:29