Just as you wrote it:
>>> matrix = [["str1", "str2"], ["str3"], ["str4", "str5"]]
>>> matrix
[['str1', 'str2'], ['str3'], ['str4', 'str5']]
>>> matrix[0][1]
'str2'
>>> matrix[0][1] += "someText"
>>> matrix
[['str1', 'str2someText'], ['str3'], ['str4', 'str5']]
>>> matrix[0].extend(["str6"])
>>> matrix[0]
['str1', 'str2someText', 'str6']
Just think about 2D matrix as list of the lists. Other operations also work fine, for example,
>>> matrix[0].append('value')
>>> matrix[0]
[0, 0, 0, 0, 0, 'value']
>>> matrix[0].pop()
'value'
>>>
Answer from Klark on Stack Overflow2D array of lists in python - Stack Overflow
Strings in 2D lists in Python - Stack Overflow
How do I merge a 2D array in Python into one string with List Comprehension? - Stack Overflow
convert a string 2d list back to 2d list in python - Stack Overflow
Just as you wrote it:
>>> matrix = [["str1", "str2"], ["str3"], ["str4", "str5"]]
>>> matrix
[['str1', 'str2'], ['str3'], ['str4', 'str5']]
>>> matrix[0][1]
'str2'
>>> matrix[0][1] += "someText"
>>> matrix
[['str1', 'str2someText'], ['str3'], ['str4', 'str5']]
>>> matrix[0].extend(["str6"])
>>> matrix[0]
['str1', 'str2someText', 'str6']
Just think about 2D matrix as list of the lists. Other operations also work fine, for example,
>>> matrix[0].append('value')
>>> matrix[0]
[0, 0, 0, 0, 0, 'value']
>>> matrix[0].pop()
'value'
>>>
You can either do it with the basic:
matrix = [
[["s1","s2"], ["s3"]],
[["s4"], ["s5"]]
]
or you can do it very genericially
from collections import defaultdict
m = defaultdict(lambda : defaultdict(list))
m[0][0].append('s1')
In the defaultdict case you have a arbitrary matrix that you can use, any size and all the elements are arrays, to be manipulated accordingly.
No, it shows everything there is, to remove it, you need to do some string replacing stuff:
print(str(shape).replace("'", '').replace('], [', '],\n ['))
Which outputs:
[[, , ],
[, , ],
[, , ]]
If it's just about printing something to the screen, you don't need to first create a list, just create a string like this:
print("[ , , ]\n[ , , ]\n[ , , ]")
Or even
print("[ | | ]\n[ | | ]\n[ | | ]")
Which I think looks better:
[ , , ]
[ , , ]
[ , , ]
[ | | ]
[ | | ]
[ | | ]
Like so:
[ item for innerlist in outerlist for item in innerlist ]
Turning that directly into a string with separators:
','.join(str(item) for innerlist in outerlist for item in innerlist)
Yes, the order of 'for innerlist in outerlist' and 'for item in innerlist' is correct. Even though the "body" of the loop is at the start of the listcomp, the order of nested loops (and 'if' clauses) is still the same as when you would write the loop out:
for innerlist in outerlist:
for item in innerlist:
...
Try that:
li=[[0,1,2],[3,4,5],[6,7,8]]
li2 = [ y for x in li for y in x]
You can read it like this:
Give me the list of every ys.
The ys come from the xs.
The xs come from li.
To map that in a string:
','.join(map(str,li2))
Use ast.literal_eval
>>> list_string = '[[1, 1], [2, 2], [3, 3]]'
>>> import ast
>>> type(ast.literal_eval(list_string))
<type 'list'>
using eval
>>> l = [[1,1] , [2,2] , [3,3]]
>>> list_string = str(l)
>>> eval(list_string)
[[1, 1], [2, 2], [3, 3]]
but eval should be used with caution, right? a thread on eval safety.
This works as well, close to what you tried with list:
import numpy as np
print np.array(list_string)
I am trying to take a string at a specific index and store it so I can manipulate it and then place it back into the 2d array in a different column. My question is regarding the where I actually try to store it.
table is a 2d array of strings here
for rowpos in range(maxnumofrows) : { stringtoparse = table[rowpos][0] }
the stringtoparse setting line is whats giving me errors. I'm normally a java user, but I need to use python here. Any obvious reason why it doesn't work? I tried using the str() method to copy the data of the position because I thought perhaps making a pointer to an index in a 2d array was not allowed in python or something, but that did not fix the issue and the syntax error still remains.
Python 3.x
print ( [list( map(int,i) ) for i in l] )
Output :
[[1, 1, 3], [2, 3, 5], [3], [4, 5], [5, 1], [6, 6], [7]]
Do with list comprehension,
In [24]: l = [['1', ' 1', ' 3'], ['2', ' 3', ' 5'], ['3'], ['4', ' 5'], ['5', ' 1'], ['6', ' 6'], ['7']]
In [25]: result = [map(int,i) for i in l]
Result
In [26]: print result
[[1, 1, 3], [2, 3, 5], [3], [4, 5], [5, 1], [6, 6], [7]]
You can assign the 2D list (list is the term for an array used in Python):
data = [["a", "b", "c", "d"], ["e", "f", "g", "h"]]
Next you can loop through each list inside the data list and assign the new value to the original value in the list:
idx = 0
for x in data:
data[idx] = "".join(x)
idx += 1
This will return the required output.
Full Code:
data = [["a", "b", "c", "d"], ["e", "f", "g", "h"]]
idx = 0
for x in data:
data[idx] = "".join(x)
idx += 1
Test your code by adding the following two lines:
print(f'First value in list: {data[0]} and second value in list: {data[1]}')
Explanation:
data = [["a", "b", "c", "d"], ["e", "f", "g", "h"]]
The line of code above assigns the 2D list to a variable to be used on later.
idx = 0
The variable idx is used to assign the required value to each of the former values of the 2D list data. You can also assign each value to a separate list by using the .append method.
for x in data:
This loops through each value (or list) inside of the list data, using the variable x to be referenced as in the code. This can also be done using a while loop.
data[idx] = "".join(x)
This assigns the former value with the index of idx in the list with the new value. This uses the python .join method to connect the values in the list by a "".
To read more about the Python .join method visit:
https://www.w3schools.com/python/ref_string_join.asp
idx += 1
This is used to add 1 to the former value of idx so that it outputs the new index of the next value in the list used in the for loop.
Testing Your Code
print(f'First value in list: {data[0]} and second value in list: {data[1]}')
the above line of code is a simple formatted string that outputs the first data[0] and second data[1] values of the variable data.
I hope this helps you with your coding and understanding your code.
You can try:
>>> data = [["a", "b", "c", "d"], ["e", "f", "g", "h"]]
>>> ["".join(d) for d in data]
['abcd', 'efgh']