Home > Enterprise >  Splitting list into a set of strings and keeping each values in individual variable
Splitting list into a set of strings and keeping each values in individual variable

Time:07-25

I have a list as follows:

list = ['a climber stops to take a drink while climbing a snow covered mountain ', 'a man holding a cup on a snow mountain ', 'a man in a yellow suit is holding up a cup while standing in snow ']

Required Output

reference1 = ['a', 'climber', 'tops', 'to', 'take', 'a', 'drink', 'while', 'climbing', 'a', 'snow 'covered', 'mountain']

reference2 = ['a', 'man', 'holding', 'a', 'cup', 'on', 'a', 'snow', 'mountain']

reference3 = ['a', 'man', 'in', 'a', 'yellow', 'suit', 'is', 'holding', 'up', 'a', 'cup', 'while', 'standing', 'in', 'snow']

My Attempt:

list = ['a climber stops to take a drink while climbing a snow covered mountain ', 'a man holding a cup on a snow mountain ', 'a man in a yellow suit is holding up a cup while standing in snow ']
x = (','.join(list))
print(x)
    
print('\n')
    
y = x.split()
print(y)

My Output:

a climber stops to take a drink while climbing a snow covered mountain ,a man holding a cup on a snow mountain ,a man in a yellow suit is holding up a cup while standing in snow 
['a', 'climber', 'stops', 'to', 'take', 'a', 'drink', 'while', 'climbing', 'a', 'snow', 'covered', 'mountain', ',a', 'man', 'holding', 'a', 'cup', 'on', 'a', 'snow', 'mountain', ',a', 'man', 'in', 'a', 'yellow', 'suit', 'is', 'holding', 'up', 'a', 'cup', 'while', 'standing', 'in', 'snow']

Required Output

reference1 = ['a', 'climber', 'tops', 'to', 'take', 'a', 'drink', 'while', 'climbing', 'a', 'snow 'covered', 'mountain']

reference2 = ['a', 'man', 'holding', 'a', 'cup', 'on', 'a', 'snow', 'mountain']

reference3 = ['a', 'man', 'in', 'a', 'yellow', 'suit', 'is', 'holding', 'up', 'a', 'cup', 'while', 'standing', 'in', 'snow']

What correction should I do to get the required output?

CodePudding user response:

Loop through the array and keep pushing into a new one, when it hits mountain push it and break the loop.

CodePudding user response:

You can use a for loop and split each string by spaces

refs = []
for i in lst:
    refs.append(i.split())
ref1, ref2, ref3 = refs

print(ref1)
# ['a', 'climber', 'stops', 'to', 'take', 'a', 'drink', 'while', 'climbing', 'a', 'snow', 'covered', 'mountain']
print(ref2)
#  ['a', 'man', 'holding', 'a', 'cup', 'on', 'a', 'snow', 'mountain']
print(ref3)
# ['a', 'man', 'in', 'a', 'yellow', 'suit', 'is', 'holding', 'up', 'a', 'cup', 'while', 'standing', 'in', 'snow']

In your code, you immediately join the lists together using x = (','.join(list)) which makes things more complicated since you lose your references to where one string ends and the other begins. By splitting each of the 3 separately it makes it much easier to determine which word belongs to which string.

CodePudding user response:

You can use map

reference1, reference2, reference3 = map(str.split, lst)

Output

# reference1 ['a', 'climber', 'stops', 'to', 'take', 'a', 'drink', 'while', 'climbing', 'a', 'snow', 'covered', 'mountain']
# reference2 ['a', 'man', 'holding', 'a', 'cup', 'on', 'a', 'snow', 'mountain']
# reference3 ['a', 'man', 'in', 'a', 'yellow', 'suit', 'is', 'holding', 'up', 'a', 'cup', 'while', 'standing', 'in', 'snow']

CodePudding user response:

You can use list comprehension with split:

lst = ['a climber stops to take a drink while climbing a snow covered mountain ', 'a man holding a cup on a snow mountain ', 'a man in a yellow suit is holding up a cup while standing in snow ']

refs = [sentence.split() for sentence in lst]

print(refs)
# [['a', 'climber', 'stops', 'to', 'take', 'a', 'drink', 'while', 'climbing', 'a', 'snow', 'covered', 'mountain'],
#  ['a', 'man', 'holding', 'a', 'cup', 'on', 'a', 'snow', 'mountain'],
#  ['a', 'man', 'in', 'a', 'yellow', 'suit', 'is', 'holding', 'up', 'a', 'cup', 'while', 'standing', 'in', 'snow']]

Usually, unless there is a good reason, having three different (but similar) variables is not recommended. In many cases, it is better to have a list of lists (in this case refs). Then you can access each list with refs[0], refs[1], and refs[2].

If you really need three seperate variables, you can use generator expression and unpacking:

ref1, ref2, ref3 = (sentence.split() for sentence in lst)

print(ref1)
# ['a', 'climber', 'stops', 'to', 'take', 'a', 'drink', 'while', 'climbing', 'a', 'snow', 'covered', 'mountain']
print(ref2)
# ['a', 'man', 'holding', 'a', 'cup', 'on', 'a', 'snow', 'mountain']
print(ref3)
# ['a', 'man', 'in', 'a', 'yellow', 'suit', 'is', 'holding', 'up', 'a', 'cup', 'while', 'standing', 'in', 'snow']
  • Related