python list to newline separated value

柔情痞子 提交于 2019-11-28 12:54:42
"\n".join(item[0] for item in my_list)

However, what's this got to do with JSON...?

Er I'm not sure what exactly you want, but if you need to print that you could do

for l in data:
    print l[0]

or if you want to make it a flat list, you could do something like

map(lambda x: x[0], a)

or if you even just want a single string with newlines, you could do something like

"\n".join(map(lambda x: x[0], a))

Dunno if that helped at all, but wish you luck

I am not exactly sure what you want, but you may try:

nested_list = [ ["abc"], ["pqr"], ["xyz"] ]
data = "\n".join( (item[0] for item in nested_list) )

This will convert your list of lists into a string separated by newline characters.

Your code is doing what you want it to, but I imagine you're inspecting the results in the python REPL or ipython, and expecting to see new lines instead of '\n'.

In [1]: items = [["abc"], ["pqr"],["xyz"]]
In [2]: s = "\n".join(item[0] for item in items)
In [3]: s
Out[3]: 'abc\npqr\nxyz'
In [4]: print s
abc
pqr
xyz

I think you want this, though it's hard to know based on your description:

  >>> l = [["abc"],["pqr"],["xyz"]]
  >>> "".join(map(lambda a:a[0] + "\n",l))
  'abc\npqr\nxyz\n'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!