Python:[...]位于列表末尾

时间:2018-11-23 02:57:55

标签: python list dictionary

将列表附加到字典后,列表末尾出现一个不需要的[...]

这是我的代码:

class Account:

    accountInfo = {} #ex. ID : 5FE19C (hexadecimal ID's)

    def __init__(self):
        choice = raw_input("Would you like to login or signup?\n")
        if choice.lower() == "login":
            self.login()

        elif choice.lower() == "signup":
            print "Great! Fill in the following."
            self.signup()

        else:
            self.__init__()

    def signup(self):

        accountID = '%010x' % random.randrange(16**10) # 10 digit hexadecimal ID generator
        personalInfo = []

        self.accountInfo[accountID] = personalInfo

        firstName = raw_input("First Name: ")
        lastName = raw_input("Last Name: ")
        email = raw_input("E-Mail: ")
        password = raw_input("Password: ")
        birthdate = raw_input("DOB (DD/MM/YYYY): ")
        alias = raw_input("Username/Alias: ")

        personalInfo.append(firstName)
        personalInfo.append(lastName)
        personalInfo.append(email)
        personalInfo.append(password)
        personalInfo.append(birthdate)
        personalInfo.append(alias)

        self.accountInfo[accountID].append(personalInfo)

        print self.accountInfo

这是我的输出:

>>> {'a92ab2fcea': ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias', [...]]}

只是想知道为什么它出现以及如何删除它。

谢谢!

2 个答案:

答案 0 :(得分:0)

我想你是说

self.accountInfo[accountID].append(personalInfo)

成为

self.accountInfo[accountID] = personalInfo

您也不需要代码前面的后一行。

答案 1 :(得分:0)

要了解到底发生了什么,您需要了解参考。

>>> a = []
>>> test['a'] = a
>>> a.append(1)
>>> a.append(2)
>>> test['a'] 
[1, 2]

self.accountInfo[accountID] = personalInfo时 它实际上所做的是将personalInfo的引用传递给self.accountInfo[accountID]

意味着修改personalInfo也会反映在self.accountInfo[accountID]

self.accountInfo[accountID] = personalInfo >> {'a92ab2fcea': []}

personalInfo.append(firstName) >> {'a92ab2fcea': ['firstName']} # Note: Since personalInfo changed, so self.accountInfo[accountID] also changed.

personalInfo.append(lastName) >> {'a92ab2fcea': ['firstName','lastName']}

personalInfo.append(email) >> {'a92ab2fcea': ['firstName','lastName','email']}

personalInfo.append(password) >> {'a92ab2fcea': ['firstName','lastName','email','password']}

personalInfo.append(birthdate) >> {'a92ab2fcea': ['firstName','lastName','email','password','birthdate']}

personalInfo.append(alias) >> {'a92ab2fcea': ['firstName','lastName','email','password','birthdate','alias']}

self.accountInfo[accountID].append(personalInfo) >> {'a92ab2fcea': ['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias', [...]]} # What you are doing is appending personalInfo to personalInfo

我想你打算做的是:

  1. 删除self.accountInfo[accountID] = personalInfo
  2. 替换self.accountInfo[accountID].append(personalInfo) with self.accountInfo[accountID] = personalInfo

但是,如果您了解参考概念,

  1. 保持self.accountInfo[accountID] = personalInfo
  2. 删除self.accountInfo[accountID].append(personalInfo) 应该也可以!

但是不建议使用后一种方法,因为它的可读性较差。