python组合无重复三位数的实例

yipeiwu_com6年前Python基础

# -*- coding: utf-8 -*-

# 简述:这里有四个数字,分别是:1、2、3、4
#提问:能组成多少个互不相同且无重复数字的三位数?各是多少?
def f(n):
 list=[]
 count=0
 for i in range(1,n+1):
  for j in range(1, n+1):
   for k in range(1, n+1):
    if i!=j and j!=k and i!=k:
     list.append(i*100+j*10+k)
 count=len(list)
 print count
 return list
 
if __name__=="__main__":
 print f(4)

运行结果:

24
[123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]

以上这篇python组合无重复三位数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python选课系统开发程序

Python选课系统开发程序

本文程序针对Python选课系统进行开发,供大家参考,具体内容如下 角色:学校、学员、课程、讲师 要求: 1. 创建北京、上海 2 所学校 2. 创建linux , python , g...

Python matplotlib画图实例之绘制拥有彩条的图表

Python matplotlib画图实例之绘制拥有彩条的图表

生产定制一个彩条标签。 首先导入: import matplotlib.pyplot as plt import numpy as np from matplotlib import...

python BeautifulSoup使用方法详解

直接看例子:复制代码 代码如下:#!/usr/bin/python# -*- coding: utf-8 -*-from bs4 import BeautifulSouphtml_doc...

python xml.etree.ElementTree遍历xml所有节点实例详解

python xml.etree.ElementTree遍历xml所有节点 XML文件内容: <students> <student name='刘备' s...

详解python的四种内置数据结构

对于每种编程语言一般都会规定一些容器来保存某些数据,就像java的集合和数组一样python也同样有这样的结构 而对于python他有四个这样的内置容器来存储数据,他们都是python语...