Chinaunix首页 | 论坛 | 博客
  • 博客访问: 283569
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 874
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-21 09:58
个人简介

traveling in cumputer science!!

文章分类

全部博文(82)

文章存档

2016年(13)

2015年(69)

我的朋友

分类: Python/Ruby

2015-09-01 17:40:28

1.different of assignment variable and list
    for variable: just copy the content of the other
    like below:

点击(此处)折叠或打开

  1. >>> var1 = 'hello'
  2. >>> var2 = var1
  3. >>> var1 = 'world'
  4. >>> var1
  5. 'world'
  6. >>> var2
  7. 'hello'
    for list: copy the address of the other
    like below:

点击(此处)折叠或打开

  1. >>> list1 = []
  2. >>> nested = [list1, list1, list1]
  3. >>> nested
  4. [[], [], []]
  5. >>> nested[1].append('Hello ')
  6. >>> nested
  7. [['Hello '], ['Hello '], ['Hello ']]
  8. >>> list1.append('world!!')
  9. >>> nested
  10. [['Hello ', 'world!!'], ['Hello ', 'world!!'], ['Hello ', 'world!!']]
2. contrast from string\list\pair
    the difference like the code below:

点击(此处)折叠或打开

  1. strings = 'I am a good student!!'
  2. >>> lines = ['zhang', 'pei', 'hua', 'hello', 'world']
  3. >>> pairs = (6, 'dog', 'cat')
  4. >>> strings[2], lines[2], pairs[2]
  5. ('a', 'hua', 'cat')
  6. >>> len(strings), len(lines), len(pairs)
  7. (21, 5, 3)
3. the defence style function
    prevent unreasonable input and suggest reasonable solution. like below function the argument  must be string,if not return warn.
    

点击(此处)折叠或打开

  1. >>> def tag(word):
  2. ... assert isinstance(word, basestring), "argument \
  3. ... to tag() must be a string"
  4. ... if word in ['a', 'the', 'all']:
  5. ... return 'det'
  6. ... else:
  7. ... return 'noun'
  8. ...
  9. >>> tag('hello')
  10. 'noun'
  11. >>> tag(['hello'])
  12. Traceback (most recent call last):
  13.   File "<stdin>", line 1, in <module>
  14.   File "<stdin>", line 3, in tag
  15. AssertionError: argument     to tag() must be a string
4. dictionary in python like hash pair in other programming language
    the character reflected in below code:

点击(此处)折叠或打开

  1. >>> pos = {'colorless':'ADJ', 'ideas':'N', 'sleep':'V'}
  2. >>> pos
  3. {'sleep': 'V', 'ideas': 'N', 'colorless': 'ADJ'}
  4. >>> pos2 = dict(colorless='ADJ', ideas='N', sleep='V')
  5. >>> pos2
  6. {'sleep': 'V', 'ideas': 'N', 'colorless': 'ADJ'}
  7. >>> pos['sleep']
  8. 'V'
  9. >>> pos['sleep'] = ['N','V']
  10. >>> pos
  11. {'sleep': ['N', 'V'], 'ideas': 'N', 'colorless': 'ADJ'}






    
阅读(1099) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~