Value Error: Item not in List when it clearly is

Value Error: Item not in List when it clearly is

我有一个列表,abbreviations,里面装满了字符串对象。我正在尝试调用列表中字符串的 .index。当我用字符串调用 .index 方法时,我得到一个 ValueError: 'LING' is not in list,而它显然在列表中。

我的代码:

for item in abbreviations:
    print item

print abbreviations.index("LING")

为什么清除时'LING'不存在?我添加了以下代码行,它在 'abbreviations' 中搜索字符串的索引。我很困惑—— "LING" 显然在我的缩写列表中。

编辑(附加代码):

import csv 

myfile = open("/Users/it/Desktop/Classbook/classAbrevs.csv", "rU")

lines = [tuple(row) for row in csv.reader(myfile)]

longSubjectNames = [] 
abbreviations = [] 

masterAbrevs = []
for item in lines:
    longSubjectNames.append(item[0])
    abbreviations.append(item[1])

with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:
    masterSchedule = tuple(open("/Users/it/Desktop/Classbook/masterClassList.txt", 'r'))

for masterline in masterSchedule:
    masterline.strip()
    masterSplitLine = masterline.split("|")

    subjectAbrev = ""
    if masterSplitLine[0] != "STATUS":
        subjectAbrev = ''.join([i for i in masterSplitLine[2] if not i.isdigit()])
        masterAbrevs.append(subjectAbrev)

finalAbrevs = []
for subject in masterAbrevs:
    if (subject[-1] == 'W') and (subject[-2:] != 'UW'): 
        subject = subject[:-1]
    finalAbrevs.append(subject)

x = 0

for item in abbreviations:
    print item

print abbreviations.index("LING")

for item in finalAbrevs:
    if masterSplitLine[0] != "STATUS":
        concat = abbreviations.index(str(finalAbrevs[x]).strip())
        print "The abbreviation for " + str(item) + " is: " + longSubjectNames[concat]
    x = x + 1

输出:

masterAbrevs = []
for item in lines:
    longSubjectNames.append(item[0])
    abbreviations.append(item[1])

print '-'.join(abbreviations)

是:

ACA-ACCY-AFST-AMST-ANAT-ANTH-APSC-ARAB-AH-FA-ASTR-BIOC-BISC-BME-BMSC-BIOS-BADM-CHEM-CHIN-CE-CLAS-CCAS-COMM-CSCI-CFA-CNSL-CPED-DNSC-EALL-ECON-EDUC-ECE-EHS-ENGL-EAP-EMSE-ENRP-EPID-EXSC-FILM-FINA-FORS-FREN-GEOG-GEOL-GER-GREK-HCS-HSCI-HLWL-HSML-HEBR-HIST-HOMP-HONR-HDEV-HOL-HSSJ-ISTM-IDIS-IAD-INTD-IAFF-IBUS-ITAL-JAPN-JSTD-KOR-LATN-LAW-LSPA-LING -MGT-MKTG-MBAD-MATH-MAE-MED-MICR-MMED-MSTD-MUS-NSC-ORSC-PSTD-PERS-PHAR-PHIL-PT-PA-PHYS-PMGT-PPSY-PSC-PORT-PSMB-PSYD-PSYC-PUBH-PPPA-REL-SEAS-SMPA-SLAV-SOC-SPAN-SPED-SPHR-STAT-SMPP-SUST-TRDA-TSTD-TURK-UW-WLP-WSTU
Traceback (most recent call last):
  File "/Users/it/Desktop/Classbook/sortClasses.py", line 25, in <module>
    with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as anything:
IOError: [Errno 2] No such file or directory: '/Users/it/Desktop/Classbook/masterClassList.txt'

myfile = open("/Users/it/Desktop/Classbook/classAbrevs.csv", "rU")

lines = [tuple(row) for row in csv.reader(myfile)]

longSubjectNames = [] 
abbreviations = [] 

masterAbrevs = []
for item in lines:
    longSubjectNames.append(item[0])
    abbreviations.append(item[1])

with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:

问题就在这里;

with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:

您在此处定义myfile 之前

myfile = open("/Users/it/Desktop/Classbook/classAbrevs.csv", "rU")

所以实际上 abbreviations = [] 并没有从 classAbrevs.csv 中获取数据。因为它是从 masterClassList.txt 中获取数据,正如您在这一行中定义的 myfile

with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile

这就是为什么你的字符串不在list.Also这一行;

for item in lines:
    longSubjectNames.append(item[0])
    abbreviations.append(item[1])

你确定 item[1] 有你想要的所有字符串吗?

And I tried these codes I just copy-pasted it from your's and here is the result;

问题是,从结果来看你 运行:

"LING\t" 显示在您的列表中,而不是 "LING"

通过 运行 我得到了所需的索引:

abbreviations.index("LING\t")

71

要纠正这个问题,有很多方法可以去除 \t,我展示了其中一种方法:

abbreviations.append(item[1].strip())

通过更正此行,您的 item[1] 将在附加到您的 缩写之前去除 \t 列表。