0

I've got a string list to iterate through with each line consisting 'is connected to' or 'likes to play' as a marker, it will only have one of them in each item of the list. So I used a for loop to iterate the list, and inside are the if statement to decide which phrase is in the item. The string list is something like this:

lines = ['a is connected to b','a likes to play guitar, trampolines','b is connected to m,z', 'b likes to play piano','c is connected to s','c likes to play video games, ball games']

def create(string_input):
    network = {}
    lines = string_input.split('.')
    for line in lines:
        user = {}
        if len(line)>0:
            name = line[0:line.index(' ')]
            if 'is connected to' in line:
                friends = line[line.index('is connected to')+len('is connected to')+1:].split(",")
                user['friends']= friends
            elif 'likes to play' in line: 
                likes = line[line.index('likes to play')+ len('likes to play')+1:].split(",")
                user['likes']= likes
            network[name] = user
    return network
print create(lines)

However, the result of print doesn't give me both user['friends'] and user['likes'] The end result only contains data in the second if block, and all if 'is connected to' data were replaced with [] and lost. I'm not sure if the data structure or the if statement was handled incorrectly. Anyone can help me to see what's wrong here? Thanks in advance.

Edit

As David pointed out, the problem in the code lies in with updating the dictionary instead of the for and if statements. Thanks y'all!!

5
  • 1
    Where do you actually call the create function? Commented Jun 28, 2017 at 2:17
  • Hi Evan, it's called : print create(lines) Commented Jun 28, 2017 at 2:18
  • 1
    That isn't anywhere in your code pasted above. You should add all relevant details. Commented Jun 28, 2017 at 2:18
  • corrected, thanks! Commented Jun 28, 2017 at 2:25
  • I'm not sure if you're using the split() function correctly. That will take a string and split it into an array, but you're passing in lines, which is already an array. Try removing lines = string_input.split('.'), since you already have a formatted array. Commented Jun 28, 2017 at 2:29

3 Answers 3

1

Try this:

lines = ['a is connected to b','a likes to play guitar, trampolines','b is connected to m,z', 'b likes to play piano','c is connected to s','c likes to play video games, ball games']

def create(string_input):
    network = {}
    lines = string_input.split('.')
    for line in lines:
        user = {}
        if len(line)>0:
            name = line[0:line.index(' ')]
            if 'is connected to' in line:
                friends = line[line.index('is connected to')+len('is connected to')+1:].split(",")
                user['friends']= friends
            elif 'likes to play' in line: 
                likes = line[line.index('likes to play')+ len('likes to play')+1:].split(",")
                user['likes']= likes
            network[name] = user
    return network

for line in lines:
    print(create(line))

Your code is good, you just need to pass in the strings in your lines list item by item. There are other ways to do this, but this is a simple one.

Hope it helps!

Edit

I I put the iteration in a main function because I could hear my CS prof scream in agony when I submitted by original answer.

def create(string_input):
    network = {}
    lines = string_input.split('.')
    for line in lines:
        user = {}
        if len(line)>0:
            name = line[0:line.index(' ')]
            if 'is connected to' in line:
                friends = line[line.index('is connected to')+len('is connected to')+1:].split(",")
                user['friends']= friends
            elif 'likes to play' in line: 
                likes = line[line.index('likes to play')+ len('likes to play')+1:].split(",")
                user['likes']= likes
            network[name] = user
    return network

def main():
    originalStrings = ['a is connected to b','a likes to play guitar, trampolines','b is connected to m,z', 'b likes to play piano','c is connected to s','c likes to play video games, ball games']
    for string in originalStrings:
        print(create(string))
main()
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thank you for the answer. Actually, I need to have the data of network correct. The print statement is only for testing.
1

The line network[name] = user is the problem. Look at the input lines 'a is connected to b', 'a likes to play guitar, trampolines'. When you process the first line, you empty the user variable and write the connection to it, then set network[name] to be equal to user. Then when processing the second line, you again empty the user variable and write the likes to it, then set network[name] to be equal to (the new) user, clobbering whatever previously existed in network[name]. So you only end up with the last line for each user being kept.

To fix this, replace network[name] = user with:

if name in network:
    network[name].update(user)
else:
    network[name] = user

1 Comment

Hi David, thank you so much for for pointing it out. It indeed is the reason!
1

You replace the dict user every time in the loop,and the dict lose the imformation before.Try this:

def create(string_input):
    network = {}
    lines = string_input.split('.')
    for line in lines:
        if len(line)>0:
            name = line[0:line.index(' ')]
            if name not in network:
                network[name] = {}
            if 'is connected to' in line:
                friends = line[line.index('is connected to')+len('is connected to')+1:].split(",")
                network[name]['friends']= friends
            elif 'likes to play' in line: 
                likes = line[line.index('likes to play')+ len('likes to play')+1:].split(",")
                network[name]['likes']= likes

    return network

1 Comment

Thank you Yi for the answer! Appreciated!

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.