python例子

Posted 浪子回头-小东清

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python例子相关的知识,希望对你有一定的参考价值。

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)]

 

from functools import reduce

fn = lambda x, code=\',\': reduce(lambda x, y: [str(i)+code+str(j) for i in x for j in y], x)
# 直接调用fn(lists, code)
list1 = [1, 2]
list2 = [3, 4]
list3 = [5, 6]
res = fn([list1, list2, list3])
print(res)
[\'1,3,5\', \'1,3,6\', \'1,4,5\', \'1,4,6\', \'2,3,5\', \'2,3,6\', \'2,4,5\', \'2,4,6\']
一、笛卡尔积:itertools.product(*iterables[, repeat])

直接对自身进行笛卡尔积:

import itertools
for i in itertools.product(\'ABCD\', repeat = 2):
    print (\'\'.join(i),end=\' \')

输出结果: 
AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD 
print (”.join(i))这个语句可以让结果直接排列到一起 
end=’ ‘可以让默认的输出后换行变为一个空格

两个元组进行笛卡尔积:

import itertools
a = (1, 2, 3)
b = (\'A\', \'B\', \'C\')
c = itertools.product(a,b)
for i in c:
    print(i,end=\' \')

输出结果: 
(1, ‘A’) (1, ‘B’) (1, ‘C’) (2, ‘A’) (2, ‘B’) (2, ‘C’) (3, ‘A’) (3, ‘B’) (3, ‘C’)

二、排列:itertools.permutations(iterable[, r])

import itertools
for i in itertools.permutations(\'ABCD\', 2):
    print (\'\'.join(i),end=\' \')

输出结果: 
AB AC AD BA BC BD CA CB CD DA DB DC

三、组合:itertools.combinations(iterable, r)

import itertools
for i in itertools.combinations(\'ABCD\', 3):
    print (\'\'.join(i))

输出结果: 
ABC 
ABD 
ACD 
BCD

四、组合(包含自身重复):itertools.combinations_with_replacement(iterable, r)

import itertools
for i in itertools.combinations_with_replacement(\'ABCD\', 3):
    print (\'\'.join(i),end=\' \')

输出结果: 
AAA AAB AAC AAD ABB ABC ABD ACC ACD ADD BBB BBC BBD BCC BCD BDD CCC CCD CDD DDD

 

以上是关于python例子的主要内容,如果未能解决你的问题,请参考以下文章

分享几个实用的代码片段(附代码例子)

如何创建片段以重复变量编号中的代码行

有条件地导入 python 类的片段

常用python日期日志获取内容循环的代码片段

python 有用的Python代码片段

Python 向 Postman 请求代码片段