Hướng dẫn print line number python

I was trying to create a program which tells me in a given text if there are 2 adjacent words which are the same and where in the text file does this happen[ line and word number]. So far I have been able to determine which word number but can't seem to figure out which line it happens in. Would anyone be able to give me a hand please? So far, next to the Error/ No Error, I am able to get the word number but if I could just get the line number as well.


for line in [textfile]:
    for x, y in enumerate[zip[line.split[], line.split[][1:]]]:

asked Dec 5, 2021 at 16:10

1

Why don't you simply use enumerate again in the outer loop?

for line_number, line in enumerate[textfile]:
    for x, y in enumerate[zip[line.split[], line.split[][1:]]]:
        if[x,y]==[y,x]:
            print[line_number,x,y,"Error"]
        else:
            print[line_number,x,y,"No error"]

answered Dec 5, 2021 at 16:15

burningalcburningalc

2,5671 gold badge7 silver badges27 bronze badges

You could create a counter where you establish an integer and add 1 to the integer every time you iterate over a line. For example:

file_name = "whatever.txt"
textfile = open[file_name, "r"]
line_number = 0 # this integer will store the line number
for line in [textfile]:
    line_number += 1
    for x, y in enumerate[zip[line.split[], line.split[][1:]]]:
        if[x,y]==[y,x]:
            print[x,y,"Error"]
        else:
            print[x,y,"No error"]

answered Dec 5, 2021 at 16:18

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

Nội dung chính

  • Modify print[] method to print on the same line
  • Print on same line with space between each element
  • Print on same line without space between elements
  • Print on same line with some sign between elements
  • Print without newline in Python 2.x
  • Print without newline in Python 3.x
  • Print without newline in Python 3.x without using for loop
  • How do I print an integer on one line?
  • How do you print on the same line in Python?
  • How do I print numbers in Python without space in one line?
  • How do you print a string and number in one line in Python?

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]]

The print[] method in Python automatically prints in the next line each time. The print[] method by default takes the pointer to the next line.

Example

 Live Demo

for i in range[5]:
   print[i]

Output

0
1
2
3
4

Modify print[] method to print on the same line

The print method takes an extra parameter end=” “ to keep the pointer on the same line.

The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.

Syntax

print[“…..” , end=” “]

Print on same line with space between each element

The end=” “ is used to print in the same line with space after each element. It prints a space after each element in the same line.

Example

 Live Demo

for i in range[5]:
   print[i,end=" "]

Output

0 1 2 3 4

Print on same line without space between elements

The end=”” is used to print on same line without space. Keeping the doube quotes empty merge all the elements together in the same line.

Example

 Live Demo

for i in range[5]:
   print[i,end=""]

Output

01234

Print on same line with some sign between elements

The end=”,” is used to print in the same line with a comma after each element. We can use some other sign such as ‘.’ or ‘;’ inside the end parameter.

Example

 Live Demo

for i in range[5]:
   print[i,end=","]
   print[i,end="."]

Output

0,1,2,3,4,
0.1.2.3.4.

Updated on 10-Mar-2021 14:07:42

  • Related Questions & Answers
  • How to print new line in Java?
  • How to Pretty print Python dictionary from command line?
  • How to print a blank line in C#?
  • How to print matrix without line numbers in R?
  • How can we combine multiple print statements per line in Python?
  • How to capture multiple matches in the same line in Java regex
  • How to create two line charts in the same plot in R?
  • Print level order traversal line by line in C++ Programming.
  • How to print pattern in Python?
  • Print new line and tab in Arduino
  • How to compare two different files line by line in Python?
  • How to execute Python multi-line statements in the one-line at command-line?
  • How to print a line on the console using C#?
  • How to use GridBagConstraints to layout two components in the same line with Java
  • How to print concatenated string in Python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Generally, people switching from C/C++ to Python wonder how to print two or more variables or statements without going into a new line in python. Since the python print[] function by default ends with a newline. Python has a predefined format if you use print[a_variable] then it will go to the next line automatically. 
     

    For example: 

    Python3

    print["geeks"]

    print["geeksforgeeks"]

    Will result in this: 

    geeks
    geeksforgeeks

    But sometimes it may happen that we don’t want to go to the next line but want to print on the same line. So what we can do? 
     

    For Example: 

    Input : print["geeks"] print["geeksforgeeks"]
    Output : geeks geeksforgeeks
    
    Input : a = [1, 2, 3, 4]
    Output : 1 2 3 4 

    The solution discussed here is totally dependent on the python version you are using. 
     

    Print without newline in Python 2.x

    python

    print["geeks"],

    print["geeksforgeeks"]

    a = [1, 2, 3, 4]

    for i in range[4]:

        print[a[i]],

    Output: 

    geeks geeksforgeeks
    1 2 3 4

    Print without newline in Python 3.x

    python3

    print["geeks", end =" "]

    print["geeksforgeeks"]

    a = [1, 2, 3, 4]

    for i in range[4]:

        print[a[i], end =" "]

    Output: 

    geeks geeksforgeeks
    1 2 3 4

    Print without newline in Python 3.x without using for loop

    Python3

    Output:

    1 2 3 4 5 6

    How do I print an integer on one line?

    Modify print[] method to print on the same line The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.

    How do you print on the same line in Python?

    Use a carriage return "\r" to print over the same line Use the syntax print[string, end = "\r"] to make the next stdout line begin at the beginning of the current line.

    How do I print numbers in Python without space in one line?

    To print without a new line in Python 3 add an extra argument to your print function telling the program that you don't want your next string to be on a new line. Here's an example: print["Hello there!", end = ''] The next print function will be on the same line.

    How do you print a string and number in one line in Python?

    Printing string and integer [or float] in the same line.

    +22. x = 8 print["The number is", x] OR x = 8 print["The number is " + str[x]] ... .

    +7. If you're using Python 3.6 you can make use of f strings. ... .

    +4. ... .

    +2. ... .

    you can also do x = 8 print["The number is %s" % x] #or print["The number is {0}".format[str[x]]].

    Chủ Đề