Lisadk
Lisadk

Reputation: 345

How to split the elements of strings in a list (Python)

I am very new to Python, and hope you can help me.

I have a list of strings called reviewerdetails that contains information on reviewers on Hostelworld. In each string, there are three elements: the country, the gender and the agegroup of the reviewer. For example, the first case looks like this:

'\n                Belgium,                Female,                18-24            '

I want to create three separate lists for these three elements, but I am not sure how to select elements within a string within a list? I have tried the .split function, but I get the error

AttributeError: 'list' object has no attribute 'split'. 

I found this question: split elements of a list in python that sort of tries to do want I want to do, but I do not know how to apply the answer to my problem.

Upvotes: 0

Views: 6448

Answers (5)

Peter
Peter

Reputation: 1

I am also a new programmer to Python, so this might be inefficient. The way I would do it would be to have three lists, one for the country, one for the gender, and one for the age range. Then I would do for loops, so if the countries list has [...,'Belgium',...] in it, it would know. So for each list, I would say

for reviewer in reveiwerdetails:
    for country in [country list name]:
        if country in reviewer:
            [list name].append(country)
            break
    for gender in [gender list name]:
        if gender in reviewer:
            [list name].append(gender)
            break
    for agerange in [age range list name]:
        if agerange in reviewer:
            [list name].append(agerange)
            break

So that way you have a list with all the countries of the reviewers, genders, and age ranges in order. Again, this is probably extremely inefficient and there are most likely much easier ways of doing it.

Upvotes: 0

Gil
Gil

Reputation: 569

Hope i understood correctly, I think this is what you are trying to do:

main_list = [
    '\n                Belgium,                Female,                18-24            ',
    '\n                Belgium,                Female,                18-24            '
]
for s in main_list:
    # create a list split by comma
    sub_list = s.split(",")
    # cleanup any whitespace from each list item
    sub_list = [x.strip() for x in sub_list]
    print(sub_list)

Upvotes: 0

DeepSpace
DeepSpace

Reputation: 81604

Unfortunately we can't use assignments in list comprehensions, so this needs to be done in an explicit for loop (if we don't want to call .split and iterate 3 times)

li = ['\n                Belgium,                Female,            18- 24           ',
      '\n                Belgium,                Male,              18-24            ']

li = [elem.split() for elem in li]
print(li)
# [['Belgium,', 'Female,', '18-24'], ['Belgium,', 'Male,', '18-24']]
countries, genders, ages = [], [], []
for elem in li:
    countries.append(elem[0])
    genders.append(elem[1])
    ages.append(elem[2])

print(countries)
print(genders)
print(ages)
# ['Belgium,', 'Belgium,']
# ['Female,', 'Male,']
# ['18-24', '18-24']

Upvotes: 2

lolbert hoolagin
lolbert hoolagin

Reputation: 85

Try using list comprehensions:

output = [input[i].split( do whatever you need to in here ) for i in range(len(input))]

The split function is a member of string, not list, so you need to apply the function to each element in the list, not to the list itself.

Upvotes: 0

Oleg Butuzov
Oleg Butuzov

Reputation: 5395

Something like this, using split and filtering empty strings.

mylist = [x.strip() for x in reviewerdetails.split(" ") if len(x.strip()) > 0];

Upvotes: 1

Related Questions