I have a list, in which is another list and I want to 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, do I have to make a for-loop in which I join and add all the sublists?
The real goal was to make it somehow readable for human beeing, but I didn't wanted a finished solution from you.
myList = [0,1,2,3,4,5,6,7,8,9]; for index in len(myList): myList[index] = 0 # Set element at "index" to 0. For a two-dimensional list, in order to reference every element, we must use two nested loops. This gives us a counter variable for every column and every row in the matrix.
A two-dimensional list can be considered as a “list of lists”. A two-dimensional list can be considered as a matrix where each row can have different lengths and supports different data types.
A 2D array in Python is a nested data structure, meaning it is a set of arrays inside another array. The 2D array is mostly used to represent data in tabular or two-dimensional format.
A 2D array uses two indices to represent the exact location of an element in the memory space. By giving both coordinates to the array definition, one can print the value of the element stored at the given memory location.
There are different legal things you can do, and no way for anyone to say which one is right without knowing which one you want.
First, you can just write the str
or repr
of 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 the str
of whatever you give it—although with a list, the str
is identical to the repr
; they're both effectively '[' + ', '.join(map(repr, self)) + ']'
).
Second, you could use a format that's designed for data persistent, like JSON:
>>> json.dumps(a)
'[[1, 2, "hello"], [3, 5, "hi There"], [5, 7, "I don\'t know"]]'
Third, you can join together the repr of each element of a
in some way of your choosing, which is trivial with a map
or a comprehension. For example:
>>> '[' + ', '.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 thing recursively.
Or you can flatten the list (e.g., flatten it one step with itertools.chain
, or recursively with the recipes from the itertools
docs or with the more-itertools
package) and then stringify the pieces however you want and then join them up.
Or you can just write the word LIST
.
All of those are perfectly valid things to pass to write
.
I was looking for an answer to this as well. After reading the comments here, this is what I came up with:
I was looking for an answer to this as well. After reading the comments here, this 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 as delimiters, then use ' ' instead of ',' at the front.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With