NezukoKamado
NezukoKamado

Reputation: 11

Python: Convert list with string sub elements to a normal list

This is for Python. I want to take a list of strings and split each string out into it's own element with the comma as a delimiter. Then turn that into a list of sublists while keeping the first element a string and the next two elements ints. What I have:

 myList = ['one, 0650 ,0730','two, 1100, 1115','three, 1210, 1250','two, 1515, 
 1530','four, 1130, 1430','five, 1750, 1930','four, 2000, 2100','three, 1800, 
 2100']

Desired output:

newList = [['one', 0650, 0730] , ['two', 1100, 1115] , ['three', 1210, 1250] 
,['two', 1515, 1530],['four', 1130, 1430] , ['five', 1750, 1930] , ['four', 
2000, 2100] , ['three', 1800, 2100]]

I know that if you apply list func on each item, it would turn each item which is in string format to a list of strings. The problem is I need sublists with different data types. Sorry I'm sure where to begin with this.

Upvotes: 1

Views: 193

Answers (3)

alex2007v
alex2007v

Reputation: 1300

You can do it like this:

result = []
for item in myList:
    items = [sub_item.strip() for sub_item in item.split(',')]
    items = [
        int(sub_item) if sub_item.isdigit() else sub_item
        for sub_item in items
    ]
    result.append(items)

print(result) # [['one', 650, 730], ['two', 1100, 1115], ['three', 1210, 1250], ['two', 1515, 1530], ['four', 1130, 1430], ['five', 1750, 1930], ['four', 2000, 2100], ['three', 1800, 2100]]

Also you can do the same using regex

import re

pattern = r'(\w+)\s*\,\s*(\d+)\s*\,\s*(\d+)'
for item in myList:
    items = re.findall(pattern, item)[0] # it will be ['one', '650', '730']
    items = [
        int(sub_item) if sub_item.isdigit() else sub_item
        for sub_item in items
    ]
    result.append(items)

In one line if you need this:

import re

pattern = r'(\w+)\s*\,\s*(\d+)\s*\,\s*(\d+)'
result = [[int(item) if item.isdigit() else item for item in re.findall(pattern, items)[0]] for items in myList]

The same without regexp:

result = [[int(item) if item.isdigit() else item for item in items.replace(' ', '').split(',')] for items in myList]

Upvotes: 1

user8314628
user8314628

Reputation: 2042

newList = [e.split(",") for e in myList]
newList = [[int(x) if x.strip().isdigit() else x for x in sublst] for sublst in newList]

Just want to make sure myList = ['one, 0650 ,0730'... is not a typo. There is one space behind 0650 while the others are just followed by a comma,.

If that was a typo, you can skip the strip() step.

Upvotes: 0

Vinod Karantothu
Vinod Karantothu

Reputation: 61

Try this.

myList = [item.split(',') for item in myList]
myList = [[i.strip() for i in item] for item in myList]
myList = [[int(i) if i.isdigit() else i for i in item] for item in myList]
print(myList) #[['one', 650, 730], ['two', 1100, 1115], ['three', 1210, 1250], ['two', 1515, 1530], ['four', 1130, 1430], ['five', 1750, 1930], ['four', 2000, 2100], ['three', 1800, 2100]]

Upvotes: 0

Related Questions