0

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.

4
  • 1
    Hint: you should split each element of the list. Commented Mar 24, 2017 at 16:38
  • 1
    for the future, it's a good idea to include some of your source code when you're getting an error, so we can better help you find out what's wrong with your code Commented Mar 24, 2017 at 16:49
  • You may want to read How to Ask and minimal reproducible example... Commented Mar 25, 2017 at 8:31
  • Thank you for your feedback! Commented Mar 27, 2017 at 7:56

5 Answers 5

2

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']
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help! It worked, but I still have a slight problem. Some of the strings consist of more elements, because the 'gender' was a group (sorry, I should have mentioned this before). Here is an example: ['USA,', 'All', 'Female', 'Group,', '18-24']. I tried to correct this with if statements, but the problem then is that 'all', 'female' and 'group' go into the list separately. Is there a way to split the elements so that these elements (for gender) stay together in a string?
To be more clear: the options are 'female', 'male', 'mixed group', 'all female group', and 'all male group'.
1

Something like this, using split and filtering empty strings.

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

Comments

0

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.

Comments

0

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)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.