我在一个列表中创建了一个笛卡尔积,现在我想随机取出4个不同的元组(我在文章末尾尝试过)。在shape = ['triangle' , 'square' , 'circle' , 'cross']

color = ['green' , 'red' , 'blue' , 'pink']

__cartesianPsc__ = list(itertools.product(shape , color))

我的笛卡尔积

^{pr2}$

现在我想随机得到4个不同的元组,三角形,正方形,圆圈和十字,4种不同的颜色,绿色,红色,粉色,蓝色,但是每种颜色和每种形状/颜色只存在一次

示例:

第一个选择first = ('triangle', 'green')

second = ('square', 'red')

third = ('circle', 'blue')

fourth = ('cross', 'pink')

第二个first = ('circle', 'blue')

second = ('cross', 'red')

third = ('triangle', 'pink')

fourth = ('square', 'green')

等等

有人知道怎么做吗?我试过用随机范围从三角形、正方形、圆形、十字中挑一个,但我不知道怎么做才能得到不同的颜色

----我的旧代码-----

我有一个问题,它总是(三角形,绿色)作为一个元组,但是其他3个元组(每次)都不同。在shape = ['triangle' , 'square' , 'circle' , 'cross']

color = ['green' , 'red' , 'blue' , 'pink']

__cartesianPsc__ = list(itertools.product(shape , color))

while True:

se1 = random.randrange(0, 4, 1)

se2 = random.randrange(5, 8, 1)

se3 = random.randrange(9, 12, 1)

se4 = random.randrange(13, 16, 1)

# safe the randomly choosen tuple of the cartesian product

first = __cartesianPsc__[se1] #triangle X color

second = __cartesianPsc__[se2] # square X color

third = __cartesianPsc__[se3] #circle X color

fourth = __cartesianPsc__[se4] #cross X color

# if statement to stop the While-LOOP only if there are 4 tuples with

# 4 different shapes and colors !

"""Problem: (triangle, green) is always a tuple, no other color with triangle

if second[1] != first[1] and second[1] != third[1] and second[1] != fourth[1] \

and third[1] != first[1] and third[1] != second[1] and third[1] != fourth[1] \

and fourth[1] != first[1] and fourth[1] != second[1] and fourth[1] != third[1] \

and first[1] != second[1] and first[1] != third[1] and first[1] != fourth[1]:

"""

break

你好,马丁:)

python 从列表中随机选择_使用条件Python从列表中随机选择相关推荐

  1. python中有那几种赋值_详解Python列表赋值复制深拷贝及5种浅拷贝

    概述 在列表复制这个问题,看似简单的复制却有着许多的学问,尤其是对新手来说,理所当然的事情却并不如意,比如列表的赋值.复制.浅拷贝.深拷贝等绕口的名词到底有什么区别和作用呢? 列表赋值 # 定义一个新 ...

  2. python列表中随机选择_如何在Python中从列表中随机选择一个项目?

    python列表中随机选择 Python random module provides an inbuilt method choice() has an ability to select a ra ...

  3. python中如何追加_如何在Python中将元素添加到列表中-追加,扩展和插入

    在Python中使用列表时,您通常会希望向列表中添加新元素. Python列表数据类型具有三种添加元素的方法:append()-将单个元素追加到列表. extend() -将iterable的元素添加 ...

  4. python从后面删除重复项_如何从Python列表中删除重复项

    如何从Python列表中删除重复项 了解如何从Python中的List中删除重复项技巧. 实例 从列表中删除任何重复项: mylist = ["a", "b", ...

  5. python以某种编码进行打印_如何在一场面试中展现你对Python的coding能力?

    (点击上方快速关注并设置为星标,一起学Python) 来源:Python数据科学    链接: https://mp.weixin.qq.com/s/KwjLpeRT4l-pOc_iRCLWzg 如果 ...

  6. python set 排序_python set 排序_如何在Python中使用sorted()和sort()

    点击"蓝字"关注我们 ?"Python基础知识" 大卫·丰达科夫斯基  著 18财税3班 李潇潇    译 日期:2019年5月6日 一. 使用sorted() ...

  7. 回归分析中自变量共线性_具有大特征空间的回归分析中的变量选择

    回归分析中自变量共线性 介绍 (Introduction) Performing multiple regression analysis from a large set of independen ...

  8. python一行输出多个数据_如何在Python中让两个print()函数的输出打印在一行内?

    1.两个连续的print()函数为什么在输出时内容会分行显示? 解:print()中有两个默认参数sep和end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用','代替空格,且默认末尾加 ...

  9. python语言中运算符号_详细介绍Python语言中的按位运算符

    <从问题到程序:用Python学编程和计算>--2.11 补充材料 本节书摘来自华章计算机<从问题到程序:用Python学编程和计算>一书中的第2章,第2.11节,作者:裘宗燕 ...

最新文章

  1. 一招一式, 成就“霸业”必做的9件大事
  2. C#基础知识之base、this、new、override、abstract梳理
  3. 遗传算法可用什么算法代替_获取可用密码算法的列表
  4. spring security:第一个程序解析
  5. MySQL高级 —— 查询性能优化
  6. java文件不能转class_安了jdk 却不能将.java文件转换成.class 文件,一运行就说‘javac’不是内部或外部指令,却能运行.class文...
  7. OpenGL学习笔记 transform feedback缓存粒子系统示例分析
  8. idea如何打开pom引用依赖_idea 怎么引入在pom.xml的jar
  9. tableView 获取网络图片,并且设置为圆角(优化,fps)
  10. java 时间格式化_还在用SimpleDateFormat格式化时间?小心经理锤你
  11. SQL Server-【知识与实战IV】多表查询、个数计算、多重约束条件、两表间的交集、多表之间的关系分析
  12. 两天撸一个天气应用微信小程序
  13. JavaScript 闭包理解
  14. 国内OA 安全现状初探
  15. 中国期货市场监控中心爬虫
  16. 学信网-账号被注销解决方法
  17. 蓝天学校计算机教学反思,小学四年级上册《飞向蓝天的恐龙》教学反思
  18. 【Oracle之AWR报告解析】
  19. 【python-docx 05】操作页眉和页脚
  20. Vue入门(二)v-show、v-if、v-bind

热门文章

  1. 数字系统设计的基础知识
  2. HDU1850 Being a Good Boy in Spring Festival【Nim博弈】
  3. matlab 三维图像的绘制
  4. 【脑经急转弯】—— 猜额头上的数字
  5. matlab 实用程序片段
  6. C++ 对象模型(二)
  7. python写csv文件按升序排列_用python给csv里的数据排序的具体代码
  8. android 7.0原生动态,Android7.0适配教程,心得
  9. 专科python应届生工资多少-Python最好的就业方向与就业岗位技能要求,能赚多少钱?...
  10. python课程开课吧怎么样-廖雪峰总结的Python商业爬虫教程,请查收!