[PYTHON] 100 amateur language processing knocks: 18

It is a challenge record of Language processing 100 knock 2015. The environment is Ubuntu 16.04 LTS + Python 3.5.2 : : Anaconda 4.1.1 (64-bit). Click here for a list of past knocks (http://qiita.com/segavvy/items/fb50ba8097d59475f760).

Chapter 2: UNIX Command Basics

hightemp.txt is a file that stores the record of the highest temperature in Japan in the tab-delimited format of "prefecture", "point", "℃", and "day". Create a program that performs the following processing and execute hightemp.txt as an input file. Furthermore, execute the same process with UNIX commands and check the execution result of the program.

18. Sort each row in descending order of the numbers in the third column

Arrange each row in the reverse order of the numbers in the third column (Note: sort the contents of each row unchanged). Use the sort command for confirmation (this problem does not have to match the result of executing the command).

The finished code:

main.py


# coding: utf-8

fname = 'hightemp.txt'
lines = open(fname).readlines()
lines.sort(key=lambda line: float(line.split('\t')[2]), reverse=True)

for line in lines:
	print(line, end='')

Execution result:

Terminal


Kochi Prefecture Ekawasaki 41 2013-08-12
40 Kumagaya, Saitama Prefecture.9	2007-08-16
40 Tajimi, Gifu Prefecture.9	2007-08-16
Yamagata 40 Yamagata.8	1933-07-25
Yamanashi Prefecture Kofu 40.7	2013-08-10
Wakayama Prefecture Katsuragi 40.6	1994-08-08
Shizuoka Prefecture Tenryu 40.6	1994-08-04
40 Katsunuma, Yamanashi Prefecture.5	2013-08-10
40 Koshigaya, Saitama Prefecture.4	2007-08-16
Gunma Prefecture Tatebayashi 40.3	2007-08-16
40 Kamisatomi, Gunma Prefecture.3	1998-07-04
Aisai 40, Aichi Prefecture.3	1994-08-05
Chiba Prefecture Ushiku 40.2	2004-07-20
40 Sakuma, Shizuoka Prefecture.2	2001-07-24
40 Uwajima, Ehime Prefecture.2	1927-07-22
40 Sakata, Yamagata Prefecture.1	1978-08-03
Gifu Prefecture Mino 40 2007-08-16
Gunma Prefecture Maebashi 40 2001-07-24
39 Mobara, Chiba.9	2013-08-11
39 Hatoyama, Saitama Prefecture.9	1997-07-05
Toyonaka 39, Osaka.9	1994-08-08
Yamanashi Prefecture Otsuki 39.9	1990-07-19
39 Tsuruoka, Yamagata Prefecture.9	1978-08-03
Aichi Prefecture Nagoya 39.9	1942-08-02

Shell script for UNIX command confirmation:

test.sh


#!/bin/sh

#Sort in reverse order with the third column as a number
sort hightemp.txt --key=3,3 --numeric-sort --reverse > result_test.txt

#Run with a Python program
python main.py > result.txt

#Check the result
diff --report-identical-files result.txt result_test.txt

Confirmation of results:

Terminal


segavvy@ubuntu:~/document/100 language processing knock 2015/18$ ./test.sh
10d9
<Gunma Prefecture Tatebayashi 40.3	2007-08-16
11a11
>Gunma Prefecture Tatebayashi 40.3	2007-08-16
17d16
<Gifu Prefecture Mino 40 2007-08-16
19,20c18
<39 Mobara, Chiba.9	2013-08-11
<39 Hatoyama, Saitama Prefecture.9	1997-07-05
---
>Gifu Prefecture Mino 40 2007-08-16
21a20
>39 Mobara, Chiba.9	2013-08-11
23a23
>39 Hatoyama, Saitama Prefecture.9	1997-07-05

The difference was detected by diff, but since the temperature in the third column is the same, it can't be helped that there is a difference in that part.

Lambda expression

I tried using a lambda expression for the first time this time. This is the part of lambda line: float (line.split ('\ t') [2]). For the given string line, it is used instead of the function that returns the third column divided by \ t as float. However, it's a little confusing. As with the inclusion notation, I think you are used to this area.

By the way, in the explanation of Small functions and lambda expressions in the Python document, the author He said he liked the style without lambda.

Sort type

When sorting, you need to pay attention to the type of the target data. In the target data this time, if you forget to convert to float with a lambda expression or forget to add --numeric-sort with the UNIX command sort, the result will be correct, but if the temperature is 5 degrees If the places are mixed, it will come to the top even though the temperature is the lowest.   That's all for the 19th knock. If you have any mistakes, I would appreciate it if you could point them out.

Recommended Posts

100 amateur language processing knocks: 41
100 amateur language processing knocks: 71
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 50
100 amateur language processing knocks: 59
100 amateur language processing knocks: 70
100 amateur language processing knocks: 62
100 amateur language processing knocks: 60
100 amateur language processing knocks: 92
100 amateur language processing knocks: 30
100 amateur language processing knocks: 84
100 amateur language processing knocks: 33
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
100 amateur language processing knocks: 40
100 amateur language processing knocks: 45
100 amateur language processing knocks: 43
100 amateur language processing knocks: 55
100 amateur language processing knocks: 22
100 amateur language processing knocks: 61
100 amateur language processing knocks: 04
100 amateur language processing knocks: 63
100 amateur language processing knocks: 78
100 amateur language processing knocks: 12
100 amateur language processing knocks: 14
100 amateur language processing knocks: 08
100 amateur language processing knocks: 42
100 amateur language processing knocks: 19
100 amateur language processing knocks: 73
100 amateur language processing knocks: 75
100 amateur language processing knocks: 98
100 amateur language processing knocks: 83
100 amateur language processing knocks: 95
100 amateur language processing knocks: 32
100 amateur language processing knocks: 96
100 amateur language processing knocks: 87
100 amateur language processing knocks: 72
100 amateur language processing knocks: 79
100 amateur language processing knocks: 23
100 amateur language processing knocks: 05
100 amateur language processing knocks: 00
100 amateur language processing knocks: 02
100 amateur language processing knocks: 37
100 amateur language processing knocks: 21
100 amateur language processing knocks: 11
100 amateur language processing knocks: 90
100 amateur language processing knocks: 74
100 amateur language processing knocks: 66
100 amateur language processing knocks: 28
100 amateur language processing knocks: 34
100 amateur language processing knocks: 36
100 amateur language processing knocks: 77
100 amateur language processing knocks: 27
100 amateur language processing knocks: 10
100 amateur language processing knocks: 03
100 amateur language processing knocks: 82
100 amateur language processing knocks: 53
100 amateur language processing knocks: 18
100 amateur language processing knocks: 35