--------看了这篇文章,觉得很有意思,后面博友评论的也很多。其实我也一直在纳闷为什么e,r,s,t,i键敲击的最多。后来就用想应该写个程序验证一下,拿起了小半年没有用过的python,费了一个小时,终于搞定。
View Code
1 import os
2
3 alist = [ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
4 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
5 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
6 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
7 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
8 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
9 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
10 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ]
11
12
13
14 #
15 #list all file in the directory and sub directory
16 #input:directory path
17 #outpur:none
18 #
19 def lstallfl(directory):
20 dirs = os.listdir(directory)
21
22 for item in dirs:
23 if not item.startswith( ' . '):
24
25 if os.path.isdir(directory +item +" /"):
26 # print directory +item +" /"
27 lstallfl(directory +item +" /")
28
29 elif os.path.isfile(directory +item):
30 calculate(directory +item)
31 # end of function
32
33 #
34 #calculate every file
35 #input:none
36 #output:none
37 #
38 def calculate(filepath):
39 #test the filepath
40 if not os.path.isfile(filepath):
41 return
42 if not filepath.endswith( ' c ') or filepath.endswith( ' h '):
43 return
44 else:
45 # print filepath
46 f = open(filepath, ' r ')
47 alllines = f.readlines()
48 f. close()
49
50 for eachline in alllines:
51 statistics(eachline)
52 # end of function
53
54
55 #
56 # statistics the number
57 #input:none
58 #output:none
59 #
60 def statistics(aString):
61 for char in aString:
62 alist [ 255 ] += 1
63 alist [ ord(char) ] += 1
64 # end of function
65
66 #
67 #process the result
68 #input:none
69 #output:none
70 #
71 def finalprocess():
72 for i in range( 56, 91):
73 alist [ i+32 ] += alist [ i ]
74 alist [ i ] = 0
75
76 for i in range( 32, 65):
77 print i,chr(i),alist [ i ]
78
79 for i in range( 91, 127):
80 print i,chr(i),alist [ i ]
81 # end of function
82
83 #main function
84 if __name__ == ' __main__ ':
85 lstallfl(" /home /archy /SourceCode /")
86 finalprocess();
87
88
89
2
3 alist = [ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
4 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
5 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
6 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
7 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
8 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
9 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
10 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ]
11
12
13
14 #
15 #list all file in the directory and sub directory
16 #input:directory path
17 #outpur:none
18 #
19 def lstallfl(directory):
20 dirs = os.listdir(directory)
21
22 for item in dirs:
23 if not item.startswith( ' . '):
24
25 if os.path.isdir(directory +item +" /"):
26 # print directory +item +" /"
27 lstallfl(directory +item +" /")
28
29 elif os.path.isfile(directory +item):
30 calculate(directory +item)
31 # end of function
32
33 #
34 #calculate every file
35 #input:none
36 #output:none
37 #
38 def calculate(filepath):
39 #test the filepath
40 if not os.path.isfile(filepath):
41 return
42 if not filepath.endswith( ' c ') or filepath.endswith( ' h '):
43 return
44 else:
45 # print filepath
46 f = open(filepath, ' r ')
47 alllines = f.readlines()
48 f. close()
49
50 for eachline in alllines:
51 statistics(eachline)
52 # end of function
53
54
55 #
56 # statistics the number
57 #input:none
58 #output:none
59 #
60 def statistics(aString):
61 for char in aString:
62 alist [ 255 ] += 1
63 alist [ ord(char) ] += 1
64 # end of function
65
66 #
67 #process the result
68 #input:none
69 #output:none
70 #
71 def finalprocess():
72 for i in range( 56, 91):
73 alist [ i+32 ] += alist [ i ]
74 alist [ i ] = 0
75
76 for i in range( 32, 65):
77 print i,chr(i),alist [ i ]
78
79 for i in range( 91, 127):
80 print i,chr(i),alist [ i ]
81 # end of function
82
83 #main function
84 if __name__ == ' __main__ ':
85 lstallfl(" /home /archy /SourceCode /")
86 finalprocess();
87
88
89
上面是写的代码,我们看一下运行结果:
View Code
1
32
457011
2 33 ! 1638
3 34 " 8792
4 35 # 3257
5 36 $ 365
6 37 % 959
7 38 & 8485
8 39 ' 6422
9 40 ( 25822
10 41 ) 25959
11 42 * 104040
12 43 + 7871
13 44 , 13738
14 45 - 17813
15 46 . 8032
16 47 / 13931
17 48 0 18880
18 49 1 9675
19 50 2 5041
20 51 3 1996
21 52 4 1443
22 53 5 1689
23 54 6 1625
24 55 7 1689
25 56 8 0
26 57 9 0
27 58 : 0
28 59 ; 0
29 60 < 0
30 61 = 0
31 62 > 0
32 63 ? 0
33 64 @ 0
34 91 [ 41808
35 92 \ 5029
36 93 ] 29069
37 94 ^ 8194
38 95 _ 29110
39 96 ` 29
40 97 a 52294
41 98 b 18025
42 99 c 39949
43 100 d 29065
44 101 e 91546
45 102 f 30223
46 103 g 19702
47 104 h 18122
48 105 i 65054
49 106 j 1818
50 107 k 9225
51 108 l 36025
52 109 m 21704
53 110 n 54152
54 111 o 56740
55 112 p 43104
56 113 q 2325
57 114 r 62993
58 115 s 61789
59 116 t 87561
60 117 u 30620
61 118 v 9783
62 119 w 7661
63 120 x 6540
64 121 y 9855
65 122 z 6270
66 123 { 6667
67 124 | 2099
68 125 } 6674
69 126 ~ 259
2 33 ! 1638
3 34 " 8792
4 35 # 3257
5 36 $ 365
6 37 % 959
7 38 & 8485
8 39 ' 6422
9 40 ( 25822
10 41 ) 25959
11 42 * 104040
12 43 + 7871
13 44 , 13738
14 45 - 17813
15 46 . 8032
16 47 / 13931
17 48 0 18880
18 49 1 9675
19 50 2 5041
20 51 3 1996
21 52 4 1443
22 53 5 1689
23 54 6 1625
24 55 7 1689
25 56 8 0
26 57 9 0
27 58 : 0
28 59 ; 0
29 60 < 0
30 61 = 0
31 62 > 0
32 63 ? 0
33 64 @ 0
34 91 [ 41808
35 92 \ 5029
36 93 ] 29069
37 94 ^ 8194
38 95 _ 29110
39 96 ` 29
40 97 a 52294
41 98 b 18025
42 99 c 39949
43 100 d 29065
44 101 e 91546
45 102 f 30223
46 103 g 19702
47 104 h 18122
48 105 i 65054
49 106 j 1818
50 107 k 9225
51 108 l 36025
52 109 m 21704
53 110 n 54152
54 111 o 56740
55 112 p 43104
56 113 q 2325
57 114 r 62993
58 115 s 61789
59 116 t 87561
60 117 u 30620
61 118 v 9783
62 119 w 7661
63 120 x 6540
64 121 y 9855
65 122 z 6270
66 123 { 6667
67 124 | 2099
68 125 } 6674
69 126 ~ 259
这里统计了ucos操作系统代码和unix操作系统代码,代码量一般,但是我觉得这两个系统的代码写的比较标准比较有代表性。从运行的结果看出,e,r,s,t,i,*,空格键用的比较多,其实我也不知道那位作者是如何统计的,我也只是统计了两个操作系统的代码,可能有偏差,但是我相信偏差不会太大。
有兴趣的博友可以测试一下c#、汇编等语言的键敲击率,也欢迎提出宝贵意见。