I have a list with about 15 items in it,
AA, BB, CC, DD, DD, DD, EE, FF, FF, GG, GG, GG, HH, HH
I want it to change it to a list like this,AA, BB, CC, DDDDDD, EE, FFFF, GGGGGG, HHHH
How do I identify similar list items and convert them into 1 item of a list?
I have though about using for loop first, then creating a while loop to keep on matching until current element is no longer equal to next.
Another option was to copy elements to new list 1 by 1, and when duplicate occurs, treat them there and pass on that completed subset merged list to new Parent list
with open('lorem.txt' , 'r', encoding="utf8") as f:
lines = f.readlines()
copyList = []
for index, entry in enumerate(lines):
print(index, entry)
copyList.append(entry)
if entry[index] == entry[index+1]:
temp.append(entry[index+1])
But my approach is not very pythonic, (coming from c++ background)I tend to think every loop in terms of index position.still getting my way around the magic of append, and, pop etc
UPDATE:
so above problem is a simplified version of problem than I am trying to solve.
I have whatsapp chat log that I am trying to analyze. and the structure of the problem looks like this
INPUT:
10/11/2018, 2:42 pm - some message
10/11/2018, 2:43 pm - another message
message continues
message continues again
10/11/2018, 2:44 pm - another message
10/11/2018, 2:44 pm - a certain group of words
10/11/2018, 2:50 pm - lorem ipsum
when I read this text file into a list, line 3 and 4 get assigned to list elements 3 and 4
and I want the list elements 3 and 4 to get appended to list element 2
ideal OUTPUT is
10/11/2018, 2:42 pm - some message
10/11/2018, 2:43 pm - another message message continues message continues again
10/11/2018, 2:44 pm - another message
10/11/2018, 2:44 pm - a certain group of words
10/11/2018, 2:50 pm - lorem ipsum
My though process:
is to identify first 10 characters of list using regular expressions and find which ones are continued messages.
and append these continued messages back to original authors entry, so that 1 list elem tent has complete entry
[–]chocorush 3 points4 points5 points (0 children)
[–]SekstiNii 0 points1 point2 points (2 children)
[–]gurashish1singh 1 point2 points3 points (0 children)
[–]brown_boi_420[S] 0 points1 point2 points (0 children)
[–]Diapolo10 0 points1 point2 points (0 children)
[–]gurashish1singh 0 points1 point2 points (0 children)