Aurora Eugene
Aurora Eugene

Reputation: 75

Split List based on range into multiple list :PYTHON

I have a small doubt , and i was not able to solve it , consider i have a list seperated with comma , and the data is dynamic may be 2 data or more than that

example : listVaue = ['Server1,Stop,Server2,START,.....,ServerN,StartN/StopN']

where N is a number

so if i have to split this into something like [[Server1,Stop],[Server2,Start2],....[ServerN,StartN/StopN]]

Is this possible . I wrote a code , but it is not working properly

listVaue = ['Server1,Stop,Server2,START']

c_index = listVaue.index("Stop")
l2 = listVaue[:c_index]

print(l2)

Can anyone help me solve this problem

Upvotes: 0

Views: 367

Answers (3)

Poorna Chandra R
Poorna Chandra R

Reputation: 129

Try this:

listVaue = ['Server1,Command1,9182,Running,START,Server2,Command2,8888,Running,RESTART,ServerN,CommandN,N,Running,restart']
listVaue = listVaue[0].split(',')
a = ['START', 'RESTART', 'STOP', 'BOUNCE']
s = []
l2 = []
for i in listVaue:
    s.append(i)
    if i.upper() in a:
        l2.append(s)
        s = []

Upvotes: 0

Gabriela Valle
Gabriela Valle

Reputation: 31

If you have a list defined as:

listVaue = ['Server1,Stop,Server2,START']

it actually only has one value, which is a string of value 'Server1,Stop,Server2,START'. If you can define a different list, I would suggest trying to do:

listVaue = ['Server1', 'Stop', 'Server2', 'START']

or even a list of tuples, if you would like values to be correspondent:

listVaue = [('Server1', 'Stop'), ('Server2', 'START')]

Now, if you don't have control over the input data (and therefore cannot change the original list), what you can do is to first split all values by comma and then aggregate them 2 by 2.

# Original list
listVaue = ['Server1,Stop,Server2,START']
# Split string into separate values
# Take the first element - from your code it looks 
# like you only have a continuous string
new_list = listVaue[0].split(',')
# If you know that the list length will always be even, you can 
# aggregate them 2 by 2 using the following code:
l2 = [(new_list[i], new_list.pop(i + 1)) for i in range(len(new_list) // 2)]
l2
>>> [('Server1', 'Stop'), ('Server2', 'START')]

What the code does is basically to get elements in position i and the next one as well (i + 1), removing the latter, whilst iterating through half the length of the list. Because you change its size as you iterate, always removing the second item, you'll end up only iterating through the original indexes 0, 2, 4, 6, 8..., whilst also retrieving their counterpart (1, 3, 5, 7, 9...).

Please note that changing the size of a list whilst you iterate is not generally a good practice, but because we are copying the original list, it shouldn't be a big problem for your case.

You can also change my code to whatever suits you, and you're probably better off with a normal for loop instead of a list comprehension:

listVaue = ['Server1,Stop,Server2,START']
new_list = listVaue[0].split(',')
l2 = []
for i in range(len(my_list) // 2):
   # Change square brackets to round ones if you want a tuple (immutable) instead
   l2.append([new_list[i], new_list.pop(i + 1)]
print(l2)

Upvotes: 1

insulanus
insulanus

Reputation: 271

This should work:

listValue = ["server1, stop, server2, start"]
listValue = listValue[0].split(",")
l2 = [[first_item, second_item] for first_item, second_item in zip(listValue[0::2], listValue[1::2])]

Upvotes: 1

Related Questions