Posts: 42
Threads: 22
Joined: Oct 2016
Jul-23-2017, 10:25 PM
(This post was last modified: Jul-23-2017, 10:25 PM by Mike Ru.)
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.
Posts: 12,032
Threads: 486
Joined: Sep 2016
Jul-23-2017, 10:45 PM
(This post was last modified: Jul-23-2017, 10:46 PM by Larz60+.)
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
Posts: 42
Threads: 22
Joined: Oct 2016
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_)
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jul-25-2017, 07:21 AM
(This post was last modified: Jul-25-2017, 07:22 AM by wavic.)
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']
Posts: 42
Threads: 22
Joined: Oct 2016
(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.
Posts: 8,162
Threads: 160
Joined: Sep 2016
Jul-25-2017, 10:50 AM
(This post was last modified: Jul-25-2017, 10:50 AM by buran.)
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
Posts: 3,458
Threads: 101
Joined: Sep 2016
Jul-26-2017, 04:28 PM
(This post was last modified: Jul-26-2017, 04:46 PM by nilamo.)
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']
Posts: 7,320
Threads: 123
Joined: Sep 2016
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']
|