Python has a cursor hole and python has a cursor hole.

Source: Internet
Author: User

Python has a cursor hole and python has a cursor hole.

For convenience, this time we will not write the script separately. Just execute it step by step.
The cursor is a pointer. For example, I have
1
2
3
4
When each entry occupies one row, the initial cursor is at the position of 1 by default. When read (1) is passed, the cursor automatically goes down to next. Now it is at the position of 2, and so on, and then 3, 4 until the end. The cursor will not be returned unless the cursor is forcibly moved.
This day I wrote a script with two txts, a.txt and B .txt. Check whether the script exists from a.txt and B .txt. If so, print the script.
Example above
Create a.txt file first.
File_a = open('a.txt ', 'w') Create a.txt in the current directory. Write mode. If you are not sure about the directory, first OS. getcwd (). Otherwise, you will not be able to find it.
File_a.write ('3 \ Users \ n5 \ n4 \ n') # Write Data
File_a.close ()
File_ B = open(' B .txt ', 'w ')
For I in range (10 ):
File_ B .write (str (I) + '\ n') # Write 1 \ n2 \ n3 \ n... 9 \ n
File_ B .close ()

# Read and match below
File_a = open('a.txt ', 'R ')
File_ B = open(' B .txt ', 'R ')
For x in file_a:
X = x. strip ()
For y in file_ B:
Y = y. strip ()
If x = y:
Print x
Break
# The result is 3 \ Jun \ n, which is only 3 and 9. It should be 3 \ Jun \ n5 \ n4 \ n.
File_a.close ()
File_ B .close ()

# As a result, I changed my mind for an hour and thought it was 3. Then I thought It was useless. I thought about it carefully. It turned out to be a cursor problem, Nima's
# I will explain that, because the file_a type is file, and the file type uses pointers, it is the same as what I mentioned above. The cursor will not return unless it is forcibly moved.
# That is to say, when the for y in file_ B in the nested loop matches 3, the cursor stops at 3, and the next is 9, then the second round is to start searching from 3, after 9 and 9 are found, no more, so no match can be found.
# It is easy to know where the cause is. There are two methods: first, use seek (0) to move the cursor to the beginning.
File_a = open('a.txt ', 'R ')
File_ B = open(' B .txt ', 'R ')
For x in file_a:
X = x. strip ()
For y in file_ B:
Y = y. strip ()
If x = y:
Print x
File_ B .seek (0)
Break
File_a.close ()
File_ B .close ()


# The second method is to use readlines to read the data in the file into a list by row. list has no cursor concept, and list only has subscript. Each time it cyclically starts from scratch
File_a = open('a.txt ', 'R ')
File_ B = open(' B .txt ', 'R ')
File_ B _list = file_ B .readlines () # Only change file_ B, because a is the primary table.
For x in file_a:
X = x. strip ()
For y in file_ B _list:
Y = y. strip ()
If x = y:
Print x
Break
File_a.close ()
File_ B .close ()
# I suggest using the second one, because the first one requires a cursor reset action. Although several items do not have an impact, if the number is tens of millions, the impact should be huge, who will use nested loops for tens of millions? hash or binary is the right choice. Of course, the fastest one must be tree query.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.