列表未按预期方式覆盖

时间:2019-06-18 20:59:41

标签: python list

我是Python的新手。我的代码应该要做的是对照现有列表检查该行,如果该行尚未在位置文件中,它将要求用户为其分配值并更新该列表验证反对。我已验证列表正确地从文本文件中读取(采用以下格式)。

它给我的结果是一个永无止境的循环,因为即使添加了它也没有在列表中找到该行。我希望在用户输入“ n”然后输入一些字符串值后,它会中断。

(此外,if dummyvar in location:还会有一行,但我还没有提到。)

变量

 from_location = [ company, address, city, state ] #changes line by line
 location = [[ 'company a', 'address a', 'city a', 'state a', 'classification a'],
             [ 'company b', 'address b', 'city b', 'state b', 'classification b']]
 loc_opt = ['classification a', 'classification b', 'classification c']

代码段:

        while from_location not in location[:][0:-1]:
                ftext=("This location has not been indexed. Please enter the " +
                      "location name or input one of these prompts \n" + 
                      " 'l' to list all acceptable locations\n" + 
                      " 'n' to add a new location\n" +
                      " 'p' to print the full line of information\n" +
                      "Company:   " +  str(from_location[0]) + " \n" +
                      "Address:   " + str(from_location[1]) + "\n" +
                      "City:   " + str(from_location[2]) + "\n" +
                      "State:   " + str(from_location[3]) + "\n")

                dummyvar = input(ftext)
                if dummyvar == "l":
                    print("Current Location Options are:  \n ")
                    for item in loc_opt:
                        print("  ", item ,"  ")
                if dummyvar == "p":
                    print("Entire Line :  ", spline,"\n")
                if dummyvar == "n":
                    new_location=input("Please input new location:\n")
                    new_location.upper()
                    new_location = [new_location]
                    loc_opt = loc_opt + new_location
                    location = location + [from_location + new_location]
                    print(location) 

完整代码

print("Press Ctrl C with the Python window selected \n"
      "at any time to stop the program. A loud beep "
      "will sound \n when the program is finished.\n\n")
date = 00000000
counter = 1
dummyvar="q"

ref_fp =(r"filepath")
loc_opt_fn = open(ref_fp + "LOCATION OPTIONS.txt")
loc_ref_fn = open(ref_fp + "LOCATION REFERENCE.txt")

loc_opt = loc_opt_fn.read().split("\n")


location = loc_ref_fn.read().split("\n")

for index in range(len(location)):
    location[index]=location[index].split("\t")

while len(str(date))!= 8 or (date < 20180101 or date > 20301231):
    date = input("What date is the file would you like to clean"
             "( yyyymmdd year like: 20190227). \n\n")
    date = str(date).strip()
    try:
        date = int(date)
    except:
        print("\nNo letters please!\n")
        date = 000

fp1 = (r"anotherfilepath")
fp2 = ( r" filename.txt")

fpr= (r"yetanotherfilepath")
filepath = fp1 + str(date) + fp2

with open(filepath) as source:
    for line in source:
        line.strip("\n")
        spline = line.split("\t")
        if counter == 1:
            spline.append("FROM")
            spline.append("TO")
        else:
            spline.append("")
            spline.append("")
        if spline[0] == "NC":
            #So we've added two columns, FROM and To
            #And we are only operating on the lines that are NC
            #Because that excludes the exceptions.
            #Now we want to check and see if the From and
            #To information is in the reference lists.
            from_location=[spline[5],spline[7],spline[9],spline[10]]
            to_location=[spline[13],spline[15],spline[17],spline[18]]

            while from_location not in location[:][0:-1]:
                    ftext=("This location has not been indexed. Please enter the " +
                          "location name or input one of these prompts \n" + 
                          " 'l' to list all acceptable locations\n" + 
                          " 'n' to add a new location\n" +
                          " 'p' to print the full line of information\n" +
                          "Company:   " +  str(from_location[0]) + " \n" +
                          "Address:   " + str(from_location[1]) + "\n" +
                          "City:   " + str(from_location[2]) + "\n" +
                          "State:   " + str(from_location[3]) + "\n")

                    dummyvar = input(ftext)
                    if dummyvar == "l":
                        print("Current Location Options are:  \n ")
                        for item in loc_opt:
                            print("  ", item ,"  ")
                    if dummyvar == "p":
                        print("Entire Line :  ", spline,"\n")
                    if dummyvar == "n":
                        new_location=input("Please input new location:\n")
                        new_location.upper()
                        new_location = [new_location]
                        loc_opt = loc_opt + new_location
                        location = location + [from_location + new_location]
                        print(location)

    counter += 1 
import winsound
winsound.Beep(600,500)
print("\n\n\n----------------------------Processing is finished.----------------------------\n\n\n") 

1 个答案:

答案 0 :(得分:1)

location[:][0:-1]不会从location的每个元素中删除最后一个元素。等效于temp = location[:],后跟temp[0:-1]temp只是location的副本,然后它返回一个列表,除了最后一个元素外,其他所有元素。

您可以通过列表理解来做您想做的事情:

while from_location not in [l[0:-1] for l in location]: