Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Sayth Renshaw
> > Well, your code was close. All you needed was a little tweak > to make it work like you requested. So keep working at it, > and if you have a specific question, feel free to ask on the > list. > > Here's a tip. Try to simplify the problem. Instead of > looping over a list of lists, and then a

Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Sayth Renshaw
> > Then using this cool answer on SO [...] > > Oh. I thought you wanted to learn how to solve problems. I had no idea you > were auditioning for the James Dean part. My bad. Awesome response burn lol. I am trying to solve problems. Getting tired of dealing with JSON and having to figure out

Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Peter Otten
Sayth Renshaw wrote: > >> myjson = ... >> path = "['foo']['bar'][42]" >> print(eval("myjson" + path)) >> >> ? >> >> Wouldn't it be better to keep 'data' as is and use a helper function like >> >> def get_value(myjson, path): >> for key_or_index in path: >> myjson = myjson[key_or_in

Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Sayth Renshaw
> myjson = ... > path = "['foo']['bar'][42]" > print(eval("myjson" + path)) > > ? > > Wouldn't it be better to keep 'data' as is and use a helper function like > > def get_value(myjson, path): > for key_or_index in path: > myjson = myjson[key_or_index] > return myjson > > path

Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Peter Otten
Sayth Renshaw wrote: > I have data which is a list of lists of all the full paths in a json > document. > > How can I change the format to be usable when selecting elements? How do you want to select these elements? myjson = ... path = "['foo']['bar'][42]" print(eval("myjson" + path)) ? Would

Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Thomas Jollans
On 24/07/18 08:25, Mark Lawrence wrote: > On 24/07/18 06:41, Sayth Renshaw wrote: >> On Tuesday, 24 July 2018 14:25:48 UTC+10, Rick Johnson  wrote: >>> Sayth Renshaw wrote: >>> elements = [['[{0}]'.format(element) for element in elements]for elements in data] >>> >>> I would suggest you a

Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Vlastimil Brom
2018-07-24 3:52 GMT+02:00, Sayth Renshaw : > I have data which is a list of lists of all the full paths in a json > document. > > How can I change the format to be usable when selecting elements? > > data = [['glossary'], > ['glossary', 'title'], > ['glossary', 'GlossDiv'], > ['glossary', 'Gloss

Re: Format list of list sub elements keeping structure.

2018-07-24 Thread Mark Lawrence
On 24/07/18 06:41, Sayth Renshaw wrote: On Tuesday, 24 July 2018 14:25:48 UTC+10, Rick Johnson wrote: Sayth Renshaw wrote: elements = [['[{0}]'.format(element) for element in elements]for elements in data] I would suggest you avoid list comprehensions until you master long-form loops. I

Re: Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw
On Tuesday, 24 July 2018 14:25:48 UTC+10, Rick Johnson wrote: > Sayth Renshaw wrote: > > > elements = [['[{0}]'.format(element) for element in elements]for elements > > in data] > > I would suggest you avoid list comprehensions until you master long-form > loops. I actually have the answer ex

Re: Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw
On Tuesday, 24 July 2018 14:25:48 UTC+10, Rick Johnson wrote: > Sayth Renshaw wrote: > > > elements = [['[{0}]'.format(element) for element in elements]for elements > > in data] > > I would suggest you avoid list comprehensions until you master long-form > loops. My general issue is that I wa

Re: Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw
I am very close to the end result. I now have it as Output [ ['[glossary]'], ['[glossary]', '[title]'], ['[glossary]', '[GlossDiv]'], ['[glossary]', '[GlossDiv]', '[title]'], ['[glossary]', '[GlossDiv]', '[GlossList]'], ['[glossary]', '[GlossDiv]', '[GlossList]', '[GlossEntry

Re: Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw
> > > > for item in data: > > for elem in item: > > out = ("[{0}]").format(elem) > > print(out) > > Hint: print implicitly adds a newline to the output string. So collect all > the values of each sublist and print a line-at-time to output, or use the > end= argument of Py3's p

Format list of list sub elements keeping structure.

2018-07-23 Thread Sayth Renshaw
I have data which is a list of lists of all the full paths in a json document. How can I change the format to be usable when selecting elements? data = [['glossary'], ['glossary', 'title'], ['glossary', 'GlossDiv'], ['glossary', 'GlossDiv', 'title'], ['glossary', 'GlossDiv', 'GlossList'], ['