Python Forum
How can I unite some elems in the list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I unite some elems in the list?
#1
I have a list.
list_ = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']
I need to unite the elems that stand in front of 'not'. As a result, I need to get ->
list_ = ['did', 'is not', 'come', 'will', 'kill', 'are not']
I realy cannot figure how to do it out.
Reply
#2
here's one way:
list_ = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']

newitem = ''
for n, item in enumerate(list_):
   if item == 'not' and n > 0:
       newitem = newitem + ' ' + list_[n-1]
print(newitem)
results:
Output:
is are
A better way would be to use a list comprehension
Reply
#3
I have resolved it so:
list_ = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']
for x in list_:
   if x == 'not':
       something = list_.index(x) -1
       newelem = list_[one] + ' not'
       list_.pop(one)
       list_.remove(x)
       list_.insert(one, newelem)
print(list_)
Reply
#4
In [1]: list_ = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']

In [2]: res = [" ".join(elements).strip() for elements in " ".join(list_).split("not") if elements]

In [3]: res
Out[3]: ['d i d   i s', 'c o m e   w i l l   k i l l   a r e']
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Jul-25-2017, 07:21 AM)wavic Wrote:
In [1]: list_ = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']

In [2]: res = [" ".join(elements).strip() for elements in " ".join(list_).split("not") if elements]

In [3]: res
Out[3]: ['d i d   i s', 'c o m e   w i l l   k i l l   a r e']
But I need output like this:
list_ = ['did', 'is not', 'come', 'will', 'kill', 'are not']
In short my code above do it.
Reply
#6
lst = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']
def new_element(item):
    return  item[0] if item[1] != 'not' else ' '.join(item)
new_list = [new_element(x) for x in zip(lst[:-1],lst[1:]) if x[0] !='not']

# ALTERNATIVE, NOT VERY PYTHONIC ONE-LINER
new_list2 = [item[0] if item[1] != 'not' else ' '.join(item)
             for item in zip(lst[:-1],lst[1:]) if item[0] !='not']

print new_list
print new_list2
Reply
#7
I like trying things, too!

>>> items = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']
>>> def process(items):
...   collected = []
...   for word in items:
...     if "not" == word:
...       collected[-1] += " " + word
...     else:
...       collected.append(word)
...   return collected
...
>>> process(items[:])
['did', 'is not', 'come', 'will', 'kill', 'are not']
Generators are also neat (ironically, this can't handle being passed a generator):
>>> def process(items):
...   phrase = []
...   while items:
...     if "not" == items[0]:
...       phrase.append(items.pop(0))
...     elif phrase:
...       yield " ".join(phrase)
...       phrase = []
...     else:
...       phrase.append(items.pop(0))
...   if phrase:
...     yield " ".join(phrase)
...
>>> list(process(items[:]))
['did', 'is not', 'come', 'will', 'kill', 'are not']
#edit #2
Here's a generator version, which CAN work with a passed generator. It's ugly, though. Very ugly.
>>> def process(items):
...   # force items to be a generator
...   items = (word for word in items)
...   phrase = []
...   while True:
...     try:
...       word = next(items)
...     except StopIteration:
...       yield " ".join(phrase)
...       break
...     if "not" == word or not phrase:
...       phrase.append(word)
...     else:
...       yield " ".join(phrase)
...       phrase = [word]
...
>>> list(process(items[:]))
['did', 'is not', 'come', 'will', 'kill', 'are not']
Reply
#8
One with regex.
>>> import re
>>> lst = ['did', 'is', 'not', 'come', 'will', 'kill', 'are', 'not']
>>> [''.join(i) for i in re.findall(r'(\w+.not)|(\w+)', ' '.join(lst))]
['did', 'is not', 'come', 'will', 'kill', 'are not']
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020