Hi guys,
I have a list which I have obtained from a python script. the content of the list goes something like

d.complex.1
24
25
67
123
764
d.complex.2
23
54
35
64
d.complex.3
.
.
.

I want to split this list such that, a new list is created from d.complex.1 to d.complex.2 (excluding d.complex.2) i.e.:

d.complex.1
24
25
67
123
764

The above in one list.
Can anyone help me out?
Cheers,
Chavanak

Recommended Answers

All 3 Replies

Can you search for the d.complex numbers? If you can, you can get a slice of the list.

# Let 'L' be your list.
newL = L[ L.index(d.complex.1) : L.index(d.complex.2) ]

Can you search for the d.complex numbers? If you can, you can get a slice of the list.

# Let 'L' be your list.
newL = L[ L.index(d.complex.1) : L.index(d.complex.2) ]

Hi,
Thanks for the suggestion but the list contains 2000 d.complex.* in no regular order :(. So I cannot splice it that way.
Cheers,
Chavanak

You can create a list of lists this way ...

# assume that raw data is read/obtained as a string
data_str = """\
d.complex.1
24
25
67
123
764
d.complex.2
23
54
35
64
d.complex.3
1
2
3
"""

data_list = []
# split at the 'd' char, later put 'd' back in
for data in data_str.split('d'):
    # exclude empty data
    if data:
        temp_list = []
        # split at the newline char
        for item in data.split('\n'):
            # exclude any empty item
            if item:
                if item.startswith('.'):
                    # put the 'd' back in
                    item = 'd' + item
                temp_list.append(item)
        data_list.append(temp_list)

print( data_list )

"""my prettied up result -->
[
['d.complex.1', '24', '25', '67', '123', '764'],
['d.complex.2', '23', '54', '35', '64'], 
['d.complex.3', '1', '2', '3']
]
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.