码迷,mamicode.com
首页 > 编程语言 > 详细

python例子

时间:2019-02-13 09:17:27      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:lis   orm   技术   mat   tools   odi   coding   rom   png   

技术图片

技术图片
list1 = [[张三,,未婚,20],[李四,,已婚,28],[小红,,未婚,18],[小芳,,已婚,25]]
output = open(data.xls,w,encoding=gbk)
output.write(name\tgender\tstatus\tage\n)
for i in range(len(list1)):
    for j in range(len(list1[i])):
        output.write(str(list1[i][j]))    #write函数不能写int类型的参数,所以使用str()转化
        output.write(\t)   #相当于Tab一下,换一个单元格
    output.write(\n)       #写完一行立马换行
output.close()
View Code

技术图片

技术图片
list1 = [[张三,,未婚,20],[李四,,已婚,28],[小红,,未婚,18],[小芳,,已婚,25]]
output = open(data.txt,w,encoding=gbk)
output.write(name,gender,status,age\n)
for row in list1:
    rowtxt = {},{},{},{}.format(row[0],row[1],row[2],row[3])
    output.write(rowtxt)
    output.write(\n)
output.close()
View Code

 组合

combinations:不考虑顺序的排列组合
import itertools
print list(itertools.combinations([1,2,3,4],3))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]


permutations:考虑顺序的排列组合

import itertools
print list(itertools.permutations([1,2,3,4],2))  #第二个参数2表示要选几个对象
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]

 

from itertools import product
l = [1, 2, 3]
print (list(product(l, l)))
print (list(product(l, repeat=3)))

结果:
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]

[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

 

python例子

标签:lis   orm   技术   mat   tools   odi   coding   rom   png   

原文地址:https://www.cnblogs.com/xaiobong/p/10368039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!