i have a list object like this
value = [<employee 'Mark Twain' as 'Captain'>,<employee 'Huckle' as 'Cowboy'>]
now i would like to have the employee and in a seperate variable like emplo[0] and as[0] like
emplo[0] = 'Mark Twain'
as[0] = 'Captain'
emplo[1] = 'Huckle'
as[1] = 'Cowboy'
from the value maybe loop through the list and return the value
is there any possible code to split the list value in seperate variables?
It would be better to access the objects' data based on the methods those objects implement or reading whatever their attributes are. But in the absence of other information you could split these up assuming that none of the names or jobs ever contain a '
.
When you print
value = [<employee 'Mark Twain' as 'Captain'>,<employee 'Huckle' as 'Cowboy'>]
that's the repr
result of each object, so you could call split
on that in two list comprehensions:
employees = [repr(obj).split("'")[1] for obj in value]
positions = [repr(obj).split("'")[3][:-1] for obj in value]
repr(value[0])
will produce <employee 'Mark Twain' as 'Captain'>
, so when you call split("'")
on that you get a list like this:
['<employee ', 'Mark Twain', ' as ', 'Captain>']
So calling [1]
on that result gets the employee name, and calling [3]
will get their job, though you'll also need to slice the last character off that string as it has >
, that's why I added [:-1]
.