table = [[False,False,False,False,False], [False,False,True,False,True]]
#not changes------------------
for lines in table:
for item in lines:
print("%s\t" % item)
print("\n")
#-----------------------------
I want output is:
False False False False False
False False True False True
But my output is:
False
False
False
False
False
False
False
True
False
True
CodePudding user response:
print() has some extra parameters, the defaults being
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Notice the end='\n'
- basically whenever you call print(), end
is added to the end of whatever you're printing. You'll want to remove this additional \n
by doing print(value, end='')
.
This will get you:
False False False False False
False False True False True
which still doesn't seem to be what you want. Replacing the \t
with
gets you a bit closer:
False False False False False
False False True False True
And finally, since you know \n
is added to the end of print()
by default, you can probably see why print('\n')
will actually become print('\n\n')
- if you replace your print('\n')
with print()
, you'll get your desired output:
False False False False False
False False True False True
Cheers
CodePudding user response:
Option one:
table = [[False,False,False,False,False], [False,False,True,False,True]]
for lines in table:
print(str(lines).replace('[', '').replace(']', ''))
Option two:
table = [[False,False,False,False,False], [False,False,True,False,True]]
for lines in table:
print(','.join([str(item) for item in lines]))
Output:
False, False, False, False, False
False, False, True, False, True
CodePudding user response:
If you want to remove the brackets too, you need to nest one more loop which will unpack the elements of the most inner list and then add a new line for each inner list , as shown in the below code:
table = [[[False,False,False,False,False], [False,False,True,False,True]]
,[[False,False,False,False,False], [False,False,True,True,False,True,True]]]
#not changes------------------
for lines in table:
for item in lines:
for i in item:
print("%s " % i, end='')# use '\t' if you need more space in between in print("%s\t" % i, end='')
print()
# print("%s\t" % item)... commenting this as not in use
print()
#-----------------------------
Below is the output :
False False False False False
False False True False True
False False False False False
False False True True False True True
Do let me know if this is what you wanted to do or not.
CodePudding user response:
You should probably leverage a pandas here:
table = [[False,False,False,False,False], [False,False,True,False,True]]
import pandas as pd
df = pd.DataFrame(table)
for index, row in df.iterrows():
row.to_string(index=False).replace('\n', ' ')
output:
'False False False False False'
'False False True False True'