|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
join()
How do i use the string.join class with multydimentional arrays?
Code:
import string matrix = [[1,2,3],[4,5,6],[7,8,9]] join = string.join(matrix) print join
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#2
|
|||
|
|||
|
join only operates on strings
http://www.python.org/doc/current/l...ng-methods.html If you are trying to convert it to display the matrix as a string, try something like this Code:
output = ""
for row in matrix:
for element in row:
output += str(element) + ' '
if len(row) > 0: output = output[:-1]
output += '\n'
|
|
#3
|
||||
|
||||
|
For small lists of data this wouldn't be much of a problem but with HUGE lists this could be slow; because strings in Python are imutable the Python interpreter has to make copies of the strings when consinating them (string + string) and etc.
This should work better: Code:
#!/usr/bin/env python l = [[1,2,3],[4,5,6],[7,8,9]] j = [] for s in l: for e in s: j.append(str(e)) print ''.join(j) it only joins the data and doesnt display it in a matrix style like yogis does but you can do this is say.. one more line by appending a '\n' in there. Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > join() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|