简体   繁体   English

重复时仅显示 1 个元素

[英]Display only 1 element when it's a repetition

I would like print a result without duplicate with my multiplication我想用我的乘法打印一个不重复的结果

Here an example:这里有一个例子:


5*3*2=30
2*3*5=30
5*2*3=30
3*2*5=30
.....

All these element are from my list that I browse and you can see it's always =30所有这些元素都来自我浏览的列表,你可以看到它总是 =30

So I would like display only the first element (5*3*2) and not others because they are the same.所以我只想显示第一个元素(5 * 3 * 2)而不是其他元素,因为它们是相同的。

To be more accurate, here an example what I have:更准确地说,这里有一个例子:

list = ['5*3*2','5*2*3','2*3*5','2*5*3']

for i in list:       
     if eval(i) == eval(i)+1 ??? (I dont know how to say the next element)
            print(eval(i))

Thanks for reading谢谢阅读

Something like this with not in will help you.像这样的东西not in会帮助你。

#python 3.5.2
list = ['5*3*2','5*2*3','6*9','2*3*5','2*5*3','8*3','9*6']
elm = []
for i in list:   
    elm_value = eval(i)
    if elm_value not in elm:
        elm.append(elm_value)
print(elm)

DEMO: https://rextester.com/QKV22875演示: https://rextester.com/QKV22875

The comparison:比较:

eval(i) == eval(i)+1 

Will compare if the the number i is equal to i + 1 , which will always return False .将比较数字i是否等于i + 1 ,这将始终返回False I'm sure you mean to use i as an index and simply wanted to compare if the current element is equal to the next element in the list.我确定您的意思是使用i作为索引,只是想比较当前元素是否等于列表中的下一个元素。 However, doing this doesn't really keep track of duplicates, since you have to consider everything else in the list.但是,这样做并不能真正跟踪重复项,因为您必须考虑列表中的所有其他内容。

Its also not a good idea to use list as a variable name, since it shadows the builtin function list .使用list作为变量名也不是一个好主意,因为它会隐藏内置的 function list Plenty of other suitable names you can use.您可以使用许多其他合适的名称。

One way is to use a set to keep track of what items you have seen, and only print items that you have seen for the first time:一种方法是使用集合来跟踪您看到的项目,并且只打印您第一次看到的项目:

lst = ["5*3*2","5*2*3","2*3*5","2*5*3"]

seen = set()
for exp in lst:
    calc = eval(exp)
    if calc not in seen:
        print(calc)
        seen.add(calc)

If you are always dealing with simple multiplying expressions with the * operator(no brackets), you could also use functools.reduce and operator.mul instead to multiply the numbers instead of eval here.如果您总是使用*运算符(无括号)处理简单的乘法表达式,您也可以使用functools.reduceoperator.mul来代替此处的eval来将数字相乘。 This will first split the numbers by * , map each number string to an integer, then multiply every element with each other.这将首先将数字除以* ,map 每个数字字符串为 integer,然后将每个元素相乘。

from operator import mul
from functools import reduce

lst = ["5*3*2","5*2*3","2*3*5","2*5*3"]

seen = set()
for exp in lst:
    numbers =  map(int, exp.split("*"))
    calc = reduce(mul, numbers)
    if calc not in seen:
        print(calc)
        seen.add(calc)

Output: Output:

30

With the following list:使用以下列表:

l = ['5*3*2','5*2*3','2*3*5','2*5*3', '2*2']

(Note that list is already something in python so I wouldn't recommend using that as a variable name) (请注意,列表已经在 python 中,所以我不建议使用它作为变量名)

I would first create a list of unique values:我将首先创建一个唯一值列表:

unique_vals = set(map(eval, list))
set([4, 30])

Then for each unique values get the first match in l:然后为每个唯一值获取 l 中的第一个匹配项:

[next(x for x in l if eval(x) ==  i) for i in unique_vals]

I get:我得到:

['2*2', '5*3*2']

Is that what you want?那是你要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM