python爬虫实例100-Python 练习实例1
本站寻求有缘人接手,详细了解请联系站长QQ1493399855
#16
zavier
126***0796@qq.com
49
使用列表形式,并计算总结:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 原答案没有指出三位数的数量,添加无重复三位数的数量
d=[]
for a in range(1,5):
for b in range(1,5):
for c in range(1,5):
if (a!=b) and (a!=c) and (c!=b):
d.append([a,b,c])
print "总数量:", len(d)
print d
126***0796@qq.com4年前 (2017-04-13)
#15
盼盼
946***399@qq.com
25
将for循环和if语句综合成一句,直接打印出结果
#!/usr/bin/env python
list_num = [1,2,3,4]
list = [i*100 + j*10 + k for i in list_num for j in list_num for k in list_num if (j != i and k != j and k != i)]
print (list)
946***399@qq.com4年前 (2017-04-16)
#14
习惯乌龙茶
rea***ngtao@163.com
16
参考方法(设置最大,最小值):#!/usr/bin/python
line=[]
for i in range(123,433):
a=i%10
b=(i%100)//10
c=(i%1000)//100
if a!=b and b!=c and a!=c and 0
print (i)
line.append(i)
print('the total is :',len(line))
习惯乌龙茶
rea***ngtao@163.com4年前 (2017-04-20)
#13
成科
121***125@qq.com
17
python3 下参考方案:#!/usr/bin/env python3
#coding:utf-8
num=[1,2,3,4]
i=0
for a in num:
for b in num:
for c in num:
if (a!=b) and (b!=c) and (c!=a):
i+=1
print(a,b,c)
print('总数是:',i)
121***125@qq.com4年前 (2017-04-24)
#12
白色帽子
liu***7@163.com
10
参考方法:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#用集合去除重复元素
import pprint
list_num=['1','2','3','4']
list_result=[]
for i in list_num:
for j in list_num:
for k in list_num:
if len(set(i+j+k))==3:
list_result+=[int(i+j+k)]
print("能组成%d个互不相同且无重复数字的三位数: "%len(list_result))
pprint.pprint(list_result)
liu***7@163.com3年前 (2017-05-22)
#11
Chyroc
che***unpeng@foxmail.com
33
python自带这个函数的#!/usr/bin/env python3
from itertools import permutations
for i in permutations([1, 2, 3, 4], 3):
print(i)
che***unpeng@foxmail.com3年前 (2017-05-31)
#10
weapon
965***124@qq.com
7
补充一下:
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#补充一下
k = ''
for j in range(0, len(i)):
k = k + str(i[j])
print (int(k))
965***124@qq.com3年前 (2017-06-07)
#9
逸章
me@***gdewen.com
没事找事之位运算# coding:utf-8
#从 00 01 10 到 11 10 01
for num in range(6,58):
a = num >> 4 & 3
b = num >> 2 & 3
c = num & 3
if( (a^b) and (b^c) and (c^a) ):
print a+1,b+1,c+1
me@***gdewen.com3年前 (2017-06-13)
#8
DCGDDD
805***426@qq.com
18
考虑减少冗余判断和循环,做如下优化;
#!/usr/bin/python3
for i in range(1, 5):
for j in range(1, 5):
if (j==i) :
continue;
for k in range(1, 5):
if (k==i or k==j):
print(i,j,k);
805***426@qq.com3年前 (2017-08-13)
#7
嘿嘿
123***.com
9
Python3 测试实例:
list = [1,2,3,4]
for i in list:
list1 = list.copy()
list1.remove(i)
for j in list1:
list2 = list1.copy()
list2.remove(j)
for k in list2:
print(i, j, k)
123***.com3年前 (2017-09-07)
#6
Krystal
104***7454@qq.com
4
加入了format函数
#!/usr/bin/python
#-*- coding: UTF-8 -*-
list_num = [1,2,3,4]
list = [i*100 + j*10 + k for i in list_num for j in list_num for k in list_num if ( i != j and i != k and j != k)]
d = len(list)
print('1,2,3,4能组成 %d 个互不相同且无重复数字的三位数。' % d)
print('他们各是:%s' % list)
104***7454@qq.com3年前 (2017-09-25)
#5
红烧土豆块
545***924@qq.com
数量统计用个做自加就够了
#coding=utf-8
t = 0
for i in permutations('1234',3):
print(''.join(i))
t += 1
print("不重复的数量有:%s"%t)
545***924@qq.com3年前 (2017-09-27)
#4
这个好好玩
303***045@qq.com
参考:
#encoding=utf8
#有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
d = []
for j in range(1,5):
for k in range(1,5):
for l in range(1,5):
if l!=j!=k!=l:
d.append(int(str(j)+str(k)+str(l)))
print d
print len(d)
303***045@qq.com3年前 (2017-11-07)
#3
XMDERO
124***5621@qq.com
6
#直接用列表推导式
[(x,y,z) for x in range(1,5) for y in range(1,5) for z in range(1,5) if(x!=y)and(x!=z)and(y!=z)]
124***5621@qq.com3年前 (2017-11-16)
#2
阳光不锈
173***979@qq.com
14
参考方法:#coding=utf-8
print("----------递归法 -------------")
#递归法
def f01(i):
if i==123:
return
else:
if (set('567890') & set(str(i))==set()) and (len(set(str(i)))==3):
f01(i-1)
f01(432)
print("----------生成器法-------------")
#生成器法
def f02():
for i in range(123,433):
yield i
for i in f02():
173***979@qq.com3年前 (2018-03-04)
#1
whitestonex
851***07@qq.com
16
import itertools
DataIn = list('1234')
TmpList = []
for x in list(itertools.combinations(DataIn,3)):
TmpList = TmpList + list(itertools.permutations(x,3))
for i in TmpList:
851***07@qq.com3年前 (2018-03-13)