试图理解词典列表

时间:2014-08-18 20:12:14

标签: python list dictionary

这是我的循环代码

for netid,email,first,last in notification_list:             # put list data in usable format
      suspend_stat_dict['netid'] = netid                        # fill dictionary
      suspend_stat_dict['email'] = email                        # fill dictionary
      suspend_stat_dict['added_day'] = added_day                # fill dictionary
      suspend_stat_dict['deletion_date'] = suspend_day          # fill dictionary
      pername_addy = convert_address(email)                     # make sure email in @uconn.edu form
      for google_data in user_emails:
         if google_data['userEmail'] == pername_addy:
            suspend_stat_dict['suspend_status'] = google_data['isSuspended']    # fill dictionary
            break
      print " each dictionary",suspend_stat_dict
      Suspended_database.append(suspend_stat_dict)              # create list of dictionaries
      print " each list", Suspended_database
      wait = raw_input("PRESS ENTER TO CONTINUE.")

这是输出...... 我本来期望我创建的列表是每个字典项,而不是当前重复的字典项? 我甚至不明白它是怎么做的...... 任何帮助或指导都会很棒!

each dictionary {'added_day': 'August 18, 2014', 'suspend_status': 'false', 'deletion_date': 'September 10, 2014', 'email': 'deanna.tripp@gapps.uconn.edu', 'netid': 'ddt04001'}

each list [{'added_day': 'August 18, 2014', 'suspend_status': 'false', 'deletion_date': 'September 10, 2014', 'email': 'deanna.tripp@gapps.uconn.edu', 'netid': 'ddt04001'}]
PRESS ENTER TO CONTINUE.

each dictionary {'added_day': 'August 18, 2014', 'suspend_status': 'false', 'deletion_date': 'September 10, 2014', 'email': 'alexander.vitruk@gapps.uconn.edu', 'netid': 'alv13010'}

each list [{'added_day': 'August 18, 2014', 'suspend_status': 'false', 'deletion_date': 'September 10, 2014', 'email': 'alexander.vitruk@gapps.uconn.edu', 'netid': 'alv13010'}, {'added_day': 'August 18, 2014', 'suspend_status': 'false', 'deletion_date': 'September 10, 2014', 'email': 'alexander.vitruk@gapps.uconn.edu', 'netid': 'alv13010'}]
PRESS ENTER TO CONTINUE.

each dictionary {'added_day': 'August 18, 2014', 'suspend_status': 'false', 'deletion_date': 'September 10, 2014', 'email': 'simon.barres@gapps.uconn.edu', 'netid': 'sib14004'}

each list [{'added_day': 'August 18, 2014', 'suspend_status': 'false', 'deletion_date': 'September 10, 2014', 'email': 'simon.barres@gapps.uconn.edu', 'netid': 'sib14004'}, {'added_day': 'August 18, 2014', 'suspend_status': 'false', 'deletion_date': 'September 10, 2014', 'email': 'simon.barres@gapps.uconn.edu', 'netid': 'sib14004'}, {'added_day': 'August 18, 2014', 'suspend_status': 'false', 'deletion_date': 'September 10, 2014', 'email': 'simon.barres@gapps.uconn.edu', 'netid': 'sib14004'}]
PRESS ENTER TO CONTINUE.

1 个答案:

答案 0 :(得分:1)

suspend_stat_dict始终是相同的dict,您不断更新。在每次迭代时创建一个新的dict。

for netid,email,first,last in notification_list:
    suspend_stat_dict = {}
    suspend_stat_dict['netid'] = netid
    ...