奇怪的python行为和字典列表

2024-10-01 09:40:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字典列表listEntries。当我在IPython中说print listEntries时,我得到以下输出。你知道吗

[{'div': ' ',
  'id': '   EE 760 ',
  'instructor': 'Narayanan H.',
  'instructor_type': 'I ',
  'name': 'Advanced Network Analysis',
  'slot': '3 ',
  'student': '28'},
 {'div': ' ',
  'id': '   EE 764 ',
  'instructor': 'Karandikar Abhay',
  'instructor_type': 'I ',
  'name': 'Wireless & Mobile Communication',
  'slot': '1 ',
  'student': '36'}]

但是当我做a = listEntries[0]然后print a时,我得到的只是{}。你知道吗

更新:当我使用pop方法时,我得到了预期的行为。我通过解析csv文件创建了这个列表。你知道吗

这是密码。你知道吗

import os
import csv

file1 = open('./data.txt', 'r');
csv1 = csv.reader(file1);
listEntry = list();
for row in csv1 :
    # if first row is a number then this row represent course.
    course = {}
    if row[0].isdigit() :
        course['id'] = row[1]
        course['name'] = row[2]
        course['instructor'] = row[3]
        course['instructor_type'] = row[4]
        course['slot'] = row[5]
        course['div'] = row[6]
        course['student'] = row[7]
    else : pass
    listEntry.append(course);

print listEntry[0]

这是文件数据.txt我正在分析。我用的是python2.7

"Running courses for year 2005-2006 and semester 2 "
"   1991-1992  1992-1993  1993-1994  1994-1995  1995-1996  1996-1997  1997-1998  1998-1999  1999-2000  2000-2001  2001-2002  2002-2003  2003-2004  2004-2005  2005-2006  2006-2007  2007-2008  2008-2009  2009-2010  2010-2011  2011-2012  2012-2013    1 - Autumn 2 - Spring 3 - Summer 4 - winter     "
"Instructor Status A=Associate"
"Sr no.","Course Code","Course Name","Instructor Name","Instructor Status","Slot","Division","Enrolled students","Biometric Attendance Enabled?","Registration Limit","Restrictions","Division"
"67","   EE 760 ","Advanced Network Analysis","Narayanan H.","I ","3 "," ","28","-","0","",""
"68","   EE 764 ","Wireless & Mobile Communication","Karandikar Abhay","I ","1 "," ","36","-","0","",""

Tags: csvnamedividtypestudenteerow
2条回答

你发布的内容非常好:

In [1]: L = [{'div': ' ',
  'id': '   EE 760 ',
  'instructor': 'Narayanan H.',
  'instructor_type': 'I ',
  'name': 'Advanced Network Analysis',
  'slot': '3 ',
  'student': '28'},
< {'div': ' ',
  'id': '   EE 764 ',
   ...:   'id': '   EE 760 ',
   ...:   'instructor': 'Narayanan H.',
   ...:   'instructor_type': 'I ',
   ...:   'name': 'Advanced Network Analysis',
   ...:   'slot': '3 ',
   ...:   'student': '28'},
   ...:  {'div': ' ',
   ...:   'id': '   EE 764 ',
   ...:   'instructor': 'Karandikar Abhay',
   ...:   'instructor_type': 'I ',
   ...:   'name': 'Wireless & Mobile Communication',
   ...:   'slot': '1 ',
   ...:   'student': '36'}]

In [5]: print L[0]
{'slot': '3 ', 'name': 'Advanced Network Analysis', 'instructor_type': 'I ', 'student': '28', 'div': ' ', 'instructor': 'Narayanan H.', 'id': '   EE 760 '}

In [6]: print L[1]
{'slot': '1 ', 'name': 'Wireless & Mobile Communication', 'instructor_type': 'I ', 'student': '36', 'div': ' ', 'instructor': 'Karandikar Abhay', 'id': '   EE 764 '}

In [7]: a = L[0]

In [8]: print a
{'slot': '3 ', 'name': 'Advanced Network Analysis', 'instructor_type': 'I ', 'student': '28', 'div': ' ', 'instructor': 'Narayanan H.', 'id': '   EE 760 '}

这是个愚蠢的错误。显然,txt文件的前几行创建了空字典,并将它们推送到listEntries。你知道吗

for i in listEntries :
   if i :
       print i

修复了问题。你知道吗

相关问题 更多 >