Chad's Logs

Python sets

Just a quick programming tip/update.

I had to parse a file of 1.2 million lines. For those who don’t know, that is big. I needed to go through the file and keep track of the unique items in the file.

My initial implementation looked like this:

id = []
for item in items:
    if item.id in ids:
        continue
    else:
        ids.append(item.id)
    # do stuff

And that code ran for about 12 hours and was still not finished when I canceled it. I then learned a little more about sets in Python. By definition a set can’t have repeats, which makes searching through the structure for an item more efficient. The following solution finished in less than 5 minutes:

id = set()
for item in items:
    if item.id in ids:
        continue
    else:
        ids.add(item.id)
    # do stuff

I like sets. ;)

Posted in Code, Python |

Be a man, use your…

One of my favorite verses of scripture is 2 Nephi 2:26. This verse talks about the Savior’s gift to us and that, because of his actions, we are “free forever” and can choose between good and evil, “to act for themselves and not to be acted upon.

I think a lot of times we forget (or maybe even don’t know) this vital truth about life. We are not rocks! We are not trees! We are not here to be acted upon. But what else would you say about someone who eats whatever is put in front of them (as I sit here eating a raspberry treat when I don’t even like raspberry, but I am eating it because it is here, within reach)? What would you say about the person who sits and watches hours of TV a night? Or the guy who gives into his base needs/desires/instincts and doesn’t attempt to control his appetites? I would say that they are being acted upon.

Note: I am writing this for myself, but I believe this applies to a lot of people.

Maggie thinks I am going through a mid-life crisis. However, I say that I am only now discovering that I need to act, I need to work towards being greater and better every day, week, month, and year.

Tangent: New Year’s resolutions are a joke. Why would you base your whole year’s success (or, more likely, failure) on some decisions you made at the beginning of the year? I mean, goals are great and all, but why not make them every month? Or better yet, once a month for big things, and then re-evaluate every week.

I recently read a very interesting and helpful article from what is rapidly becoming one of my most favorite web sites (artofmanliness.com), about what it means to grow up and be a man. I highly recommend everyone (not just men) read the article. I also highly recommend everyone (mostly men) visit frequently The Art of Manliness site to learn the lost art of becoming (and what it means to become) a man.

From the article there are two concepts addressed: freedom from and freedom to. Freedom from (aka negative freedom) can be summed up as “I am a slave to no man.” This is the ability that everyone has (as an adult) to choose between options. As an adult we are free to choose to do things that are not prohibited by law. We are free to go to parties, sleep in late, use drugs, and sleep around. As the image below illustrates, we are free to choose between options.

Freedom to (aka positive freedom) can be summed up as “I am my own master.” This is the ability that every adult has to master his appetites and to put up his own constraints and limits. A boy that wants to become a man places his own restrictions on his actions. The man sees things like the use of drugs and promiscuous sex as not uplifting or wholesome activities. The man puts up his own restrictions, knowing that he is improving his life and happiness, not by doing whatever he wishes, but by limiting himself and his appetites to improve the many aspects of his life.

Too often we think that being happy is the ability to choose between options. We experience the freedom to do what we wish after being restricted during childhood. We can stay up late, eat what we want when we want, and spend our time doing what we wish. However, growing up and becoming a real man/woman is realizing that being able to do those things doesn’t mean that we should. Growing up means choosing what is wise and right. I choose to put restrictions on myself and my actions because I know that doing those things is stupid, useless, or even damaging. I am not perfect and I do things all the time that are stupid, useless, and damaging. But I am trying to recognize those things each day, week, month, and year and I’m trying to be a better man as I grow.

In short, be a man, use your brain.

Posted in Growing Up, Religion |

Bad Info: Potatoes are good

In a recent study in the New England Journal of Medicine (June 23, 2011) there was a study published entitled Changes in Diet and Lifestyle and Long-Term Weight Gain in Women and Men. I first found about about this study from the following WSJ video produced the same day the journal issue was published.

When I heard the part about the potatoes it shocked me. I guess I should reveal my slight bias here, I grew up on a potato farm and my Dad still goes seed potatoes in Montana. But I was still shocked, since when is eating a vegetable bad for you? This just didn’t sit right.

I heard the study cited several times during the day including on a local radio talk show. Each time I heard it they pointed out that potatoes were found to contribute to more weight gain than anything else they looked at, just behind potato chips.

I decided to take a closer look at the study. Now I didn’t read the whole thing, but I did see this little nugget of information that some how no one else decided to share or at least was miss represented. Under the results section of the article there is a table of information (Table 2. Pooled, Multivariable-Adjusted Results for the Relationships between Changes in Dietary Habits and Weight Change.). In this table it shows the “Potatoes” category. According to this table over a 4 year period potatoes contribute on average about 1.28 lbs to a persons weight. But there was a little footnote to the potato:

For the categories … potatoes … subtypes were evaluated together in the full, multivariable-adjusted model in place of the overall food group.

Meaning that the two subtypes underneath potatoes (french fried and boiled, baked, mashed) were lumped in together. This confirmed my suspicion. People had been reporting that potatoes were bad for you contributing significantly to weight gain. But what this table tells me is that french fries not potatoes in general contribute significantly to weight gain. French fries indeed were found to contribute an average of (low end) 2.29 lbs over 4 years while other forms of potatoes (boiled, baked, and mashed) contributed 0.26 lbs.

People, please don’t misrepresent the facts. Potatoes are not bad for you. The way you eat them (what you put on them) might be, but the potato in and of itself it actually really good for you.

What I got from this study, be smart.

Posted in Random |