Since you favor attractiveness, want to use only one line and with format
only, you can do
'{} {} {}{}'.format('foo', 'bar', len(x), ' {}' * len(x)).format( * x) # foo bar 10 0 1 2 3 4 5 6 7 8 9
I might be missing the point of your question, but you could simply extend the approach you link to as follows:
>>> x = range(10) >>>
out = " ".join(map(str, ["foo", "bar", len(x)] + x)) >>>
out 'foo bar 10 0 1 2 3 4 5 6 7 8 9'
You can simply use the print function for this:
>>> from __future__
import print_function #Required
for Python 2
>>>
print('foo', 'bar', len(x), * x)
foo bar 10 0 1 2 3 4 5 6 7 8 9
Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.,Concatenates the members of a collection, using the specified separator between each member.,Concatenates the specified elements of a string array, using the specified separator between each element.,Concatenates all the elements of a string array, using the specified separator between each element.
public:
static System::String ^ Join(char separator, ... cli::array <System::Object ^> ^ values);
public:
static System::String ^ Join(char separator, ... cli::array <System::Object ^> ^ values);
public static string Join(char separator, params object ? [] values);
public static string Join (char separator, params object?[] values);
public static string Join(char separator, params object[] values);
static member Join : char * obj[] -> string
Public Shared Function Join(separator As Char, ParamArray values As Object()) As String
public:
static System::String ^ Join(char separator, ... cli::array <System::String ^> ^ value);
public:
static System::String ^ Join(char separator, ... cli::array <System::String ^> ^ value);
public static string Join(char separator, params string ? [] value);
public static string Join (char separator, params string?[] value);
public static string Join(char separator, params string[] value);
static member Join : char * string[] -> string
Public Shared Function Join(separator As Char, ParamArray value As String()) As String
public:
static System::String ^ Join(System::String ^ separator, System::Collections::Generic::IEnumerable<System::String ^> ^ values);
public:
static System::String ^ Join(System::String ^ separator, System::Collections::Generic::IEnumerable<System::String ^> ^ values);
public static string Join (string separator, System.Collections.Generic.IEnumerable<string> values);
public static string Join (string separator, System.Collections.Generic.IEnumerable<string> values);
public static string Join (string? separator, System.Collections.Generic.IEnumerable<string?> values);
[System.Runtime.InteropServices.ComVisible(false)]
public static string Join (string separator, System.Collections.Generic.IEnumerable<string> values);
static member Join : string * seq<string> -> string
static member Join : string * seq<string> -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
static member Join : string * seq<string> -> string
public:
static System::String ^ Join(System::String ^ separator, ... cli::array <System::Object ^> ^ values);
public:
static System::String ^ Join(System::String ^ separator, ... cli::array <System::Object ^> ^ values);
public static string Join(string separator, params object[] values);
public static string Join (string separator, params object[] values);
public static string Join(string ? separator, params object ? [] values);
[System.Runtime.InteropServices.ComVisible(false)]
public static string Join (string separator, params object[] values);
static member Join: string * obj[] - > string
static member Join : string * obj[] -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
static member Join : string * obj[] -> string
public:
static System::String ^ Join(System::String ^ separator, ... cli::array <System::String ^> ^ value);
public:
static System::String ^ Join(System::String ^ separator, ... cli::array <System::String ^> ^ value);
public:
static System::String ^ Join(System::String ^ separator, cli::array <System::String ^> ^ value);
public:
static System::String ^ Join(System::String ^ separator, cli::array <System::String ^> ^ value);
public static string Join(string separator, params string[] value);
public static string Join (string? separator, params string?[] value);
public static string Join(string separator, string[] value);
public static string Join (string separator, string[] value);
static member Join: string * string[] - > string
public:
static System::String ^ Join(char separator, cli::array <System::String ^> ^ value, int startIndex, int count);
public:
static System::String ^ Join(char separator, cli::array <System::String ^> ^ value, int startIndex, int count);
public static string Join(char separator, string ? [] value, int startIndex, int count);
public static string Join (char separator, string?[] value, int startIndex, int count);
public static string Join(char separator, string[] value, int startIndex, int count);
static member Join : char * string[] * int * int -> string
Public Shared Function Join(separator As Char, value As String(), startIndex As Integer, count As Integer) As String
The most Pythonic way to convert a list of integers ints to a list of strings is to use the one-liner strings = [str(x) for x in ints]. It iterates over all elements in the list ints using list comprehension and converts each list element x to a string using the str(x) constructor. ,You can use the list comprehension expression [str(x) for x in a] to convert each element of an integer list a to a string list.,This article shows you the simplest ways to convert a one-dimensional list consisting only of integers to a list of strings.,This basic method to convert a list of ints to a list of strings uses three steps:
Suppose you have a list:
a = [4, 3, 2, 1, 10, 14, -14]
Now, check the type of the list elements:
print(type(a[0]))
# <class 'int'>
Let’s apply the built-in function str()
, and get a list of strings using list comprehension:
print([str(x) for x in a])
#['4', '3', '2', '1', '10', '14', '-14']
Here’s how you apply the map()
function to the same list a
the following code:
a = [4, 3, 2, 1, 10, 14, -14]
print(list(map(str, a)))
#['4', '3', '2', '1', '10', '14', '-14']
This is what most people coming from a programming language such as Java and C++ would do as they don’t know the most Pythonic way of using list comprehension, yet (see Method 1).
a = [4, 3, 2, 1, 10, 14, -14]
strings = []
for element in a:
strings.append(str(element))
print(strings)
#['4', '3', '2', '1', '10', '14', '-14']
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL),Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
Dim List3 As New List(Of List(Of Integer)) Dim List1 As List(Of Integer) = { 1, 2, 5, 7 }.ToList Dim List2 As List(Of Integer) = { 5, 3, 2, 1 }.ToList List3.Add(List1) List3.Add(List2)
1, 2, 5, 7 5, 3, 2, 1
Dim str As String = String.Join(vbNewLine, List3)
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Public Shared Function DumpListOfList(Of T)(List(Of List(Of T)) list) As String
Dim builder As New StringBuilder()
For Each sublist As List(Of T) In list
builder.AppendLine(String.Join(",", sublist.ToArray()))
Next
builder.Remove(builder.Length - 2, 2)
''
Get rid of the last CrLf
Return builder.ToString()
End
''
Usage:
Dim result As String = DumpListOfList(List3)
Dim str As String = String.Join(vbNewLine, List3.Select(Function(list) String.Join(",", list)))