So my first list created via a for loop looks like the following
["1variable1", "1variable2", "1variable3", "1variable4", "1variable5"].
I then want it to be appened to new list depending on their position.
so List1 will be [1variable1]
List2 will be [1variable2]
etc
Then the next step of the loop will create another
["2variable1", "2variable2", "2variable3", "2variable4", "2variable5"]
I then want to append that to the previous list to make;
List1 will be [1variable1, 1variable2]
List2 will be [1variable2, 2variable2]
etc.
At the moment I have it so that it simply used square brackets to pull out each part of the list but I don't know how to get this in a loop, maybe next time I run it there will be more than 4 entries and I will miss the 5th.
lst1 = [item[0] for item in Genres]
lst2 = [i[1] if len(i) > 1 else '' for i in Genres]
lst3 = [i[2] if len(i) > 2 else '' for i in Genres]
lst4 = [i[3] if len(i) > 3 else '' for i in Genres]
lst5 = [i[4] if len(i) > 4 else '' for i in Genres]
If the next list doesn't have as many as a previous list then it should fill in a blank space as they all need to be in the same position
The lists are created as follows;
my_list = my_list[1:]
length = len(my_list)
my_list = my_list[0:3]
for film in my_list:
filmIndex = my_list.index(film) + 1
query = film + " imdb"
for j in search(query, tld="co.in", num=10, stop=1, pause=2):
page = requests.get(j)
response = page.status_code
if response == 200:
soup = BeautifulSoup(page.content, "lxml")
genreData = soup.find_all("div",{"class":"subtext"})
Then
for h in genreData:
a = h.find_all('a')
aLength = len(a)
a1 = a[0]
for b in range(0,aLength - 1):
r = a[b].string
genres.append(r)
I then want to add each genre into a seperate column, genre1, genre2 etc. up to how ever many max is. Obviously some of these mull be NULL if the max is 4 and one film only has 1 Then for each film will create a list of all the genres for that film and I want to put them into separate columns
One possible method is to create a list of lists.
Creating a list of lists lets you iterate through the list to insert and place each variable into the list at the variable's index. Of course, if you've got a big list or are doing your first pass, you'll hit an index you haven't encountered yet, so you'll need to init an empty list at that index.
list1 = ["1variable1", "1variable2", "1variable3", "1variable4", "1variable5"]
list2 = ["2variable1", "2variable2", "2variable3", "2variable4", "2variable5"]
biglist = []
#insert list1
#this for loop can be repeated for every list you want to enter - maybe
#put those into a list of lists, too?
for i in range(0, len(list1)):
#check to see if there's a list in this entry
if 0 <= i < len(biglist):
#there's already a list here, add this to it
biglist[i].append(list1[i])
else:
#there's no list here yet, create one and add its first variable
biglist.append([])
biglist[i].append(list1[i])
#repeat for the second list, and so on - you can nest these
for i in range(0, len(list2)):
if 0 <= i < len(biglist):
biglist[i].append(list2[i])
else:
biglist.append([])
biglist[i].append(list2[i])
for l in biglist:
print(l)
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.