Using list comprehension to extract all the tweets in the tweets_and_more list and save them in another list called tweets. Print the length of the resulting list. Also, print the first three elements of the new list.
start_tag = "<tweet>"
def extract_tweet(data):
start = data.index("<tweet>")+len(start_tag)
end = data.index("</tweet>")
seq = data[start:end]
return seq
This is my extract tweet function
My list with all the tweets line by line is saved in a list called "tweets_and_more"
I'm having a hard time figuring out how to use List compression to do this. I can do it using a loop I guess but any help with explanation would be great.
The for loop code is placed at the end
tweets = [extract_tweet(data) for data in tweets_and_more]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.