Biplov
Biplov

Reputation: 1142

python iterate over 2 lists then assign longer lists to shorter lists

I have two arrays different length. One array of user information Second array of dictinaries of proxy information. I have 50 differnet proxy and 100 different accounts. They're both an array of dictionaries.

Below is what proxy array looks like:

[
    {
        "username": "", 
        "ip": "", 
        "password": "", 
        "port": "",
        "accounts":[] ## this is where I store 2 accounts.
    }
] 

And below is what user array looks like:

[
    {
        "username": "", 
        "first_name": "", 
        "password": "", 
        "email": "", 
        "proxy": ""
    }
]

MY GOAL: I want to iterate over 50 different proxies then append 2 account to each proxies. So my output would be something like

[
    {
        "username": "", 
        "ip": "", 
        "password": "", 
        "port": "",
        "accounts":[] ## this is where I store 2 accounts.
    }
] 

I want to assign my 50 proxies 2 accounts per proxy.

This is what I've tried so far and miserably failed.

proxyData = proxyData() # 50 proxy list
userData = userData() # 100 user list.
proxy_len = len(userData)


idx = 1
for proxy in proxyData:
    proxy['users'] = userData[idx - 1], userData[idx]
    idx = (idx + 1) % proxy_len
    print idx

The following code produces tons of same proxy for all my users; I want 2 proxy for each user.

The error I'm currently having is; the iteration seems to be wrong and gives me this output.

[
    {
        "username": "", 
        "ip": "", 
        "password": "", 
        "port": "", 
        "accounts": [
            {
                "username": "name8338614", 
                "first_name": "Name", 
                "password": "41asdasdasd", 
                "email": "[email protected]", 
                "proxy": ""
            }, 
            {
                "username": "smith83213334", 
                "first_name": "Smith", 
                "password": "4108605ciplov", 
                "email": "[email protected]", 
                "proxy": ""
            }
        ]
    }, 
    {
        "username": "laurenburn7800", 
        "ip": "104.160.233.44", 
        "password": "f61h66jyu2", 
        "port": "21265", 
        "users": [
            {
                "username": "smith83213334", 
                "first_name": "Smith", 
                "password": "4108605ciplov", 
                "email": "[email protected]", 
                "proxy": ""
            }, 
            {
                "username": "johnson111765968", 
                "first_name": "Johnson", 
                "password": "4108605Diplov", 
                "email": "[email protected]", 
                "proxy": ""
            }
        ]
    }, 
]

You see how smith83213334 user is iterated twice; it shouldn't. Each accounts should only be iterated ONCE. In this case the user johnson111765968 should've been on smith83213334 place instead.

Upvotes: 2

Views: 57

Answers (1)

bashrc
bashrc

Reputation: 4835

Why not store the account as a tuple since you know there would be two values. This can be achieved by zip and range. zip would produce an iterable that would end once the shorter of the two ranges is exhausted.

   for proxy,idx in zip(proxyData, range(0,len(userData), 2)):
       user1, user2 = userData[idx], userData[idx + 1]
       proxy['account'] = user1, user2 

Upvotes: 1

Related Questions