How to print a vector of integers by removing the last comma in Python 3?

I want to print on the screen a vector of integers in the same line and separated by comma, and I want to remove the comma that is after the last number.

How could I do this in Python?

My code looks like this:

for i in range(12):
    print(numeros[i],end=",")
Author: LINQ, 2017-05-22

2 answers

In a Pythonic way it is possible to concatenate the elements of the list into a comma-separated string. The map serves to convert the values of the list (created by range()) to string.

numeros = ','.join(map(str, range(12))) 
print(numeros)

Or maybe so, doing the conversion to string using list comprehension :

numeros = ','.join([str(x) for x in range(12)])
print(numeros)

Is also possible by making a if. This is a approach minus Pythonic , but it is much simpler to understand, especially if you are learn.

for i in range(12):
    if(i != 11):
        print(i, end=",")
    else: 
        print(i)
 2
Author: LINQ, 2017-05-23 11:54:30

Python has this idea of having a way that is the obvious of solving things - but not always the obvious is so obvious at first glance. This problem always gets ugly in other languages, precisely because you have to leave a special case for the last element.

So in the case of a string that you don't know the length beforehand, (for example, if you are reading from a file) in other languages you may have to use an auxiliary variable to know if it is the first interaction, otherwise print the", " at the beginning of the loop:

primeiro = True
for i in sequencia:
    if not primeiro:
        print(", ", end="")
        primeiro = False
    print(i)
print()

If the sequence has known length, such as a list, one can do an "if" and not print the", " for the last element.

In Python, strings have the join method which is quite interesting: it concatenates strings from a sequence using the initial string as a separator. So it never prints the separator either at the beginning or at the end of a sequence.

So if I have:

a = ["Universidade", "de", "Campinas"]
b = " ".join(a)
print(b)

Will display

University of Campinas

The only problem is that being a string method, the "join" expects all the elements of the sequence it will concatenate to be themselves strings.

Here comes another element of Python-the language has a syntactic way to solve synthetically everything that solves repetition (and filters) of a sequence - the so-called "Mapping and filtering" problems: you use a for as part of an expression: you first put the expression you want to use to transform all the elements of the sequence, and then write the " for " - for example:

(elemento * 2 for elemento in sequencia)

Would result in an iterator that delivers each element of the initial" sequence " multiplied by 2.

So since we want all elements of the number vector turned into a string before calling the "join", just do:

print(", ".join(str(i) for i in range(12)))

To make your impression of the elements interspersed with",".

(Note that there is a difference if the "for" of an expression is bracketed ([x for x in y])- this always generates a list, with all the elements in memory - without the additional brackets, you have a generator that can be used only once, with the difference that the elements are computed as they are used and are never saved all in memory. For an immediate flame to join this makes no difference).

 3
Author: jsbueno, 2017-05-23 11:27:23