简体   繁体   English

Python中的列表/元组上的算术运算符?

[英]Arithmetic operators on lists/tuples in Python?

I have a couple functions like this: 我有几个这样的功能:

object obj.getChild(childIndex)
int obj.numChildren()

So I am using these to create this function: 所以我用这些来创建这个功能:

collection obj.getChildren()

I am flexible in the return type, but I will doing a lot of "subtraction", "multiplication" using another list. 我在返回类型上很灵活,但是我会使用另一个列表来做很多“减法”,“乘法”。 So something like this: 所以像这样:

children = obj.getChildren()
children = [5,10,15,20,25]

globalChildren = [1,2,3,4,5,6,7,8,9,10,12,14,16,18,20]

difference = children - globalChildren
difference = [15,25]

shared = children * globalChildren
shared = [5,10,20]

Is there a fast and elegant way to do these or do I have to go through each element one by one and gather the elements manually? 是否有一种快速而优雅的方法来执行这些操作,还是我必须逐一遍历每个元素并手动收集这些元素?

You're looking for set s 您正在寻找set

children = {5,10,15,20,25}

globalChildren = {1,2,3,4,5,6,7,8,9,10,12,14,16,18,20}

difference = children - globalChildren
shared = children & globalChildren

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

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