0

インテリジェントに整列させたいデータの列があります ( gspreadのおかげで Google ドキュメントから簡単にインポートできます)。エントリを辞書に取り込みます。入力には、電子メール、Twitter ハンドル、またはブログ URL を含めることができます。例えば:

mike.j@gmail.com
@mikej45
j.mike@world.eu
_http://tumblr.com/mikej45

現在、「ダム」バージョンは次のとおりです。

def NomineeCount(spreadsheet):
    worksheet = spreadsheet.sheet1
    nominees = worksheet.col_values(6) # F = 6
    unique_nominees = {}
    for c in nominees:
        pattern = re.compile(r'\s+')
        c = re.sub(pattern, '', c)
        if unique_nominees.has_key(c) == True: # If we already have the name
            unique_nominees[c] += 1
        else:
            unique_nominees[c] = 1

        # Print out the alphabetical list of nominees with leading vote count
        for w in sorted(unique_nominees.keys()):
        print string.rjust(str(unique_nominees[w]), 2)+ "  " + w

        return nominees

if プロセス中にいくつかのスマートを追加する効率的な (-ish) 方法は何ですか?

4

1 に答える 1

1

あなたはdefaultdictで試すことができます:

from collections import defaultdict
unique_nominees = defaultdict(lambda: 0)
unique_nominees[c] += 1
于 2012-11-26T21:25:42.390 に答える