日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python 案例001 (有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数)...

發布時間:2024/6/30 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 案例001 (有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目內容來自網絡 ,加入了個人理解的過程 ,和點評


#!/usr/bin/python # -*- coding: UTF-8 -*-#Author : Wumi#題目:有四個數字:1、2、3、4,能組成多少個互不相同且無重復數字的三位數?各是多少? #程序分析:可填在百位、十位、個位的數字都是1、2、3、4。組成所有的排列后再去 掉不滿足條件的排列。# 如果四個數字中有個零 ,難度就稍微增加了 num =[] # list for i in range(1,5): # range 的高位是不包括在內的for j in range(1,5):for k in range(1,5):if i!=j and i != k and j!=k :print i,j,kprint k,j,i # reverse numnum.append([i,j,k]) print "how many num we have of the permutation and combination :",len(num) # 24# big tall up #將for循環和if語句綜合成一句,直接打印出結果lists =[ (i*100+10*j+k) for i in range(1,5) for j in range(1,5) for k in range(1,5) if ( i!=j and i !=k and j!=k )] print len(lists) print len(lists),":",lists# use set function to remove the duplicate numbers ,set 是沒有重復數據的 # one important tips is that the alphabet can uses join function + alphabetList = ['1','2','3','4'] numberList = [ int(i+j+k) for i in alphabetList for j in alphabetList for k in alphabetList if (len(set(i+j+k)) ==3)] print numberList ,":",len(numberList) # question is why i != k and != j ? 也就是為什么沒有重復位 ?? 譬如 111 or 122 等# here is the core reason :: lists =['1','2','3'] for i in lists:for j in lists:print set(i+j) # add memeber into set using plus + ---> set(['1', '2']) ,no replicate dataprint int(i+j) """ set(['1']) 11 set(['1', '2']) 12 set(['1', '3']) 13 set(['1', '2']) 21 set(['2']) 22 set(['3', '2']) 23 set(['1', '3']) 31 set(['3', '2']) 32 set(['3']) 33 """

轉載于:https://www.cnblogs.com/TendToBigData/p/10501231.html

總結

以上是生活随笔為你收集整理的Python 案例001 (有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。