Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2789200
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: Python/Ruby

2013-03-04 15:53:48

1、Python里面如何实现tuple和list的转换?
> list(a_tuple), tuple(a_list_)
请写出一段Python代码实现删除一个list里面的重复元素
> list(set(a_list))

点击(此处)折叠或打开

  1. #coding:utf-8
  2. #!/usr/bin/python
  3. # Filename: tuple2list.py

  4. a_tuple = (1,2,5,5,6,7)
  5. a_list =list(a_tuple)

  6. print "a tuple:"
  7. for j in a_tuple:
  8.     print j,

  9. print "na llst :"
  10. for i in a_list:
  11.     print i,

  12. print "na set:"
  13. a_list = list(set(a_list))
  14. for i in a_list:
  15.     print i,

  16. a tuple:
  17. 1 2 5 5 6 7
  18. a llst :
  19. 1 2 5 5 6 7
  20. a set:
  21. 1 2 5 6 7
2、Python中pass语句的作用是什么?
> 空代码块, 占位置


3、介绍一下Python下range()函数的用法?

返回一个迭代器的list
> range(end)
> range(start, end)
> range(start, end, step)

#range会直接生成一个list对象   
alist=range(0,100)
for i in alist:
    if i==99:
        print i
4、什么是lambda函数?它有什么好处?
> 匿名函数, 方便, 类函数式, 写回调方便

5、如何用Python来进行查询和替换一个文本字符串?

字符串替换str.replace('old','new')

点击(此处)折叠或打开

  1. str4="how can you do it like that , fuck you"
  2. if "you" in str4:
  3.     newstr=str4.replace("you", "he")
  4. print str4
  5. print newstr

  6. how can you do it like that , fuck you
  7. how can he do it like that , fuck he

6、Python里面如何生成随机数?

import random
for i in range(0,10):
    print random.randint(1,100)
































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