-2

I am trying to get the tweets of a list of users into an object in Python using Tweepy, with the intention of iterating through every tweet and passing it into a text classification algorithm.

The array where the tweets are saved in, is of the form:

tweets = [[tweet.id_str+screen_name, 0, tweet.text.encode("utf-8")] for tweet in alltweets]

So what I need is to iterate through every row of the array and get the string (the tweet text) of the third column.

What is the proper syntax for doing it?

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    This is an array of arrays, not of tuples. It doesn't matter for @BardiHarborow's answer, but perhaps worth pointing out. – unwind Jun 01 '16 at 13:48
  • 2
    In fact it's a list of lists. An [`array`](https://docs.python.org/3/library/array.html) is something different in Python. – Matthias Jun 01 '16 at 14:18

1 Answers1

1

Based on what you provided:

tweet_texts = [tweet[2].decode("utf-8") for tweet in tweets]
Bardi Harborow
  • 1,803
  • 1
  • 28
  • 41
  • Why this does not work? csvtweets = [[tweet.id_str+screen_name, 0, tweet.text.encode("utf-8")] for tweet in alltweets] tweet_texts = [csvtweets[3] for row in csvtweets] print tweet_texts Instead, the output is all the 3 objects that every row/array (as @unwind said) has. Shouldn't the 3 in the brackets identify the column i want to get from every row? – Yanis Kartalis Jun 01 '16 at 14:02
  • @YanisKartalis, Python uses [0-based indexing](http://python-history.blogspot.com.au/2013/10/why-python-uses-0-based-indexing.html), i.e. the first element is actually referred to as the 0th element. – Bardi Harborow Jun 01 '16 at 14:05
  • Thank you @Bardi Harborow ! this solved my issue :) – Yanis Kartalis Jun 01 '16 at 14:08