์ฌ๋ฌ ๊ธฐ์ฐจ๋ณ ์์ก๋์ ๋ฐ๋ฅธ ์์ ์ถ๋ ฅ์ ํ๋ ํ๋ก๊ทธ๋จ
ํํ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด์ ๊ธฐ์ฐจ์ด๋ฆ, ์์ก๋(ํค) ์ ์ ๋ ฅํด์ค๋ค.
trainTupleList ๋ฅผ ๋์ ๋๋ฆฌ๋ก ๋ณํํด์ค๋ค.
tName ๊ฐ์๋ tmpTup [0] ๋ฒ์งธ ๊ฐ = ์งํจ
tWeight ๊ฐ์๋ tmpTup [1] ๋ฒ์งธ ๊ฐ = 5
์กฐ๊ฑด๋ฌธ = trainDic์ tName ์ด ์๋์ง ํ์ธ
์์ผ๋ฉด + ๋์
์์ผ๋ฉด + ๋์
(.... ๋ฐ๋ณตํด์ค๋ค)
-------------------
๊ทธ๋ค์์ ๋์ ๋๋ฆฌ ์ ๋ ฌ ์ฝ๋๋ค
ํค๋ก ์ ๋ ฌํ ํ ๋์ ๋๋ฆฌ ์ถ์ถ
๊ฒฐ๊ณผ๋ ํํ๋ก ๋ณ๊ฒฝ -> ๋ฆฌ์คํธ๋ก ๋ฐํ๋จ
itemgetter(1)์ ์ ์์ดํ ์ธ ํํ์ 2๋ฒ์งธ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค.
TrainList ๋ ์ซ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ์ด ๋๋ค.
-------------------
๊ทธ ๋ค์์ ์ ๋ ฌํ ์์๋๋ก ๊ธฐ์ฐจ์ด๋ฆ/ ์ด์์ก๋์ ์ถ๋ ฅํ๋ ์ฝ๋
0๋ฒ์งธ๋ ๊ทธ๋๋ก ์ถ๋ ฅ
1๋ฒ์งธ๋ถํฐ ์์
for i in range (1, len(trainList)): // trianlist ๊ธธ์ด๊น์ง 1~(4-1)
totalRank += 1
if trainList[i][1] == trainList[i-1][1]:
pass
else :
currentRank = totalRank
๋์ฐ ๋ฏธ๋ ์งํจ ๋ชจ๋ชจ
0 1 2 3
๋ฏธ๋ , 13 == ๋์ฐ , 17
๋ฏธ๋ +2
์งํจ == ๋ฏธ๋
์งํจ +3
๋ชจ๋ชจ == ์งํจ
๋ชจ๋ชจ +4
import operator
trainTupleList = [('์งํจ',5),('๋์ฐ',8),('๋์ฐ',9),('๋ชจ๋ชจ',5),
('๋ชจ๋ชจ',3),('๋ฏธ๋',5),('์งํจ',5),('๋ฏธ๋',8)]
trainDic, trainList = {},[]
tmpTup = None
totalRank, currentRank = 1,1
if __name__ == "__main__" :
for tmpTup in trainTupleList :
tName = tmpTup[0]
tWeight = tmpTup[1]
if tName in trainDic :
trainDic[tName] += tWeight
else:
trainDic[tName] = tWeight
print('๊ธฐ์ฐจ์์ก๋ ๋ชฉ๋ก = > ', trainTupleList)
print('================== ')
trainList = sorted(trainDic.items(),key = operator.itemgetter(1), reverse=True)
print('๊ธฐ์ฐจ์์ก๋ ์์ = > ' )
print('================== ')
print(trainList[0][0], '\t' , trainList[0][1], '\t', currentRank)
for i in range (1, len(trainList)):
totalRank += 1
if trainList[i][1] == trainList[i-1][1]:
pass
else :
currentRank = totalRank
print(trainList[i][0], '\t', trainList[i][1], '\t', currentRank)