从python3中的列表中删除元素

时间:2016-11-06 22:06:35

标签: python list

我无法开始使用此代码,希望能得到一些帮助。我有一个名为STATE_LIST的列表,其中包含美国所有州的缩写。我需要做的是在一个窗口中打印此列表,当用户在文本框中输入STATE_LIST中的一个缩写时,我需要从中删除该缩写窗口中打印的列表。到目前为止,这是我的整个代码,希望你们能够理解它。谢谢。

from proj2_support_code import *

def verify_input(x,y):
    for i in range (len(x)):
        if x[i] == y.upper():
            return True



def clicked_trump(x_point,y_point):


    trump_top_left_x = WINDOW_WIDTH * .25 - 50
    trump_top_left_y = WINDOW_HEIGHT * 0
    width = 100
    height = 100
    trump_top_left = Point(trump_top_left_x, trump_top_left_y)
    trump_bottom_right = Point(trump_top_left_x + width, trump_top_left_y + height)


    if (trump_top_left.getX() <= x_point <= trump_bottom_right.getX() and trump_top_left.getY() <= y_point <=
       trump_bottom_right.getY()):
        return True

def clicked_clinton(x_point, y_point):

    clinton_top_left_x = WINDOW_WIDTH * .75 - 50
    clinton_top_left_y = WINDOW_HEIGHT * 0
    width = 100
    height = 100
    clinton_top_left = Point(clinton_top_left_x, clinton_top_left_y)
    clinton_bottom_right= Point(clinton_top_left_x + width,clinton_top_left_y + height )

    if (clinton_top_left.getX() <= x_point <= clinton_bottom_right.getX() and clinton_top_left.getY() <= y_point <=
       clinton_bottom_right.getY()):
        return True


def main():

    win= GraphWin('Project_2',WINDOW_WIDTH, WINDOW_HEIGHT)

    clin_in_v = 0
    tru_in_v = 0



    trump_img = Image(Point(WINDOW_WIDTH*.25,WINDOW_HEIGHT/6 - 50),"trump.gif")  # Filenames of candidate pics
    trump_img.draw(win)

    clinton_img = Image(Point(WINDOW_WIDTH * .75,WINDOW_HEIGHT/6 -50),"clinton.gif")
    clinton_img.draw(win)

    textbox_trump = Text(Point(WINDOW_WIDTH * .245, WINDOW_HEIGHT/6 +10), "VOTES: " + str(clin_in_v))
    textbox_trump.draw(win)

    textbox_clinton = Text(Point(WINDOW_WIDTH * .75, WINDOW_HEIGHT/6 +10), "VOTES: " + str(tru_in_v))
    textbox_clinton.draw(win)


    inst_textbox = Text(Point(WINDOW_WIDTH * .5, WINDOW_HEIGHT - USER_INSTRUCTIONS_HEIGHT), "Enter a state abbreviation, then click a picture.")
    inst_textbox.draw(win)
    entrybox = Entry(Point(WINDOW_WIDTH * .5,WINDOW_HEIGHT - ENTRYBOX_HEIGHT), 10)
    entrybox.draw(win)

    STATE_LISTbox= Text(Point(WINDOW_WIDTH * .5, WINDOW_HEIGHT *.5),'States Remaining:' + str(STATE_LIST))
    STATE_LISTbox.draw(win)

    for i in range(len(STATE_LIST)):
        STATE_LISTB = print(STATE_LIST[i])

    STATE_LISTB.draw(win)

    clin_in_v1=0
    tru_in_v1 =0
    for i in range(50):
        c_point = win.getMouse()
        x_c_point = c_point.getX()
        y_c_point = c_point.getY()

        if clicked_trump(x_c_point,y_c_point) == True:
            enteredtext = entrybox.getText().upper()
            x= verify_input(STATE_LIST,enteredtext)
            if x == True:
                tru_in_v1 = STATE_VOTES[enteredtext] + tru_in_v
            else:
                print('Incorrect State Entered:',enteredtext)

            tru_in_v = tru_in_v1

            textbox_trump.setText("VOTES: " + str(tru_in_v))




        elif clicked_clinton(x_c_point, y_c_point) == True:
            enteredtext = entrybox.getText().upper()
            x = verify_input(STATE_LIST,enteredtext)
            if x == True:
                clin_in_v1 = STATE_VOTES[enteredtext] + clin_in_v
            else:
                 print('Incorrect State Entered:', enteredtext)

            clin_in_v = clin_in_v1

            textbox_clinton.setText("VOTES: " + str(clin_in_v))



    win.getMouse()
    win.close()

main()

因此,用户在条目框中输入州名缩写,然后点击总统候选人的图像。然后将投票添加到候选人的总投票数中。我需要在图形窗口中放置一个列表,列出所有状态缩写,并且当用户在条目框中输入州缩写并单击候选项时,需要从状态列表中删除该缩写。

1 个答案:

答案 0 :(得分:0)

当从列表中删除一个从用户输入的索引时,我认为它可以被认为是以下脚本。

list = ["AB", "BC", "CD", "DE", "EF"]
idx = list.index("BC")
list.pop(idx)
print(list)

在第2行,输入了一个单词。 在第3行,列表中的单词将被删除。

如果您不想破坏清单,第3行将如下所示。

list[idx] = ""

这是暗示吗?