Python:最不常见的字符串出现多少次?

-1 python csv function

这是一个链接到代码的csv文件的示例

csv图像

我想知道如何找到最不常见的字符串出现在字段中的次数[gas]

def least_string(gas):
    if  gas in gasdic:
        gasdic[gas] += 1
    else:
        gasdic[gas] = 1
Run Code Online (Sandbox Code Playgroud)

我将上半部分更改为功能

if  gas in gasCount:
     gasCount[gas] += 1
else:
    gasCount[gas] = 1
Run Code Online (Sandbox Code Playgroud)

它打印出来了

{'Nitrogen': 3, 'Methane': 3, 'Helium': 2, 'CarbonDioxide': 1, ' Chlorine': 3, 'Oxygen': 3, 'Xenon': 1, 'Hydrogen': 2, 'Argon': 1}
Run Code Online (Sandbox Code Playgroud)

我需要将其更改为功能

smallest = 100000
for key in gasCount:
    if gasCount[key] < smallest:
        smallest = gasCount[key]

answers = []
for key in gasCount:
    if gasCount[key] == smallest:
        answers.append(key)
Run Code Online (Sandbox Code Playgroud)

所以它会打印出来

The least common string appear in the field [gas]: ['CarbonDioxide', 'Xenon', 'Argon']
Run Code Online (Sandbox Code Playgroud)

这是完整的代码

import string

def getFile():
    filename = input('Filename: ') #the file name should be .csv
    file = open(filename, 'r')
    firstline = True

    Line = file.readline()
    if Line == None or Line == '':
        return None

    if firstline: # I do not want to read the field names
        Line = file.readline() # there is more to read
        firstline = False # so I skip them. the code assuems 

    return file

#Count the number of (T's) in the field [correct]
def calcT(correct):
    global tCount
    found = False
    for ch in correct:#look at each character in turn
        if ch in 'tT':
            found = True

    if found:
        tCount +=1
#How many times does the least common string appear in the field [gas]
def least_string(gas):
    if  gas in gasdic:
        gasdic[gas] += 1
    else:
        gasdic[gas] = 1


#Find the sum of the values in the field [quant] less than (408)
def sum_quant(quant):
    global qsum
    if quant < 408:
        qsum += quant

#How many values in the 'code' field do not match the format 9999(x9+)9?
def checkString(astring):
    if len(astring)  != 10:
        return False
    if not astring[0] in string.digits:
        return False
    if not astring[1] in string.digits:
        return False
    if not astring[2] in string.digits:
        return False
    if not astring[3] in string.digits:
        return False
    if not astring[4]=='(':
        return False
    if not astring[5] in string.ascii_lowercase:
        return False
    if not astring[6] in string.digits:
        return False
    if not astring[7]=='+':
        return False
    if not astring[8]==')':
        return False
    if not astring[9] in string.digits:
        return False
    return True



#What is the average value of the numbers in the field [age] in the range (30) and (107) inclusive 
def average_age(age):
    global tAge, ageCount
    if age >= 30 and age <=107:
        tAge += age
    ageCount += 1

#Find the sum of the numbers in field [length] between (2.482) and (6.428) inclusive 
def sum_Length(leng):
    global lensum
    if leng >= 2.482 and leng <= 6.428:
        lensum += leng

#count the lines where gas's have the value (Nitrogen) *or* quant is less than 318
def calcGas(gas, quant):
    global clines
    if gas == 'Nitrogen' or quant < 318:
        clines += 1

def processLine(Line):
    Line = Line.strip()
    fields = Line.split(',')

    correct = fields[0]
    gas = fields[1]
    quant = int(fields[2])
    code = fields[3]
    if checkString(code):
        global cCount
        cCount += 1
    age = int(fields[4])
    leng = float(fields[5])
    calcT(correct)
    sum_Length(leng)
    calcGas(gas, quant)
    average_age(age)
    sum_quant(quant)
    least_string(gas)


def processFile(data):

    for line in data:
        processLine(line)

    data.close()

def displayResults():
    #Count the number of (T's) in the field [correct]
    print('The number of (T) in the field [correct]: %d'%(tCount))
    print('-' *10)
    print(gasdic)
    print('The least common string appear in the field [gas]:%s'%(answers))
    print('-' *10)
    #Find the sum of the values in the field [quant] less than (408)
    print('The sum of the values in the field [quant] less than (408): %d'%(qsum))
    print('-' *10)
    #How many values in the 'code' field do not match the format 9999(x9+)9?
    print('The values in the code field do not match the format 9999(x9+)9: %d'%(cCount))
    print('-' *10)
    #What is the average value of the numbers in the field [age] in the range (30) and (107) inclusive
    print('The average value of numbers in the field[age] in range(30)and(107):%0.2f'%((tAge/ageCount)))
    print('-' *10)
    #Find the sum of the numbers in field [length] between (2.482) and (6.428) inclusive 
    print('The sum of the numbers in field [length] between (2.482) and (6.428): %6.3f'%(lensum))
    print('-' *10)
    #count the lines where gas's have the value (Nitrogen) *or* quant is less than 318
    print('The lines where gas have the value (Nitrogen) *or* quant is less than 318: %d' %(clines))


tCount = 0
qsum = 0
gasdic = {}
answers =[]
cCount = 0
ageCount = 0
tAge = 0
lensum = 0
clines = 0
myfile = getFile()
processFile(myfile)
displayResults()
Run Code Online (Sandbox Code Playgroud)

fer*_*mas 5

from collections import Counter

def least_common(ls):
    c = Counter(ls)
    m = min(c.values())
    return [k for k, v in c.items() if v == m]

least_common('Foo Bar FooBar Bar'.split()) # ['FooBar', 'Foo']
Run Code Online (Sandbox Code Playgroud)