# How to impose condition on n-tuple?

**URL:** <https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882>\
**Category:** General Usage\
**Tags:** question, tuple, vector\
**Created:** [June 16, 2022, 2:55pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882 "2022-06-16T14:55:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_12](https://avatars.discourse-cdn.com/v4/letter/k/ba8739/32.png) [@Kevin\_12](https://discourse.julialang.org/u/Kevin_12)\
**Post date:** [June 16, 2022, 2:55pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/1 "2022-06-16T14:55:22Z")

</div>

Is it possible to place some condition over the ements of Vector of Tuples to creat another Vector of Tuples?

For example, I have buch of tuples demonstraing some properties of devices (property1, property2, deice\_ID). besides there are two other vectors IDs, and a Super\_property vector showing a special property of a device (alaways length(ID) = length( Super\_P)) . For instance, device with ID `4` is special only if it has the proprty of `25`. Based o these how to crreate a vector that only returns the special devices?

```julia

Devices = [(25, 504, 4), (24, 518, 9) , (20, 250, 7), (29, 248, 4), (29, 283, 4), (22, 528, 9) , (21, 270, 4), (27, 218, 7), (25, 123, 4), (32, 123, 9)]

ID = [4,7,9]
Super_P = [25, 27, 22]

```

This is what I’m looking for:

```julia
Specials = [[ (25, 504, 4), (25, 123, 4)] , (27, 218, 7), (22, 528, 9)]

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [June 16, 2022, 3:09pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/2 "2022-06-16T15:09:43Z")

</div>

Something like this?

```julia
julia> Devices = [(25, 504, 4), (24, 518, 9) , (20, 250, 7), (29, 248, 4), (29, 283, 4), (22, 528, 9) , (21, 270, 4), (27, 218, 7), (28, 123, 4), (32, 123, 9)];
julia> ID = [4,7,9];
julia> Super_P = [25, 27, 22];

julia> function is_special(device)
          prop1, prop2, id = device
          i = findfirst(==(id), ID)
          return prop1 == Super_P[i]
       end
is_special (generic function with 1 method)

julia> filter(is_special, Devices)
3-element Vector{Tuple{Int64, Int64, Int64}}:
 (25, 504, 4)
 (22, 528, 9)
 (27, 218, 7)

```

Here I used `findfirst` to look for the index of `id` in the `ID` array, and then compare `prop1` to the special value stored at the same index in `Super_P`. (If it is not guaranteed that all possible ids are listed in `ID`, then you’d have to handle the special case where you don’t find the given id in `ID`.)

`filter` is then used to only keep the devices matching the criterion in `Devices`.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 16, 2022, 3:33pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/3 "2022-06-16T15:33:56Z")

</div>

This sounds like it would be easier to maintain if you made a struct,

```julia
struct Device
    property_1::Int
    property_2::Int
    id::Int
end

is_special(d::Device, id, property) = d.id == id && d.property_1 == property

devices = [Device(d...) for d in Devices]

special = [filter(devices) do d
    is_special(d, i, p)
end for (i, p) in zip(ID, Super_P)]

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 16, 2022, 5:23pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/4 "2022-06-16T17:23:40Z")

</div>

@ffevotte, is there an easy way to group the tuples that result from your code?

A laborious way:

```julia
is_special(device) = device[1] == Super_P[findfirst(==(device[3]), ID)]

sd = filter(is_special, Devices)
ix = getindex.(sd,3)
specials = [sd[ix .== u] for u in unique(ix)]

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [June 16, 2022, 5:41pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/5 "2022-06-16T17:41:10Z")

</div>

> [@rafael.guerra](#):
>
> @ffevotte, is there an easy way to group the tuples that result from your code?

Ah, it looks like the grouping requirement arrived in an edit to the question, after I posted my answer… @Kevin_12 please don’t do that: it makes it rather hard to follow threads when posts are substantially modified without warning…

> [@rafael.guerra](#):
>
> A laborious way:

LGTM.

An other way, probably as laborious as yours 🙂

```julia
is_special(device) = device[1] == Super_P[findfirst(==(device[3]), ID)]
specials = filter(is_special, Devices)
specials_grouped = [filter(d->d[3]==id, specials) for id in ID]

```

But I do agree with @gustaphe: it’s awkward to manipulate tuples here. Since we know that we are manipulating “devices”, it’s best to declare a specific type and use that.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 16, 2022, 5:46pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/6 "2022-06-16T17:46:24Z")

</div>

```julia
fandl=[(first(D),last(D)) for D in Devices]
Devices[[findfirst(isequal(spid), fandl) for spid in zip(Super_P,ID)]]

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 16, 2022, 6:01pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/7 "2022-06-16T18:01:44Z")

</div>

> [@ffevotte](#):
>
> probably as laborious as yours

Not at all, your comprehension with a filter inside looks pretty good. Thanks.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 16, 2022, 6:33pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/8 "2022-06-16T18:33:15Z")

</div>

I just got back to my computer, and I’m frankly shocked that my phone written solution ran perfectly.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 23, 2022, 3:36pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/9 "2022-06-23T15:36:07Z")

</div>

this should do just what is required

```julia
getindex.([Devices],[findall(isequal(spid), getindex.(Devices, [[1,3]])) for spid in zip(Super_P,ID)])

3-element Vector{Vector{Tuple{Int64, Int64, Int64}}}:
 [(25, 504, 4), (25, 123, 4)]
 [(27, 218, 7)]
 [(22, 528, 9)]

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 23, 2022, 7:51pm UTC](https://discourse.julialang.org/t/how-to-impose-condition-on-n-tuple/82882/10 "2022-06-23T19:51:05Z")

</div>

```julia
filter(t->(t[1],t[3]) in zip(Super_P,ID), Devices)

4-element Vector{Tuple{Int64, Int64, Int64}}:
 (25, 504, 4)
 (22, 528, 9)
 (27, 218, 7)
 (25, 123, 4)

```
