简体   繁体   中英

Is there any way to make this code better?

i have TXT file with football results in this format:

1-0
2-0
2-3
etc...

I want to count each result to know how much of them are in this file. I have code to count it:

def find_result(self):
    file = open("results.txt", "r")
    result_00 = 0
    result_01 = 0
    result_02 = 0
    result_03 = 0
    result_10 = 0
    result_11 = 0
    result_12 = 0
    result_13 = 0
    result_20 = 0
    result_21 = 0
    result_22 = 0
    result_23 = 0
    result_30 = 0
    result_31 = 0
    result_32 = 0
    result_33 = 0
    result_other = 0
    results = 0
    for line in file:
        results += 1
        line = line.rstrip()
        if line == '0-0':
            result_00 += 1
        elif line == '0-1':
            result_01 += 1
        elif line == '0-2':
            result_02 += 1
        elif line == '0-3':
            result_03 += 1
        elif line == '1-0':
            result_10 += 1
        elif line == '1-1':
            result_11 += 1
        elif line == '1-2':
            result_12 += 1
        elif line == '1-3':
            result_13 += 1
        elif line == '2-0':
            result_20 += 1
        elif line == '2-1':
            result_21 += 1
        elif line == '2-2':
            result_22 += 1
        elif line == '2-3':
            result_23 += 1
        elif line == '3-0':
            result_30 += 1
        elif line == '3-1':
            result_31 += 1
        elif line == '3-2':
            result_32 += 1
        elif line == '3-3':
            result_33 += 1
        else:
            result_other += 1
    print('[0-0]' + str(result_00))
    print('[0-1]' + str(result_01))
    print('[0-2]' + str(result_02))
    print('[0-3]' + str(result_03))
    print('[1-0]' + str(result_10))
    print('[1-1]' + str(result_11))
    print('[1-2]' + str(result_12))
    print('[1-3]' + str(result_13))
    print('[2-0]' + str(result_20))
    print('[2-1]' + str(result_21))
    print('[2-2]' + str(result_22))
    print('[2-3]' + str(result_23))
    print('[3-0]' + str(result_30))
    print('[3-1]' + str(result_31))
    print('[3-2]' + str(result_32))
    print('[3-3]' + str(result_33))
    print('[OTHER]' + str(result_other))
    print('Matches: ' + str(results))

I think it is not good way to do this (i mean too much code), is there any better solution to do this? Thank You

# import Counter for Counting the number of occurances of each items
from itertools import Counter

# Open the file for reading
with open("results.txt", "r") as input_file:

    # Create the counter object with the lines read from the file
    c = Counter(line.rstrip() for line in input_file)

    # Print the actual item (key) and the number of occurances (count)
    for key, count in c.items():
        print("[{}] - {}".format(key, count))

    # Find the total number of elements encountered, by adding all the counts
    print(sum(c.values())

As a more pythonic way you can use collections.Counter that is for this aim :

from collections import Counter
c_dic=Counter(open("results.txt", "r").readlines())

and you have a lot of choice for print your dictionary or items , as you have not a nested dictionary there is no need to loop over items and print them you can use json.dumps for print the dictionary with arbitrary indent :

from collections import Counter
import json
print json.dumps(Counter(open("newefile.txt", "r").readlines()),indent=4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM