2014年10月15日 星期三

blog2 something about python

We need to write python in Assignment1, so I want to write something about python.

There are some structures that we can use in python. The first one is list, this is a very common structure, the elements of list can be dulplicate, so here is an example:







we create a list from the string and list includes all words of the string. The second is set, set is different from list in that the elemenst of set can not be dulplicate, so here is the example:

we can see that the duplicate words are not in the set, so set is very useful if you want to get a list that is not dulplicate. The third structure is dictionary or map, every element has a key and a value. we can create a simple dictionary like this:
so we can see that dictionary is something like a relation structure, every key is related a value.

Now I want to talk something about sorting, a method called sorted is very useful, now let's take a loot at it's basic function:
we can see that sorted will sort the element in list and return a new sorted list, but the function of sorted is much more powerful than this, suppose we have a dictionary and we want to sort it by key firstly, the sort it by value, we can do like this:

finally we get the list that has been sorted twice, first by key, then by value, but we have imported a library called operator, also, see the code that we used to gererate the original list? that is a very useful method, do a lot of things by a little code.

Ok, knowledge above is enough to solve the assignment1, but you need to design the simple algorithm yourself, I really did it using the methods above!














7 則留言:

  1. Thank you for the information above. It is really helpful for me to tackle some problems in assignment1.

    回覆刪除

    回覆刪除
    回覆
    1. That's great, I know this blog is gonna help someone, but if you want to use the nltk, you need to intsall some libraries, check this link(http://blog.sina.com.cn/s/blog_8af1069601019xmo.html)

      刪除
  2. Thank you for your sharing about Python. It's a good overview about the basic functions of Python which is easy to understand.

    回覆刪除
  3. Thank you for sharing, your blog explains how to use python to solve the assignment 1,.The words and the pictures are quite easy to understand, great work.

    回覆刪除
  4. I forgot to say thanks before, your blog is useful when doing the assignment! It can help me to be more familiar with using list, sort, key and value of python! Thanks!

    回覆刪除
  5. I think you can build the program based on NLTK. Using a library is a better working habit. Because that can make your own code focus on the problem you want to handle nor the data structure. Any way. your sharing is vary useful to me.

    回覆刪除
  6. Thanks for your post! For a Python beginner, your content is really helpful for my course assignment. On the other hand, I start to find that programming using Python is really interesting! Thanks.

    回覆刪除