Issue
I have a python code as below but I cant pass the variable of inner loop to outer loop. Every time the inner loop breaks, the value of variable "x" reset to initial value.
import pandas as pd
import csv
user_list = pd.read_csv(r'C:\Users\Administrator\Desktop\user_list.csv')
domain_list = pd.read_csv(r'C:\Users\Administrator\Desktop\domainlist.csv')
x=1
y=0
for y in user_list.index:
print(user_list.iloc[y,0])
for x in domain_list.index:
print(domain_list.iloc[x,0])
x=x+1
if(x % 10 == 0):
break
print("out of loop, value of x is "+str(x))
below is my csv files
User_list
user1,pw1
user2,pw2
user3,pw3
Domain_list
burton.com
amazon.com
gizmodo.com
theverge.com
venturebeat.com
digitaltrends.com
mashable.com
theinformation.com
engadget.com
arstechnica.com
techcrunch.com
thenextweb.com
tomshardware.com
roblox.com
discord.com
office.com
tiktok.com
wikipedia.org
baidu.com
samsung.com
bilibili.com
duckduckgo.com
Desired output is as below
After User1 is printed, 1-10 website names are printed and then User2 is printed and 11-20 websites are printed
User1
burton.com
amazon.com
gizmodo.com
theverge.com
venturebeat.com
digitaltrends.com
mashable.com
theinformation.com
engadget.com
arstechnica.com
User2
techcrunch.com
thenextweb.com
tomshardware.com
roblox.com
discord.com
office.com
tiktok.com
wikipedia.org
baidu.com
samsung.com
Current output is as below
user1
burton.com
amazon.com
gizmodo.com
theverge.com
venturebeat.com
digitaltrends.com
mashable.com
theinformation.com
engadget.com
arstechnica.com
user2
burton.com
amazon.com
gizmodo.com
theverge.com
venturebeat.com
digitaltrends.com
mashable.com
theinformation.com
engadget.com
arstechnica.com
user3
burton.com
amazon.com
gizmodo.com
theverge.com
venturebeat.com
digitaltrends.com
mashable.com
theinformation.com
engadget.com
arstechnica.com
out of loop, value of x is 10
Solution
This is the correct behaviour, since once you go back to outer loop again, the inner loop starts from the begging.
You can do something like this, use another variable z
and use it.
z=0
for y in user_list.index:
print(user_list.iloc[y,0])
for x in domain_list.index:
if len(domain_list)==z:
break
print(domain_list.iloc[z,0])
z=z+1
if(z % 10 == 0):
break
Output:
user1
burton.com
amazon.com
gizmodo.com
theverge.com
venturebeat.com
digitaltrends.com
mashable.com
theinformation.com
engadget.com
arstechnica.com
user2
techcrunch.com
thenextweb.com
tomshardware.com
roblox.com
discord.com
office.com
tiktok.com
wikipedia.org
baidu.com
samsung.com
user3
bilibili.com
duckduckgo.com
out of loop, value of x is 22
Answered By - KunduK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.