2017-03-18 7 views
1

Ich versuche ein Programm zu erstellen, das, wenn ein Benutzer einen Namen eingibt, die Anzahl der Personen mit diesem Namen zurückgibt. Wenn sie "am meisten" eingeben, wird der am häufigsten verwendete Name zurückgegeben. Ich habe das Zählen einzelner Wörter irgendwie gehört, aber ich bin mir nicht sicher, wie man das Programm dazu bringt, das am häufigsten auftretende Wort zu definieren und zu zählen.Zähle das häufigste Wort in einer Liste

import re 
from collections import Counter 

data = ('Billy Bob', 'Misty', 'Leroy', 'Leroy', 'Leroy', 'Billy Bob', 'Betty Sue', 
     'Billy Bob', 'Betty Sue', 'Misty', 'Betty Sue', 'Betty Sue', 
     'Misty', 'Betty Sue', 'Horace', 'Misty', 'Betty Sue', 'Misty', 
     'Leroy', 'Betty Sue', 'Misty', 'Doug', 'Misty', 'Wilma', 'Jesse', 
     'Misty', 'Billy Bob', 'Betty Sue', 'Betty Sue', 'Leroy', 'Misty', 
     'Leroy', 'Jesse Jr', 'Betty Sue', 'Betty Sue', 'Misty', 'Misty', 
     'Misty', 'Betty Sue', 'Misty', 'Misty', 'Misty', 'Leroy', 'Leroy', 
     'Bailey', 'Peggy', 'Leroy', 'Billy Bob', 'Leroy', 'Leroy', 'Misty', 
     'Paris', 'Leroy', 'Leroy', 'Misty Mae', 'Leroy', 'Misty', 'Leroy', 
     'Bart', 'Big Daddy', 'Betty Sue', 'Billy Bob', 'Betty Sue', 
     'LeeAnne', 'Billy Bob', 'Leroy', 'Betty Sue', 'Leroy', 'Betty Sue', 
     'Misty', 'Rowdy', 'Billy Bob', 'Ricky', 'Misty', 'Billy Bob', 'Billy 
     Bob', 'Billy Bob', 'EvaSue', 'Mark', 'Betty Sue', 'Leroy', 'Betty 
     Sue', 'Billy Bob', 'Leroy', 'Leroy', 'Billy Bob', 'Billy Bob', 
     'Billy Bob', 'Billy Bob', 'Billy Bob', 'Misty', 'Rob', 'Betty Sue', 
     'SuelySue', 'Billy Bob', 'Misty', 'Betty Sue', 'Misty', 'Billy Bob', 
     'Betty Sue', 'Leroy', 'Misty', 'Billy Bob', 'Misty', 'Misty', 'Billy 
     Bob', 'Billy Bob', 'Billy Bob', 'Billy Bob', 'Leroy', 'Jesse Jr Jr', 
     'Billy Bob', 'Grady', 'Leroy', 'Billy Bob', 'Leroy', 'Billy Bob', 
     'Betty Sue', 'Billy Bob', 'Misty', 'Louise', 'Leroy', 'Betty Sue', 
     'Leroy', 'Betty Sue', 'Leroy', 'Betty Sue', 'Betty Sue', 'Billy 
     Bob', 'Leroy', 'Jenny Jae', 'Misty', 'Betty Sue', 'Billy Bob', 
     'Leroy', 'Billy Bob', 'Jesse', 'Misty', 'Misty', 'Leroy', 'Betty 
     Sue', 'BJ', 'Misty', 'Leroy', 'Boris', 'Misty', 'Billy Bob', 'Pegs', 
     'Misty', 'Leslie', 'James', 'Melvin', 'Misty', 'Betty Sue', 'Mary 
     Beth', 'Billy Bob', 'Betty Sue', 'Billy Bob', 'Misty', 'Betty Sue', 
     'Leroy', 'Billy Bob', 'Billy Bob', 'BethAnne', 'Leroy', 'Betty Sue', 
     'Bett', 'Billy Bob', 'Misty', 'Misty', 'Billy Bob', 'Leroy', 'Billy 
     Bob', 'Billy Bob', 'Misty', 'Billy Bob', 'Raina', 'Betty Sue', 
     'Misty', 'Misty', 'Misty', 'Betty Sue', 'Mikey', 'Betty Sue', 'Billy 
     Bob', 'Misty', 'Betty Sue', 'Leroy', 'Betty Sue', 'Billy Bob', 
     'Betty Sue', 'Billy Bob', 'Betty Sue', 'Louise Jr', 'Billy Bob', 
     'Misty', 'Leroy', 'Leroy', 'Billy Bob', 'Billy Bob', 'Misty', 
     'Leroy', 'Leroy', 'Leroy', 'Billy Bob', 'Betty Sue', 'Billy Bob', 
     'Betty Sue', 'Misty', 'Betty Sue', 'Betty Sue', 'Misty', 'Betty 
     Sue', 'Horace', 'Misty', 'Betty Sue', 'Misty', 'Leroy', 'Betty Sue', 
     'Misty', 'Doug', 'Misty', 'Wilma', 'Jesse', 'Misty', 'Billy Bob', 
     'Betty Sue', 'Betty Sue', 'Leroy', 'Misty', 'Leroy', 'Jesse Jr', 
     'Betty Sue', 'Betty Sue', 'Misty', 'Misty', 'Misty', 'Betty Sue', 
     'Misty', 'Misty', 'Misty', 'Leroy', 'Leroy', 'Bailey', 'Peggy', 
     'Leroy', 'Billy Bob', 'Leroy', 'Leroy', 'Misty', 'Paris', 'Leroy', 
     'Leroy', 'Misty Mae', 'Leroy', 'Misty', 'Leroy', 'Bart', 'Big 
     Daddy', 'Betty Sue', 'Billy Bob', 'Betty Sue', 'LeeAnne', 'Billy 
     Bob', 'Leroy', 'Betty Sue', 'Leroy', 'Betty Sue', 'Misty', 'Rowdy', 
     'Billy Bob', 'Ricky', 'Misty', 'Billy Bob', 'Billy Bob', 'Billy 
     Bob', 'EvaSue', 'Mark', 'Betty Sue', 'Leroy', 'Betty Sue', 'Billy 
     Bob', 'Leroy', 'Leroy', 'Billy Bob', 'Billy Bob', 'Billy Bob', 
     'Billy Bob', 'Billy Bob', 'Misty', 'Rob', 'Betty Sue', 'SuelySue', 
     'Billy Bob', 'Misty', 'Betty Sue', 'Misty', 'Billy Bob', 'Betty 
     Sue', 'Leroy', 'Misty', 'Billy Bob', 'Misty', 'Misty', 'Billy Bob', 
     'Billy Bob', 'Billy Bob', 'Billy Bob', 'Leroy', 'Jesse Jr Jr', 
     'Billy Bob', 'Grady', 'Leroy', 'Billy Bob', 'Leroy', 'Billy Bob', 
     'Betty Sue', 'Billy Bob', 'Misty', 'Louise', 'Leroy', 'Betty Sue', 
     'Leroy', 'Betty Sue', 'Leroy', 'Betty Sue', 'Betty Sue', 'Billy 
     Bob', 'Leroy', 'Jenny Jae', 'Misty', 'Betty Sue', 'Billy Bob', 
     'Leroy', 'Billy Bob', 'Jesse', 'Misty', 'Misty', 'Leroy', 'Betty 
     Sue', 'BJ', 'Misty', 'Leroy', 'Boris', 'Misty', 'Billy Bob', 'Pegs', 
     'Misty', 'Leslie', 'James', 'Melvin', 'Misty', 'Betty Sue', 'Mary 
     Beth', 'Billy Bob', 'Betty Sue', 'Billy Bob', 'Misty', 'Betty Sue', 
     'Leroy', 'Billy Bob', 'Billy Bob', 'BethAnne', 'Leroy', 'Betty Sue', 
     'Bett', 'Billy Bob', 'Misty', 'Misty', 'Billy Bob', 'Leroy', 'Billy 
     Bob', 'Billy Bob', 'Misty', 'Billy Bob', 'Raina', 'Betty Sue', 
     'Misty', 'Misty', 'Misty', 'Betty Sue', 'Mikey', 'Betty Sue', 'Billy 
     Bob', 'Misty', 'Betty Sue', 'Leroy', 'Betty Sue', 'Billy Bob', 
     'Betty Sue', 'Billy Bob', 'Betty Sue', 'Louise Jr', 'Billy Bob', 
     'Misty', 'Leroy', 'Leroy', 'Billy Bob', 'Billy Bob', 'Betty Sue') 

