printing list of lists in python with space delimited text
You have a list of tuples. Try
for s in surfaceList:
print("The vertex are {0}".format(" ".join(str(x) for x in s)))
The
for
loop allows you to print every tuple in its own line (there are other ways but I find this one more readable than others).The
.join
together with the comprehension (to convertint
values inside the tuples tostr
) formats your tuple such that every value is separated by a space. The{0}
inside the format string is a placeholde that designates it will be replaced by the first (index 0) argument of the.format()
call.The
.format()
concatenates the text (The vertex are...) and the space separated values.
numbers string of list separated with space in Python
You can split on a tab \t
to get 2 parts. Then split the second part on a space to get all the numbers.
strings = [
"04/10/2000 02 75 30 54 86",
"04/16/2000 63 63 48 32 12",
"04/15/2000 36 47 68 09 40",
"04/14/2000 06 27 31 36 43",
"04/13/2000 03 08 41 60 87"
]
numbers = list()
numberlist = list()
for s in strings:
lst = s.rstrip().split("\t")
if len(lst) > 1:
lst = lst[1].split(" ")
numberlist.append(lst)
for n in lst:
numbers.append(n)
print(numbers)
print(numberlist)
Output
['02', '75', '30', '54', '86', '63', '63', '48', '32', '12', '36', '47', '68', '09', '40', '06', '27', '31', '36', '43', '03', '08', '41', '60', '87']
[['02', '75', '30', '54', '86'], ['63', '63', '48', '32', '12'], ['36', '47', '68', '09', '40'], ['06', '27', '31', '36', '43'], ['03', '08', '41', '60', '87']]
Python demo
Printing the elements of list inline
NOTE: If you just need to "convert" a list of integers into a string of space-separated numbers, you just need to use list.join(" ")
. I understand you just practice regex and hence is my regex answer below.
You need to pass a RegExp compiled object to the .replaceAll
to actually use a regex. Also, you need to use a raw string literal, or you will have to double your backslashes.
You can use
print(list.toString().replaceAll(new RegExp(r'^\[|,|\]'), ''));
Output: 4 1 8 3
.
Note your regex is equal to ^\[|[,\]]
that matches a [
at the start of string, or a comma or ]
anywhere inside a string. You probably wanted to match ]
only at the end of string, ^\[|,|]$
.
Printing a list of list as columns with an equal amount of space inbetween
Something like this:
sample_list = [['1','22222222222222222','333'],
['111111111','2','3'],
['1','2','3']]
Alternative 1:
# Store max lengths
l = [len(max(i, key=len)) for i in zip(*sample_list)]
for item in sample_list:
for idx in range(len(l)):
print(item[idx].ljust(l[idx]), end=' ')
print()
Alternative 2 (less readable but no artifacts)
# Store max lengths
l = [len(max(i, key=len)) for i in zip(*sample_list)]
print('\n'.join(' '.join(item[i].ljust(l[i]) for i in range(len(l)))
for item in sample_list))
Alternative 1 (python2):
# Store max lengths
l = [len(max(i, key=len)) for i in zip(*sample_list)]
for item in sample_list:
for idx in range(len(l)):
print item[idx].ljust(l[idx]), "",
print
1 22222222222222222 333
111111111 2 3
1 2 3
How do I convert a list into a string with spaces in Python?
" ".join(my_list)
You need to join with a space, not an empty string.
Related Topics
Fitting a Straight Line to a Log-Log Curve in Matplotlib
Incorrect Column Alignment When Printing Table in Python Using Tab Characters
Python Strip Hyphen from Block of String
Pick Dictionary Keys:Values Randomly
Pandas: Merging Two Columns into One With Corresponding Values
How to Manage Division of Huge Numbers in Python
Python3: Typeerror: List Indices Must Be Integers or Slices, Not Str
Replacing Special Characters in a List in Python
Navigating Through Pagination With Selenium in Python
Python Comparing Previous and Next Row Value
Make Alternate Letters Capital
Find First Non-Zero Value in Each Column of Pandas Dataframe
How to Append New Data Onto a New Line
How to Read Gz Compressed File by Pyspark