Python. Create string date getting items at indexes of sublists and replace date by the indexes

Hello,

I am trying to achieve this with Pyhton in Dynamo: Create string date getting items at indexes of sublists and replace date by the indexes.

I share how I do it with OOTB nodes.
createdatestringandreplaceitemsinsublists.dyn (22.2 KB)

1 Like

You’re overthinking it. Your output is just a list of appended items (within a sublist) that already exist as either the item itself or parts of the item. Use the indices you need to create the date then append each of the items in their respective order.

Thanks I am aware of it but I do not know in Python so trying to learn but if I have to wait for it I do not do anything until next year, otherwise I would not ask here and share a simple sample to resolve.

There are lots of courses you can do online.

1 Like

What have you tried so far though? What research have you done? The nodal example is helpful for explaining the problem, but you do have to show some attempt at a (Python) solution. You’ve had other posts with Python so I know you’re learning and making progress (which is fantastic by the way). Learning means doing some research on your own though. What you’re asking for is just a few basic principles strung together. Look into looping through and appending to lists. That will get you started.

4 Likes

hopefully when I learn soon I will be able to resolve questions in this forum more actively, by now I am trying to do something I use to do with nodes in Python because Dynamo eats all memory resources, so I am forced to do it and in a hurry, because I did a massive script in 2 days but now I am more than a month trying to remake it in Python and feeling very frustrated.

I am sure this question like many others I did are quite basic to do in Python but no clue, also receiving many answers a few finally really are working, so huge time already invested trying Python

1 Like

I would suggest… this probably isn’t the best way of doing it… But I wanted to play with lists like you did in Dynamo…

Tbh this seems kinda limiting

1 Like

This is essentially what I had in mind except to replace all the if statements with variable definitions at the given indices. Then just combine into a date and append your variables.

Example Code
dataEnteringNode = IN
data = IN[0]
out = []

for set in data:
    first = set[0]
    year = set[1]
    day = set[2]
    month = set[3]
#   rest of variables here
    last = set[7]
#   create date instead of string
    date = str(month)+"/"+str(day)+"/"+str(year)
    out.append([first,date,last])
OUT = out
1 Like

Asked a mate who’s a programmer and he did it by joining lists… Much shorter.

@Nick_Boyts >>

2 Likes

Exactly.

pfff… Mine still worked :yum:

And that’s all that matters! :hugs:

1 Like

heh! If only that were true!

You’ve identified a knowledge gap in needing to learn Python. THis happens all the time in AEC - in fact it’s why we have specialty engineers. A classic example is that architects know SOME structural engineering, but are not experts. They know they need an expert to ensure the project goes smoothly, so they either hire a structural engineer as a teammate, or bring in a consulting engineer. Similar knowledge gaps are plugged in just about every trade (MEP, Fire protection, building code, life safety, construction sequencing, water proofing and envelope…).

In many ways code should be the same; rather than get frustrated at not knowing the specifics of Python, bring in someone who does to help out. You’ve got a graph which does the work I effectively, hire someone to teach you what you need and/or fill in the knowledge gap.

And I get that not everyone is in a ‘hire someone’ situation (I know I’m not), but you can pass on that input to the principals at your office who are, and give them the options:

  1. We can do this manually
  2. Automatically but it will lock up the CPU for a month straight
  3. Semi-automatically where we process 1/8 of the job at a time (this may be the way to go)
  4. Hire someone to make the code more effective
  5. Pay for me to get some training so I can make this effective and build future tools with efficiency in mind

The mixed combination of 4 and 5 is usually the best way to go, but if you’re in a pinch #3 will usually do the trick right off the bat; Write stuff out to external files, Liist.GetItemAtIndex with a slider that you increment on each run… whatever need be there is a way to reduce scope and still be more effective than #1.

4 Likes

it seems there is something written not accepted, perhaps those symbols { }?

I am impressed of the many options to write code.

in this case I see this is somehow limited because it is supposing always there will be an item at the index 0 that is not a number and also last item as string which is not used to create the date combination of the other indexes.

The only thing I know for sure is the fixed position of the index numbers in the original list that will compose the date, so I do not know necessarily what is at the beggining before and after the index of those numbers and how many items are in total in the sublist.

Also the problem I see with this joining numbers with separators is the format number, so numbers between 0-9 will have only one digit but it needs 2 digits like 00-09, so I though would make more sense to create a real date function, perhaps similar as the OOTB node, then the output will be formatted I suppose :thinking:

well yea… Where’s the rest of it?

I am not familiar with those expressions recursive I think is called, maybe I tyed something wrong or in Dynamo some symbols are different

The first item isn’t a problem then since the date will always be indices 1-6. If the length of the list varies then you’ll have to split the list at the end of the date to give you a remaining sublist of n length.

You can get subsections of a list via the specified indices. A colon (:) allows you to get get the indices from the start or end of a list depending on its location. (Example: You can get the first subsection with list[:7] and the second subsection with list[7:].)

Example Code
dataEnteringNode = IN
data = IN[0]
out = []

for set in data:
    first = set[0]
    year = set[1]
    day = set[2]
    month = set[3]
#   rest of variables here
    rest = set[4:]
#   create date instead of string
    date = str(month)+"/"+str(day)+"/"+str(year)
    
    output = [first,date]
    
    for r in rest:
        output.append(r)
        
    out.append(output)
    
OUT = out

Yes, you should probably create an actual datetime object instead of just a string. I mentioned that in a comment. It was just easier for me to create a placeholder string in my example.

1 Like

@Nick_Boyts many thanks, I achieved the result wanted of combining the numbers in a single item and remove the original numbers of sublists, although the result is first as sublist inside a sublist and the date and rest are inside the sublist as flatten, I would like to get all full sublist flatten without more sublists inside a sublist.

by the moment looks the list like that scheme:

sublist
   sublist
   list

and would like to get:

sublist
   list

I know it is a quite simple stupid question but no clue

the code I used is this:

import datetime

cleanlistwithDate = []

for set in cleanbasiclist:
    first = set[0:6]
    last = set[13:]
    year = set[10]
    day = set[7]
    month = set[8]
    hour = set[9]
    minute = set[11]
    second = set[12]
    date = str(datetime.datetime(year,month,day,hour,minute,second))
    vv=[first,date]
    for r in last:
    	vv.append(r)
    cleanlistwithDate.append(vv)

OUT = cleanlistwithDate