Recently started learning python. Can you tell me how to find the maximum element in the file? And how to create a new file that will consist of the pair numbers of another file? Can you help please.
Closed due to the fact that the essence of the issue is not clear to the participants of the Air , 0xdb , aleksandr barakin , user192664, user300000 9 Oct '18 at 8:29 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- 2What exactly is your problem? Did you try to write something yourself? There should be no problems in opening the file and finding the maximum element. - RiotBr3aker
- I tried to find on the Internet "How to find the maximum element in the file", but found nothing. I am new and wanted to find sample code. - Nastya Grant
- Give an example of input and output data in question - MaxU
1 answer
This answer is based on the assumption that the source data file is a text file, the numbers in which are written with a space and / or on different lines. It is also assumed that the numbers are written in the format "xx".
How to find the maximum element in the file?
In the first implementation, this task can be divided into 2:
- Read the data from the file and write it to the appropriate collection for this task.
- Work with the collection, not with the file. In this case, search for the maximum number in the collection using a standard function.
The advantage of this approach in the absence of "solidity": the code below at least allows you to read the file and write data to the list as a float of numbers, this can be used to find the minimum number, or, for example, sorting - an infinite number of options.
filepath = 'data.txt' numbers = [] with open(filepath, 'r') as inf: for line in inf: line = line.strip() if line: numbers.extend(map(float, line.split(' '))) print(max(numbers)) The problem with this approach is to download the entire file, convert it to a list, and only then extract some result, this leads to the obvious problem: a large file - the program will eat a lot of RAM.
To avoid this, you can find the maximum in each read line:
import math filepath = 'data.txt' maxNum = -math.inf with open(filepath, 'r') as inf: for line in inf: line = line.strip() if line: lineMax = max(map(float, line.split(' '))) if lineMax > maxNum: maxNum = lineMax print(maxNum) Both implementations work with any textual type of files - whether each digit is written on a separate line or several numbers are written on each line, including blank lines ignored.
How to create a new file consisting of even numbers in another file?
Open 2 files - a file with data and a file with input data. Next, read the file line by line and get a list of numbers, each number in which we check for parity.
inputFilepath = 'data.txt' outputFilepath = 'result.txt' with open(outputFilepath, 'w') as of, open(inputFilepath, 'r') as inf: for line in inf: line = line.strip() if line: of.writelines(['{0} '.format(number) for number in map(int, line.split(' ')) if number % 2 == 0]) - 2Why do you think the file consists of numbers separated by a space, which are recognized by the float? If such questions are answered, then it is worth mentioning the main assumptions clearly. At the entrance there can be a csv file and xls and db.sqlite ¶ For a beginner (not only the author, but generally possible readers of this question), it is worth mentioning explicitly that the task should be divided into two: 1. get a collection of numbers from a file 2. Find the maximum element in the collection (if you learn how to break a task into more universal subtasks, then it is easier to reuse / find solutions - jfs
- In some cases, you can
max(map(int, Path('numbers.txt').read_text().split()))In others:pandas.read_csv().column.max()- jfs - Thank you very much))) Everything works and everything is clear)))) - Nastya Grant
- @jfs, if it was a csv file - I don’t think the author would have indicated it. Yes, and I do not adhere to the point of view of "reading the entire file, and then processing," which I explicitly said. But in general, I agree with you, for a beginner it is better to chew it - corrected the answer. - RiotBr3aker
- if you read line by line (what’s the point? if you come up with a file format for the author, you can think of a file in memory), the code may look like this:
with open(filename) as file: result = max(int(w) for line in file for w in line.split())¶ If you are so worried about the memory consumed, then you can think of an option that supports large lines, using mmap for example. - jfs