Combine List to single text - operation

Dear Experts,
Is it possible to do task explained at photo below (by python or dynamo):

@kvusal ,

what is the purpose for doing that?

OUT = str(IN[0])
1 Like

Good morning,
a node version

Cordially
christian.stan

1 Like

I have one mechanical task. That is why trying to do this task. However, no achieved by your recommendation.

Dear Christian, thanks for recomendation. But what you did is not same as we requested:
AAA AND BBB AND CCC
We need:
AAA, BBB AND CCC

Thanks anyways.

1 Like

Try looking at the total number of objects being combined into a string. If it’s more than 3 include “AND”. You can do this by replacing the final substring or just including an extra condition in a python node.

1 Like

Voici une version Python (peut être améliorer tres certainement)

Python Script:

# Charger les bibliothèques DesignScript et Standard Python
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Les entrées effectuées dans ce noeud sont stockées sous forme de liste dans les variables IN.
_list=IN[0]
# Placer votre code au-dessous de cette ligne
cont=[]
for i in (IN[0]):
    if len(i)==1:
        cont.append([i[0]])
    if len(i)==2:
        cont.append([i[0]+" AND "+i[1]])
    if len(i)==3:
        cont.append([i[0]+", "+i[1]+" AND "+i[2]])           
# Affectez la sortie Ă  la variable OUT.
OUT = cont

cordialement
christian.stan

2 Likes

If you want something scalable you can add conditional checks for the specific conditions you need met.

1 Like

An alternative

import sys
out = []

first_sep = ", "
second_sep = " and "

for lst in IN[0]:
    s = first_sep.join(lst)
    s = s[::-1].replace(first_sep[::-1], second_sep[::-1] , 1)[::-1]
    out.append(s)
    
OUT = out
3 Likes

Thank you gentlemen for the scalable and elegant codes :+1:

cordially
christian.stan
nag mode in python

1 Like