![]() |
How can I unite some elems in the list? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How can I unite some elems in the list? (/thread-4104.html) |
How can I unite some elems in the list? - Mike Ru - Jul-23-2017 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. RE: How can I unite some elems in the list? - Larz60+ - Jul-23-2017 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: A better way would be to use a list comprehension
RE: How can I unite some elems in the list? - Mike Ru - Jul-24-2017 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_) RE: How can I unite some elems in the list? - wavic - Jul-25-2017 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'] RE: How can I unite some elems in the list? - Mike Ru - Jul-25-2017 (Jul-25-2017, 07:21 AM)wavic Wrote:But I need output like this: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'] list_ = ['did', 'is not', 'come', 'will', 'kill', 'are not']In short my code above do it. RE: How can I unite some elems in the list? - buran - Jul-25-2017 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 RE: How can I unite some elems in the list? - nilamo - Jul-26-2017 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'] RE: How can I unite some elems in the list? - snippsat - Jul-26-2017 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'] |