简体   繁体   中英

Print range of numbers on same line

Using python I want to print a range of numbers on the same line. how can I do this using python, I can do it using C by not adding \n , but how can I do it using python.

for x in xrange(1,10):
    print x

I am trying to get this result.

1 2 3 4 5 6 7 8 9 10
>>>print(*range(1,11)) 
1 2 3 4 5 6 7 8 9 10

Python one liner to print the range

Python 2

for x in xrange(1,11):
    print x,

Python 3

for x in range(1,11):
    print(x, end=" ") 
for i in range(10):
    print(i, end = ' ')

You can provide any delimiter to the end field (space, comma etc.)

This is for Python 3

str.join would be appropriate in this case

>>> print ' '.join(str(x) for x in xrange(1,11))
1 2 3 4 5 6 7 8 9 10 

This is an old question, xrange is not supported in Python3.

You can try -

print(*range(1,11)) 

OR

for i in range(10):
    print(i, end = ' ')

Same can be achieved by using stdout .

>>> from sys import stdout
>>> for i in range(1,11):
...     stdout.write(str(i)+' ')
...
1 2 3 4 5 6 7 8 9 10 

Alternatively, same can be done by using reduce() :

>>> xrange = range(1,11)
>>> print reduce(lambda x, y: str(x) + ' '+str(y), xrange)
1 2 3 4 5 6 7 8 9 10
>>>
[print(i, end = ' ') for i in range(10)]
0 1 2 3 4 5 6 7 8 9

This is a list comprehension method of answer same as @Anubhav

for i in range(1,11):
    print(i)

i know this is an old question but i think this works now

Though the answer has been given for the question. I would like to add, if in case we need to print numbers without any spaces then we can use the following code

        for i in range(1,n):
            print(i,end="")

另一个单行 Python 3 选项,但带有显式分隔符:

print(*range(1,11), sep=' ')

n = int(input())
for i in range(1,n+1):
    print(i,end='')

Use end = " " , inside the print function

Code:

for x in range(1,11):
       print(x,end = " ")

Here's a solution that can handle x with single or multiple rows like scipy pdf:

from scipy.stats import multivariate_normal as mvn

# covariance matrix
sigma = np.array([[2.3, 0, 0, 0],
           [0, 1.5, 0, 0],
           [0, 0, 1.7, 0],
           [0, 0,   0, 2]
          ])
# mean vector
mu = np.array([2,3,8,10])

# input
x1 = np.array([2.1, 3.5, 8., 9.5])
x2 = np.array([[2.1, 3.5, 8., 9.5],[2.2, 3.6, 8.1, 9.6]])


def multivariate_normal_pdf(x, mu, cov):
    x_m = x - mu

    if x.ndim > 1:
        sum_ax = 1
        t_ax = [0] 
        t_ax.extend(list(range(x_m.ndim)[:0:-1])) # transpose dims > 0
    else:
        sum_ax = 0
        t_ax = range(x_m.ndim)[::-1]


    x_m_t = np.transpose(x_m, axes=t_ax) 
    A = 1 / ( ((2* np.pi)**(len(mu)/2)) * (np.linalg.det(cov)**(1/2)) )
    B = (-1/2) * np.sum(x_m_t.dot(np.linalg.inv(cov)) * x_m,axis=sum_ax)
    return A * np.exp(B)

print(mvn.pdf(x1, mu, sigma))
print(multivariate_normal_pdf(x1, mu, sigma))

print(mvn.pdf(x2, mu, sigma))
print(multivariate_normal_pdf(x2, mu, sigma))

For Python 3:

for i in range(1,10): 
    print(i,end='')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM