终于开始这个坑了。把这个系列2017秋冬学期的作业心得分享出来。
首先贴一下题目:
我们把words.txt的内容粘贴在下面:
Singaporeans were meant to go to the polls at the end of next week to vote for a new president but theyll no longer have the chance with only one candidate qualifying for the races Former Speaker of Parliament Halimah Yacob has emerged victorious by defaults after other presidential hopefuls fell foul of new ruless I can only say that I promise to do the best that I can to serve the people of Singapore and that doesnt change whether there is an election or no electionss she told reporters Mondays What should be a moment of celebration ss Halimah will be Singapores first female president ss has proved contentious for several reasons and appears at odds with Singapores reputation as a technocratic and efficient city states While the office of president is largely a ceremonial role in Singapores he or she has power to veto some of the governments decisionss for example in fiscal matters that touch on the countrys reservess or key appointments in the public services The only beneficiaries from this reserved presidential election are Halimah Yacob and her teams as well as Singapores oppositions which now has a new line of attack against the PAP The rest of Singapore has sufferedss Sudhir Vadakeths a Singapore author and commentators told CNNs Halimah wass until recentlys a loyal member of the ruling PAPs which dominates Singaporean politics All Singaporeans are unhappy that meritocracy and electoral fairnesss core Singaporean valuess have been eroded to fulfill perceived political goals In this elections for the first times candidates to become Singapores president could only come from one racial groups Malay Its a radical policy that would likely prove divisive elsewhere but its one the Southeast Asian nation said was necessary to ensure better representation among the countrys three main racess Chineses Indian and Malay It shows we dont only talk about multisracialisms but we talk about it in the context of meritocracy or opportunities for everyones and we actually practice itss Halimah told The Straits Times newspapers before declaring her intention to contest the elections The new rules also set stricter criteria on the background of candidatess For examples those from the private sector are required to be a chief executive of a companys with at least million in shareholders equity The two other Malay presidential hopefuls ss businessmen Salleh Marican and Farid Khan ss failed to gain Certificates of Eligibility from the Presidential Elections Committee on these groundss although the Presidential Elections Committee could have exercised its discretion to allow them to run for the office Critics charge that the new rules are a way for the government to stages manage the election and prevent opponents from runnings In Augusts Singapores appeal court ruled against a legal challenge to the new system by ruling party lawmaker turned critics Tan Cheng Bocks Tan had narrowly lost the previous presidential election in to Tony Tans a former deputy prime minister widely recognized as the governments favored candidates and planned to run agains Singapores population is Chineses Malays Indian and are the ambiguously named Other
题目要求用python3.5解决这个问题,并不包含任何其他库。
思考一下,这个题目可以使用python读完这个文件后,用‘ ’(空格)去分割整个字符串,变成以每个单词为元素的列表。然后遍历这个列表,同时建立一个字典,字典的键为各个单词,字典的值为[编号,出现次数]这样一个列表。如果这个单词不在字典中,就在字典中添加一项,编号+1,出现次数为1;如果字典中已经有了以这个词为键值的项,就把出现次数+1即可。
之后输出的时候,要按照编号排序,使用sorted() + lambda语句即可快速解决问题。
总的来说这个题目作为上手题目还是比较简单的,在python中主要用到了读写文件、字典按值排序这两个点。贴上代码如下:
#coding = utf-8
#author :
if __name__ == "__main__":
file = open("C:\\Learn\\machine_learning\\HW0\\words.txt", 'r')
words = file.readlines()[0].strip().split(' ')
print(u"读取完毕")
dict = {}
i = 0
for word in words:
if not word in dict:
dict.update({word : [i, 1]})
i = i + 1
else:
dict[word] = [dict[word][0], dict[word][1] + 1]
print(u"装入字典完毕")
file.close()
dic = sorted(dict.items(), key = lambda d:d[1][0])
file = open("C:\\Learn\\machine_learning\\HW0\\Q1.txt", 'w')
text = []
print(dic)
for key in dic:
text.append(key[0] + " " + str(key[1][0]) + " " + str(key[1][1]) + "\n")
else:
text[-1] = text[-1].replace('\n','')
print(u"数据准备完毕")
file.writelines(text)
file.close()
print(u"数据写入完毕")
这样执行就可以啦。
输出结果:
Singaporeans 0 2
were 1 1
meant 2 1
to 3 18
go 4 1
the 5 29
polls 6 1
at 7 3
end 8 1
of 9 14
next 10 1
week 11 1
vote 12 1
for 13 8
a 14 13
new 15 6
president 16 4
but 17 3
theyll 18 1
no 19 2
longer 20 1
have 21 3
chance 22 1
with 23 3
only 24 5
one 25 3
candidate 26 1
qualifying 27 1
races 28 1
Former 29 1
Speaker 30 1
Parliament 31 1
Halimah 32 5
Yacob 33 2
has 34 5
emerged 35 1
victorious 36 1
by 37 2
defaults 38 1
after 39 1
other 40 2
presidential 41 4
hopefuls 42 2
fell 43 1
foul 44 1
ruless 45 1
I 46 3
can 47 2
say 48 1
that 49 7
promise 50 1
do 51 1
best 52 1
serve 53 1
people 54 1
Singapore 55 3
and 56 12
doesnt 57 1
change 58 1
whether 59 1
there 60 1
is 61 3
an 62 1
election 63 4
or 64 4
electionss 65 1
she 66 2
told 67 3
reporters 68 1
Mondays 69 1
What 70 1
should 71 1
be 72 3
moment 73 1
celebration 74 1
ss 75 4
will 76 1
Singapores 77 7
first 78 2
female 79 1
proved 80 1
contentious 81 1
several 82 1
reasons 83 1
appears 84 1
odds 85 1
reputation 86 1
as 87 4
technocratic 88 1
efficient 89 1
city 90 1
states 91 1
While 92 1
office 93 2
largely 94 1
ceremonial 95 1
role 96 1
in 97 6
he 98 1
power 99 1
veto 100 1
some 101 1
governments 102 2
decisionss 103 1
example 104 1
fiscal 105 1
matters 106 1
touch 107 1
on 108 3
countrys 109 2
reservess 110 1
key 111 1
appointments 112 1
public 113 1
services 114 1
The 115 5
beneficiaries 116 1
from 117 5
this 118 2
reserved 119 1
are 120 5
her 121 2
teams 122 1
well 123 1
oppositions 124 1
which 125 2
now 126 1
line 127 1
attack 128 1
against 129 2
PAP 130 1
rest 131 1
sufferedss 132 1
Sudhir 133 1
Vadakeths 134 1
author 135 1
commentators 136 1
CNNs 137 1
wass 138 1
until 139 1
recentlys 140 1
loyal 141 1
member 142 1
ruling 143 2
PAPs 144 1
dominates 145 1
Singaporean 146 2
politics 147 1
All 148 1
unhappy 149 1
meritocracy 150 2
electoral 151 1
fairnesss 152 1
core 153 1
valuess 154 1
been 155 1
eroded 156 1
fulfill 157 1
perceived 158 1
political 159 1
goals 160 1
In 161 2
elections 162 2
times 163 1
candidates 164 2
become 165 1
could 166 2
come 167 1
racial 168 1
groups 169 1
Malay 170 3
Its 171 1
radical 172 1
policy 173 1
would 174 1
likely 175 1
prove 176 1
divisive 177 1
elsewhere 178 1
its 179 2
Southeast 180 1
Asian 181 1
nation 182 1
said 183 1
was 184 1
necessary 185 1
ensure 186 1
better 187 1
representation 188 1
among 189 1
three 190 1
main 191 1
racess 192 1
Chineses 193 2
Indian 194 2
It 195 1
shows 196 1
we 197 3
dont 198 1
talk 199 2
about 200 2
multisracialisms 201 1
it 202 1
context 203 1
opportunities 204 1
everyones 205 1
actually 206 1
practice 207 1
itss 208 1
Straits 209 1
Times 210 1
newspapers 211 1
before 212 1
declaring 213 1
intention 214 1
contest 215 1
rules 216 2
also 217 1
set 218 1
stricter 219 1
criteria 220 1
background 221 1
candidatess 222 1
For 223 1
examples 224 1
those 225 1
private 226 1
sector 227 1
required 228 1
chief 229 1
executive 230 1
companys 231 1
least 232 1
million 233 1
shareholders 234 1
equity 235 1
two 236 1
businessmen 237 1
Salleh 238 1
Marican 239 1
Farid 240 1
Khan 241 1
failed 242 1
gain 243 1
Certificates 244 1
Eligibility 245 1
Presidential 246 2
Elections 247 2
Committee 248 2
these 249 1
groundss 250 1
although 251 1
exercised 252 1
discretion 253 1
allow 254 1
them 255 1
run 256 2
Critics 257 1
charge 258 1
way 259 1
government 260 1
stages 261 1
manage 262 1
prevent 263 1
opponents 264 1
runnings 265 1
Augusts 266 1
appeal 267 1
court 268 1
ruled 269 1
legal 270 1
challenge 271 1
system 272 1
party 273 1
lawmaker 274 1
turned 275 1
critics 276 1
Tan 277 2
Cheng 278 1
Bocks 279 1
had 280 1
narrowly 281 1
lost 282 1
previous 283 1
Tony 284 1
Tans 285 1
former 286 1
deputy 287 1
prime 288 1
minister 289 1
widely 290 1
recognized 291 1
favored 292 1
planned 293 1
agains 294 1
population 295 1
Malays 296 1
ambiguously 297 1
named 298 1
Other 299 1
Q2是一个读取图片,处理图片并保存的问题。
按照题目要求是使用PIL库,但是PIL库并没有Python3.5的版本,我又装了Python2.7,但是在2.7上PIL库仍然不能使用,这个坑先保留在这里,日后解决了再来填。