How can I adjust the list to have some elements positioned on the same index?
Let’s say I have a list like this:
info = ['385395982', '3.1', 'This is dummy text', '3843955682', '2.0', 'This is also dummy text', '248298493', '4.2', 'Another dummy text for you']
As you can see, the list includes this information: timestamp, rating and some text. Each of the element is appended to the list. I would like to be able to access all timestamps at index 0, ratings at index 1, and texts at index 2. How should I modify the list in order to achieve that?
class Scraper(): review_information =  review_date = '' review_rating = '' review_content = '' def __init__(): ... for contribution in review_json.get('contributions',): self.review_date = contribution.get('sortTimestamp', '') self.review_rating = contribution.get('rating', '') self.review_content = contribution.get('text', '') self.review_information.append(self.review_date) self.review_information.append(self.review_rating) self.review_information.append(self.review_content) def parse(self): for info in self.review_information: item['review_date'] = self.review_information item['review_rating'] = self.review_information item['review_content'] = self.review_information
Bottom line, I would love to have all timestamps at index 0 so that in parse method I can access them with
self.review_information (and the same for rating and content).
The desired output of the list should be something like this:
In : info Out: ['385395982'] ['384395562'] ['248298493'] In : info Out: ['3.1'] ['2.0'] ['4.2'] In : info Out: ['This is dummy text'] ['This is also dummy text'] ['Another dummy text for you']
So that when I call the
info in the for loop, I get one timestamp at the time, not all timestamps at once. Does that make sense?
Thanks in advance!
this will let you access all your entities using indices as a list
info = [info[::3], info[1::3], info[2::3]]
In : info Out: ['385395982', '3843955682', '248298493'] In : info Out: ['3.1', '2.0', '4.2'] In : info Out: ['This is dummy text', 'This is also dummy text', 'Another dummy text for you']