Python 3:“对于列表中的项目和列表中的项目:”这可能吗?

时间:2018-01-22 15:42:54

标签: python

我有2个列表,并希望根据该列表中的项目数进行for循环,我的代码:

accounts = []
passwords = []

entry1= input('Accounts : ').split()
entry2= input('Passwords : ').split()

accounts.extend(entry1)
passwords.extend(entry2)

我想做什么:

for account in accounts and password in passwords:
    # do something that matchs accounts[n] with passwords[n] each loop  

2 个答案:

答案 0 :(得分:2)

看看zip

https://docs.python.org/3.4/library/functions.html#zip

您可以将其用作

for (account, password) in zip(accounts, passwords):
    do_stuff_with_account_and_password(account, password)

答案 1 :(得分:2)

听起来你想要zip()功能:

accounts = input('Accounts : ').split()
passwords = input('Passwords : ').split()

for account, password in zip(accounts, passwords):
    #do stuff with account and password