0

I have a strings in nested lists structure, can someone give me a tip on how to modify the strings in a for loop?

For example, I am trying to delete the last couple characters my string these values: /CLG-MAXFLOW

If I do

example = 'slipstream_internal/slipstream_hq/36/CLG-MAXFLOW'

print(example[0:36])

This is what I am looking for:

'slipstream_internal/slipstream_hq/36'

But how can I apply this to strings inside nested lists?

devices = [['slipstream_internal/slipstream_hq/36/CLG-MAXFLOW'],
            ['slipstream_internal/slipstream_hq/38/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/31/CLG-MAXFLOW'],
            ['slipstream_internal/slipstream_hq/21/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/29/CLG-MAXFLOW'],
            ['slipstream_internal/slipstream_hq/25/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/9/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/6/CLG-MAXFLOW'],
            ['slipstream_internal/slipstream_hq/13/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/14/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/30/CLG-MAXFLOW'],
            ['slipstream_internal/slipstream_hq/19/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/8/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/26/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/24/CLG-MAXFLOW'],
            ['slipstream_internal/slipstream_hq/34/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/11/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/27/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/20/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/23/CLG-MAXFLOW'],
            ['slipstream_internal/slipstream_hq/15/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/37/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/39/CLG-MAXFLOW',
            'slipstream_internal/slipstream_hq/10/CLG-MAXFLOW']]
4
  • You can have a nested for loop and use substring or indexing to delete the last few characters. Commented Jul 2, 2021 at 2:03
  • Something like for device in devices? Any chance you could give me some more tips? Commented Jul 2, 2021 at 2:05
  • What do you mean by "apply"? Do you know how to do anything at all with a nested list? Do you know how to do anything at all with an unnested list? Where exactly is the conceptual difficulty? When you say that you want to "modify" the strings, what exactly does that mean? Could you show an example of exactly what the list should look like after the code runs? Commented Jul 2, 2021 at 2:08
  • @KarlKnechtel Although the question could have been improved, I believe it was 'detailed' enough to understand what the OP wants... Commented Jul 2, 2021 at 2:11

2 Answers 2

1

Not exactly the best solution but worth giving a try:

def check_about(lists:list):
    for i,j in enumerate(lists):
        if isinstance(j,list):
            check_about(j)
        else:
            lists[i]=lists[i].strip('/CLG-MAXFLOW')
    return lists
print(check_about(devices))
Sign up to request clarification or add additional context in comments.

1 Comment

well that seemed to work thank you it keeps my nested list data structure
1

If your output must be the same structure given by devices variable, but with the string changed, then you can do this:

for row in devices:
  for index, string in enumerate(row):
    row[index] = '/'.join(string.split('/')[:-1])

Output:

[['slipstream_internal/slipstream_hq/36'],
 ['slipstream_internal/slipstream_hq/38',
  'slipstream_internal/slipstream_hq/31'],
 ['slipstream_internal/slipstream_hq/21',
  'slipstream_internal/slipstream_hq/29'],
 ['slipstream_internal/slipstream_hq/25',
  'slipstream_internal/slipstream_hq/9',
  'slipstream_internal/slipstream_hq/6'],
 ['slipstream_internal/slipstream_hq/13',
  'slipstream_internal/slipstream_hq/14',
  'slipstream_internal/slipstream_hq/30'],
 ['slipstream_internal/slipstream_hq/19',
  'slipstream_internal/slipstream_hq/8',
  'slipstream_internal/slipstream_hq/26',
  'slipstream_internal/slipstream_hq/24'],
 ['slipstream_internal/slipstream_hq/34',
  'slipstream_internal/slipstream_hq/11',
  'slipstream_internal/slipstream_hq/27',
  'slipstream_internal/slipstream_hq/20',
  'slipstream_internal/slipstream_hq/23'],
 ['slipstream_internal/slipstream_hq/15',
  'slipstream_internal/slipstream_hq/37',
  'slipstream_internal/slipstream_hq/39',
  'slipstream_internal/slipstream_hq/10']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.