print('''Welcome to the White Valley Name Counter. Enter a name, or "most" to see what name is the most used in this great city!''') 

print() 
keepgoing = 'y' 

while keepgoing == 'y': 
    count = 0 
    search = input("What name do you want to search for in White Valley database? ") 
    print() 
    data_list = list(data) 
    if search != "most": 
     print("There are {} people named {}".format(data_list.count(search),search)) 
     print() 
    elif search == "most": 
     print("{} is the most common. There are {} of them".format(
       data_list.count.most_common(data_list), search)) 
    keepgoing = input('''Want to search another name ("y" for yes)? ''') 
    print() 

Ich versuche, die Ausgabe wie folgt aussehen zu machen:

Welcome to the White Valley Name Counter. Enter a name, or "most" to see what name is the most used in this great city! 

What name do you want to search for in White Valley database? john 

There are 0 people named john 

Want to search another name ("y" for yes)? y 

What name do you want to search for in White Valley database? Betty Sue 

There are 79 people named Betty Sue 

Want to search another name ("y" for yes)? y 

What name do you want to search for in White Valley database? most 

Billy Bob is most common. There are 93 of them 

Want to search another name ("y" for yes)? n 
+0

Wie soll ein Benutzer nach einer Person namens "most" suchen? –

Antwort

