Print two-dimensional list

I have a list in which there is another list and I want doc.write(a)

 a = [[1, 2, "hello"], [3, 5, "hi There"], [5,7,"I don't know"]] doc.write(''.join(a)) TypeError: sequence item 0: expected str instance, list found 

How can I handle this, should I make a for loop that I join and add all the sublists?

The real goal was to make it somehow human-readable, but I didn’t want a ready-made solution from you.

+10
source share
7 answers

You can try something like

 >>> a = [[1, 2, "hello"],[3, 5, "hi There"],[5,7,"I don't know"]] >>> >>> ''.join(str(r) for v in a for r in v) "12hello35hi There57I don't know" 

i.e.

 doc.write(''.join(str(r) for v in a for r in v)) 
+13
source

There are various legal things that you can do, and no one has been given to say which one is right without knowing which one you need.


At first you can simply write str or repr from a :

 >>> a=[[1, 2, "hello"],[3, 5, "hi There"],[5,7,"I don't know"]] >>> repr(a) '[[1, 2, \'hello\'], [3, 5, \'hi There\'], [5, 7, "I don\'t know"]]' 

Note that this is what print does (it prints str of what you give it), although str is identical to repr , both are effectively '[' + ', '.join(map(repr, self)) + ']' ).


Secondly, you can use a format designed for persistent data storage, for example JSON:

 >>> json.dumps(a) '[[1, 2, "hello"], [3, 5, "hi There"], [5, 7, "I don\'t know"]]' 

Thirdly, you can combine the representation of each element a some way of your choice, which is trivial with map or understanding. For instance:

 >>> '[' + ', '.join(map(repr, a)) + ']' '[[1, 2, \'hello\'], [3, 5, \'hi There\'], [5, 7, "I don\'t know"]]' 

... or...

 >>> 'My stuff includes: ' + ','.join(map(repr, a)) + '\n' 'My stuff includes: [1, 2, \'hello\'],[3, 5, \'hi There\'],[5, 7, "I don\'t know"]\n' 

Or you can do the same recursively.

Or you can flatten the list (for example, hide it one step with itertools.chain or recursively with recipes from itertools documents or with the more-itertools ), and then run the line, but you want, and then join them.

Or you can just write the word LIST .

All these are absolutely correct things to pass to write .

+3
source

List optimization would be a better choice:

 >>> ''.join([str(item) for sublist in a for item in sublist]) "12hello35hi There57I don't know" 

This is the most recommended approach in a similar SO question , given performance and syntax.

+3
source

I also searched for the answer to this question. After reading the comments, here is what I came up with:

I also searched for the answer to this question. After reading the comments, here is what I came up with:

 ','.join(str(' '.join(str(x) for x in v)) for v in a) 

This creates something like:

 1 2 hello,3 5 hi There,5 7 I don't know 

If you want all spaces to be used as separators, use "" instead of "" at the beginning.

+1
source

How about using itertools?

 from itertools import chain doc.write(''.join(map(str, chain(a)))) 

As an alternative:

 doc.write(''.join(str(i) for sub_list in a for i in sub_list)) 

You suggested using a for loop. You can really do this, although options are probably better.

 new_a = [] for sub_list in a: for i in sublist: new_a.append(str(i)) doc.write(''.join(new_a)) 

This is basically the previous option, but is unfolding.

If you just want to write the first list, in this case you can do this:

 doc.write(''.join(map(str, a[0]))) 
0
source

It’s hard for me to be sure, because your question is too short, but it seems to me that you have an XY problem, that is:
you ask a question about problem Y that you think of as a solution to get away from problem X. But your real problem is that you think problem Y is a way to answer the real problem X, and what you represent here is only problem Y.
By writing this, I will only rephrase what is said here: Problem XY

If I am right, I believe that you will have a better way to solve your real X problem using one of the following tools that allow you to serialize the object and write the serialized object to a file:

pickle

marshal

shelve

I will not paraphrase and repeat all the documents of these tools, read them.

.

If I'm wrong and that you just want to write an object in the form of a string representation, you can also do:

 from pprint import pformat a = [[1, 2, "hello"], [3, 5, "hi There"], [5,7,"I don't know"]] with open('doc.txt','w') as f: f.write(pformat(a,width=12)) 
0
source

You can also just use a simple list comprehension as follows:

 doc.write([x for x in i for i in a]) 
0
source

Source: https://habr.com/ru/post/957152/


All Articles