ZZucker 342 Practically a Master Poster

Just another word frequency program that shows you how to sort the output by frequency:

# count words in a text and show the first ten items
# by decreasing frequency using a list of tuples

# sample text for testing (could come from a text file)
text = """\
My name is Fred Flintstone and I am a famous TV
star.  I have as much authority as the Pope, I
just don't have as many people who believe it.
"""

word_freq = {}

word_list = text.split()

for word in word_list:
    # word all lower case
    word = word.lower()
    # strip any trailing period or comma
    word = word.rstrip('.,')
    # build the dictionary
    count = word_freq.get(word, 0)
    word_freq[word] = count + 1

# create a list of (freq, word) tuples for sorting by frequency
freq_list = [(freq, word) for word, freq in word_freq.items()]

# sort the list by the first element in each tuple (default)
freq_list.sort(reverse=True)

print "The ten most frequent words are:"
for n, tup in enumerate(freq_list):
    # print the first ten items
    if n < 10:
        freq, word = tup
        print freq, word

"""
my output -->
The ten most frequent words are:
3 i
3 as
2 have
1 who
1 tv
1 the
1 star
1 pope
1 people
1 name
"""
ZZucker 342 Practically a Master Poster

February 1865 is the only month in recorded history not to have a full moon.

ZZucker 342 Practically a Master Poster

Albert Einstein was born into a Jewish family in Ulm, Germany on March 14, 1879. Note that March 14 is somehow connected to 3.14 (pi), so Albert was born on pi-day.

ZZucker 342 Practically a Master Poster

Here is an example how to output a formatted table from a loop:

# print a table (16 columns) of ASCII characters from 1 to 127

# Bumsfeld, thanks for the dictionary
controls_dic = {
1: 'SOH', 2: 'STX', 3: 'ETX', 4: 'EOT', 5: 'ENQ', 6: 'ACK', 7: 'BEL',
8: 'BS', 9: 'HT', 10: 'LF', 11: 'VT', 12: 'FF', 13: 'CR', 14: 'SO',
15: 'SI', 16: 'DLE', 17: 'DC1', 18: 'DC2', 19: 'DC3', 20: 'DC4', 21: 'NAK',
22: 'SYN', 23: 'ETB', 24: 'CAN', 25: 'EM', 26: 'SUB', 27: 'ESC', 28: 'FS',
29: 'GS', 30: 'RS', 31: 'US'}

n = 1
for k in range(1, 128):
    if k < 32:
        s = controls_dic[k]
    else:
        s = chr(k)
    if n % 16 > 0:
        print "%4s" % s,
    else:
        print "%4s" % s
    n += 1
ZZucker 342 Practically a Master Poster

Over eighty percent of people will yawn within twenty seconds of seeing another person yawn.

I just yawned reading this!

ZZucker 342 Practically a Master Poster

A function to allow for a relatively secure input of dates:

# a more secure way to enter a date

import datetime as dt

def enter_date():
    """
    asks to enter year, month and day and returns
    date info as <type 'datetime.datetime'>
    """
    def extract_date(s):
        try:
            # month given as a decimal number
            date = dt.datetime.strptime(s, "%d %m %Y")
        except:
            try:
                # month given as a 3 letter abreviation
                date = dt.datetime.strptime(s, "%d %b %Y")
            except:
                return None
        return date

    while True:
        year = raw_input("Enter year (eg. 1979): ")
        # you can enter number of month, full name or 3 letter abbreviation
        month = raw_input("Enter month (eg. 9 or sep): ")
        day = raw_input("Enter the day of the month: ")
        date = extract_date(day + " " + month[:3].title() + " " + year)
        if date == None:
            print "Illegal info!"
            continue
        else:
            return date


date = enter_date()

# testing ...
#print date, type(date)           # 2001-09-11 00:00:00 <type 'datetime.datetime'>
#print date.strftime("%m/%d/%Y")  # 09/11/2001
#print date.strftime("%d%b%Y")    # 11Sep2001

# do something with it ...
today = dt.datetime.today()
age = today - date
# assumes week starts with Monday
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
week_day = weekdays[dt.date.weekday(date)]
print "Today is %s" % today.strftime("%d%b%Y")
print "%s was %d days ago on a %s" % (date.strftime("%d%b%Y"), age.days, week_day)

"""
my output -->
Enter year (eg. 1979): 2001
Enter month (eg. 9 or sep): september
Enter the day of the month: 11
Today is 03Mar2008
11Sep2001 was 2365 days ago on a …
ZZucker 342 Practically a Master Poster

If you want to clear the whole canvas instance of all items on it, use:
canvas.delete('all')

vegaseat commented: you are getting good with Python +7
ZZucker 342 Practically a Master Poster

Blue eyed people are twice as likely to need glasses than brown eyed people.

DeanMSands3 commented: It all makes sense now! +0
ZZucker 342 Practically a Master Poster

I cannot conceive of a God who rewards and punishes his creatures, or has a will of the kind that we experience in ourselves.

Neither can I nor would I want to conceive of an individual that survives his physical death; let feeble souls, from fear or absurd egoism, cherish such thoughts.

I am satisfied with the mystery of the eternity of life and with the awareness and a glimpse of the marvelous structure of the existing world, together with the devoted striving to comprehend a portion, be it ever so tiny, of the Reason that manifests itself in nature.

--- Albert Einstein

vegaseat commented: nice quote +7
ZZucker 342 Practically a Master Poster

You have two lists, one list with all the cities you wanted to visit in your life, and another list of cities you have already visited. Now you want to create a new list of cities you have not visited yet:

# subtract/remove elements of one list from another

city = ['Lisbon', 'Paris', 'Reno', 'London', 'Oslo', 'Ypsilanti']

visited = ['Reno', 'Ypsilanti']

not_visited = [c for c in city if c not in visited]
# order of remaining elements has not changed
print not_visited

# simpler ...
not_visited = list(set(city) - set(visited))
# order of remaining elements has changed
print not_visited

"""
my result --->
['Lisbon', 'Paris', 'London', 'Oslo']
['Paris', 'Oslo', 'London', 'Lisbon']
"""
ZZucker 342 Practically a Master Poster

Yahoo almost picked Hooray for its company name as it started up.

ZZucker 342 Practically a Master Poster

Just in time for Valentine's day:
John said to Mary, "I'll bet you a Dollar that I can kiss you on the lips without touching them."
"You're crazy," said Mary. "That's impossible. Here's a Dollar that says you can't."
The two Dollars were placed on the table and John then hugged Mary and for two minutes kissed her passionately. She broke away at last, panting and disheveled, and said, "You did touch my lips!"
John pushed the money toward her and said, "So I lose."

Ancient Dragon commented: Ha Ha good one :) +23
maravich12 commented: Yeah..awesome joke. +1
iamthwee commented: Thanks for sharing, I' gonna use this! +13
ZZucker 342 Practically a Master Poster
SpS commented: Nice Link +4
ZZucker 342 Practically a Master Poster

The letters 'a', 'b', 'c', and 'd' do not appear in the English spelling of numbers 1 to 99.

ZZucker 342 Practically a Master Poster

Babies come from the cabbage patch of course. Anything else are just old wife tales!

Ancient Dragon commented: That's where the Cabage Patch Dolls came from? :) +21
ZZucker 342 Practically a Master Poster

News orgs that scare you on a daily basis with the bird flu pandemic, recession, global heating, price of oil, nukes in Iran, riots in some country, democrat landslide ...

I better stop, I don't want to scare any DaniWeb posters.

Ancient Dragon commented: HaHa -- I liked that :) +21