我正在尝试创建一个包含未指定数量的用户的用户名和密码的列表。我试图让这个列表包含类型" User"的类对象,包含在下面:
import datetime
import sys
class User:
global_count = 0
def __init__(self, username, key):
self.username = username
self.key = key
User.global_count += 1
def showGlobalCount(self):
print "Number of Keys: %d" % global_count
def showKeys(self):
print "username: " + self.username
print "key: " + self.key + '\n'
简单地说,我想转此:
usernames = [John, Tim, Scott]
passwords = [password, password1, password2]
进入这个:
userInfo = [(John, password), (Tim, password1), (Scott, password2)]
以下代码是我目前所拥有的。这将创建一个包含用户名的列表,以及一个包含密码的列表。我想找到一种方法将用户名列表与相应的密码结合起来。
from Credentials import *
def main():
with open("Info.txt", "r") as infile:
data = [line.rstrip().split(",") for line in infile]
usernames, passwords = zip(*data)
usernameList = []
passwordList = []
for user in usernames:
usernameList.append(user)
for key in passwords:
passwordList.append(key)
print usernameList,passwordList
if __name__ == "__main__":
main()
任何人都可以提供的帮助将非常感谢!!
答案 0 :(得分:3)
您可以压缩两个列表,然后创建对象:
users = [User(user, password) for user, password in zip(usernameList, passwordList)]
修改强>
虽然您在阅读数据时解压缩数据,然后想再次压缩数据,但我认为这有点浪费时间,因为数据是您需要的列表列表