Skip to main content
Question

Regroup list elements to generate new lists of 2 pairs


samisnunu
Contributor
Forum|alt.badge.img+10

I have the following list as an example:

Original list:

mylist{0}.attr1 = a1

mylist{0}.attr2 = a2

mylist{0}.attr3 = a3

mylist{1}.attr1 = b1

mylist{1}.attr2 = b2

mylist{1}.attr3 = b3

mylist{2}.attr1 = c1

mylist{2}.attr2 = c2

mylist{2}.attr3 = c3

mylist{3}.attr1 = d1

mylist{3}.attr2 = d2

mylist{3}.attr3 = d3

mylist{4}.attr1 = e1

mylist{4}.attr2 = e2

mylist{4}.attr3 = e3

 

I want to regroup this list elements by recreating multiple lists by grouping the elements into 2 pairs while taking into account the previous element index as follows:

 

New regrouping of elements:

lstGroup1{0}.attr1 = a1

lstGroup1{0}.attr2 = a2

lstGroup1{0}.attr3 = a3

lstGroup1{1}.attr1 = b1

lstGroup1{1}.attr2 = b2

lstGroup1{1}.attr3 = b3

 

lstGroup2{0}.attr1 = b1

lstGroup2{0}.attr2 = b2

lstGroup2{0}.attr3 = b3

lstGroup2{1}.attr1 = c1

lstGroup2{1}.attr2 = c2

lstGroup2{1}.attr3 = c3

 

lstGroup3{0}.attr1 = c1

lstGroup3{0}.attr2 = c2

lstGroup3{0}.attr3 = c3

lstGroup3{1}.attr1 = d1

lstGroup3{1}.attr2 = d2

lstGroup3{1}.attr3 = d3

 

lstGroup4{0}.attr1 = d1

lstGroup4{0}.attr2 = d2

lstGroup4{0}.attr3 = d3

lstGroup4{1}.attr1 = e1

lstGroup4{1}.attr2 = e2

lstGroup4{1}.attr3 = e3

I tried creating a custom looping transformer using a Sampler, but not succeeding, may be a Python code can be easier to do the task..please help.

Note the challenge is the original list length can be varied (dynamic) could be 2, 3, 4, ..50+ etc

4 replies

ebygomm
Influencer
Forum|alt.badge.img+38
  • Influencer
  • April 15, 2019

Do you actually want this output?

lstGroup2{1}.attr1 = b1

lstGroup2{1}.attr2 = b2

lstGroup2{1}.attr3 = b3

lstGroup2{2}.attr1 = c1

lstGroup2{2}.attr2 = c2

lstGroup2{2}.attr3 = c3

 

or this?

lstGroup2{0}.attr1 = b1

lstGroup2{0}.attr2 = b2

lstGroup2{0}.attr3 = b3

lstGroup2{1}.attr1 = c1

lstGroup2{1}.attr2 = c2

lstGroup2{1}.attr3 = c3


samisnunu
Contributor
Forum|alt.badge.img+10
  • Author
  • Contributor
  • April 15, 2019

oh thanks for the hint @egomm..you're right as the index for the new list will start at 0 , I'll edit the output example..


ebygomm
Influencer
Forum|alt.badge.img+38
  • Influencer
  • April 15, 2019

What is the intention afterwards? It's fairly straightforward to build the list you want in python but you would have to type in all the lstGroup names to expose them. 

import fme
import fmeobjects

def processFeature(feature):
        attr1 = feature.getAttribute('mylist{}.attr1')
        attr2 = feature.getAttribute('mylist{}.attr2')
        attr3 = feature.getAttribute('mylist{}.attr3')
        for i, val in enumerate(attr1):
            if i < len(attr1)-1:
                feature.setAttribute('lstGroup'+str(i+1)+'{0}.attr1',attr1[i])
                feature.setAttribute('lstGroup'+str(i+1)+'{1}.attr1',attr1[i+1])
                feature.setAttribute('lstGroup'+str(i+1)+'{0}.attr2',attr2[i])
                feature.setAttribute('lstGroup'+str(i+1)+'{1}.attr2',attr2[i+1])
                feature.setAttribute('lstGroup'+str(i+1)+'{0}.attr3',attr3[i])
                feature.setAttribute('lstGroup'+str(i+1)+'{1}.attr3',attr3[i+1])

 


samisnunu
Contributor
Forum|alt.badge.img+10
  • Author
  • Contributor
  • April 15, 2019
ebygomm wrote:

What is the intention afterwards? It's fairly straightforward to build the list you want in python but you would have to type in all the lstGroup names to expose them. 

import fme
import fmeobjects

def processFeature(feature):
        attr1 = feature.getAttribute('mylist{}.attr1')
        attr2 = feature.getAttribute('mylist{}.attr2')
        attr3 = feature.getAttribute('mylist{}.attr3')
        for i, val in enumerate(attr1):
            if i < len(attr1)-1:
                feature.setAttribute('lstGroup'+str(i+1)+'{0}.attr1',attr1[i])
                feature.setAttribute('lstGroup'+str(i+1)+'{1}.attr1',attr1[i+1])
                feature.setAttribute('lstGroup'+str(i+1)+'{0}.attr2',attr2[i])
                feature.setAttribute('lstGroup'+str(i+1)+'{1}.attr2',attr2[i+1])
                feature.setAttribute('lstGroup'+str(i+1)+'{0}.attr3',attr3[i])
                feature.setAttribute('lstGroup'+str(i+1)+'{1}.attr3',attr3[i+1])

 

many thanks!..the big challenge is I found the original list can be varied in length.. so it could be 4 elements or 50+ in some cases (dynamic list length), so the grouping should accommodate the list length.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings