测试所有N个变量是否都不同

编程入门 行业动态 更新时间:2024-10-25 18:33:04
本文介绍了测试所有N个变量是否都不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创造一个条件,其中所有选定变量都不相等.到目前为止,我的解决方案是比较每对扩展不佳的对:

I want to make a condition where all selected variables are not equal. My solution so far is to compare every pair which doesn't scale well:

if A!=B and A!=C and B!=C:

我想对多个变量(例如五个或更多)进行相同的检查,这让很多变量感到困惑.我可以做些什么来简化它?

I want to do the same check for multiple variables, say five or more, and it gets quite confusing with that many. What can I do to make it simpler?

推荐答案

创建一个集合,并检查集合中的元素数是否与传递给它的列表中的变量数相同:

Create a set and check whether the number of elements in the set is the same as the number of variables in the list that you passed into it:

>>> variables = [a, b, c, d, e] >>> if len(set(variables)) == len(variables): ... print("All variables are different")

一个集合没有重复的元素,因此,如果您创建一个集合并且它具有与原始列表中的元素数量相同的元素数量,那么您就会知道所有元素彼此都不相同.

A set doesn't have duplicate elements so if you create a set and it has the same number of elements as the number of elements in the original list then you know all elements are different from each other.

更多推荐

测试所有N个变量是否都不同

本文发布于:2023-11-29 23:34:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!