0

Sie einen Zähler verwenden hierfür kann:

import collections 

data = ('Billy Bob', 'Misty', …) 
counts = collections.Counter(data) 

# add an additional 'Misty' 
counts.update(['Misty']) 

# most seen name 
if counts: 
    name, count = counts.most_common(1)[0] 
    print("most common name is {} with a count of {}".format(name, count)) 
else: 
    print("no names available") 

name = "Billy Bob" 
count = counts.get(name) 
if count is not None: 
    print("There are {} people named {}".format(count, name)) 
else: 
    print("There is no {}".format(name)) 
0

Nun die Anzahl der Male ein Name kommt zu zählen das ist vom Benutzer angegeben ist ziemlich einfach!

Lassen Sie uns eine kleine Funktion schreiben, um das zu behandeln und das Ergebnis zurückzugeben.

names = ("billy","bob","pete","bob",'pete','bob'); 


def count_my_name(name): 
    return ("The name %s occurs %s times." % (name,str(names.count(name)))); 

Wenn wir dieses Ergebnis mit dem Namen pete drucken möchten wir folgendes Ergebnis:

The name pete occurs 2 times.

nun die häufigste Name in der Liste zum Zählen wir noch eine nette kleine Funktion schreiben damit umgehen und das Ergebnis für uns zurückgeben.

names = ("billy","bob","pete","bob",'pete','bob'); 
def get_most_common_name(): 
    all_names = set(names); 
    most_common = max([names.count(i) for i in all_names]); 
    for name in all_names: 
     if names.count(name) == most_common: 
      return ("The most common name is %s occuring a total of %s times." % (name,str(most_common))); 

Welche gibt uns das Ergebnis: The most common name is bob occuring a total of 3 times.

Okay, so jetzt eine Erklärung für die zweite Funktion, was wir eigentlich hier?

Nun zuerst wir greifen unsere Tupel namens Namen, es hat Namen, aber einige von ihnen sind Duplikate und wir wollen nicht über den gleichen Namen mehrmals durchlaufen. Also machen Sie eine neue Variable namens all_names und machen Sie einen Satz aus dieser Liste.

Sätze sind nützlich, da sie alle Duplikate für uns entfernen.

Jetzt können wir die Anzahl der Male zählen ein Name in Namen erfolgt mit:

most_common = max([names.count(i) for i in all_names]);

Das die Anzahl der Namen gibt, die am meisten in unserem Tupel auftritt. Welches wäre 3.

Jetzt iterieren wir einfach über unsere Menge all_names und zählen, wie oft dieser Name in Namen auftritt.

Wenn der Name so oft in Namen wie unsere most_common Variable auftritt, haben wir den Namen, der am häufigsten vorkommt.

Hoffe, das hilft!

Verwandte Themen