Reputation: 99
if I have a list that looks like this:
['📌📌📌📌📌📌', 'today', 'happy', '📌📌📌']
and I want to split only the emoji icons, not text :
['📌', '📌', '📌', '📌', '📌', '📌', 'today', 'happy', '📌', '📌', '📌']
I try these code but it didn't return what I want:
!pip install emoji
import emoji
emoji_list = list(emoji.UNICODE_EMOJI)
def split_emoji(input):
result = []
str_i = str(input)
for i in str_i:
if i in [*emoji_list]:
result.append(i)
else:
result.append(i)
return result
and the result:
[['📌', '📌', '📌', '📌', '📌', '📌'],
['t', 'o', 'd', 'a', 'y'],
['h', 'a', 'p', 'p', 'y'],
['📌', '📌', '📌']]
The icons are split perfectly, but I want to keep the text.
What should I do?
Upvotes: 2
Views: 252
Reputation: 11929
You could convert strings with emojis in lists and put the words in a list according to the result of str.isalnum()
>>> l = ['📌📌📌📌📌📌', 'today', 'happy', '📌📌📌']
>>> res = list(itertools.chain(*[list(el) if not el.isalnum() else [el] for el in l]))
>>> res
['📌', '📌', '📌', '📌', '📌', '📌', 'today', 'happy', '📌', '📌', '📌']
or also
>>> res = []
>>> for el in l:
... if el.isalnum():
... res.append(el)
... else:
... res+=list(el)
...
>>> res
['📌', '📌', '📌', '📌', '📌', '📌', 'today', 'happy', '📌', '📌', '📌']
Upvotes: 3
Reputation: 4315
Using extra for-loop to iterate list element of characters and validate char has emoji if yes then set is_emoji flag true and append emoji into list.
Ex.
import emoji
emoji_list = list(emoji.UNICODE_EMOJI)
myList = ['📌📌📌📌📌📌', 'today', 'happy', '📌📌📌']
def split_emoji(textArray):
result = []
for word in textArray:
is_emoji = False
# iterate element of characters and check char is emoji if yes
# then append into result list and set is_emogi flag true.
for char in word:
if char in [*emoji_list]:
result.append(char)
is_emoji = True
# check is_emoji flag is false then append string into list.
if not is_emoji:
result.append(word)
return result
print(split_emoji(myList))
O/P:
['📌', '📌', '📌', '📌', '📌', '📌', 'today', 'happy', '📌', '📌', '📌']
Upvotes: 0