Home > OS >  How to skip the key in Dictionary by the list of exclusion key
How to skip the key in Dictionary by the list of exclusion key

Time:01-13

I'm making a Wiktionary editor, which works with keyboard key and hotkey with convenience. It changes the label by pushing key. enter image description here You can exclude the property, which is etymology, similar term and Descendants so on, by activate the check boxes. But before implementing the check box exclusion function, I made the Tkinter UI button by using the dictionary in python, as I thought it better to understand the mechanism.

How to skip the key in the Dictionary by exclusion the key list?

I don't think of any idea or even similar principle.

import tkinter
from tkinter import *

window = tkinter.Tk()

Key = 1

dictionary = {1: 'term', 2: 'language', 3: 'part of speech', 4:'etymology', 5: 'meaning', 6: 'example', 7: 'notice', 8: 'similar', 9: 'Descendance'}

exrow = {1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1}

def CountUp():
    global Key
    if Key < 10 and exrow[Key] == 1:
        Key  = 1
        label.config(text=dictionary[Key])

def CountDown():
    global Key
    if 1 < Key and exrow[IndexCount] == 1:
        Key -= 1
        label.config(text=dictionary[Key])

label = tkinter.Label(window, text=dictionary[Key])

UpButton = tkinter.Button(window, text="Next", command=CountUp)
UpButton.pack()

DownButton = tkinter.Button(window, text="Back", command=CountDown)
DownButton.pack()

Thank you for reading it though of my clumsy English

What do I should to exclude the key of dictionary when I use variable 'Key'?

CodePudding user response:

If you simply want to exclude a key, you can use the != operator.

dict = {
    'key1': 'value1',
    'key2': 'value2',
    'key3': 'value3'
}

key_to_exclude = 'value'

for key in dict:
    if key != key_to_exclude:
        #do stuff

or if you have a list of keys you want to exclude:

keys_to_exclude = ['value1','value2']
for key in dict:
    if key not in keys_to_exclude:
        #do stuff

You'll probably want to extend your if statement to include the operator.

CodePudding user response:

I'm the user who has asked the question, I found the answer to my question. I left the solution and I suppose to be there for someone who searches for the solution.

keys = {0: 'term', 1: 'language', 2: 'speech part', 3:'etymology', 4: 'meaning', 5: 'example', 6: 'concern', 7: 'similar', 8: 'Descendance'}

key_to_include = [0, 1, 2, 4] 

lenghth_of_IncluList = len(key_to_include)

key_IncluList = []
for n in range(0, lenghth_of_IncluList):
    key_IncluList.append(key_to_include[n])

key = 0
    
def UpCount():
    global key
    if key < lenghth_of_IncluList - 1 :
        key  = 1
        label.config(text= keys[key_IncluList[key]])

def DownCount():
    global key
    if 1 <= key:
        key -= 1
        label.config(text= keys[key_IncluList[key]])

label = tkinter.Label(window, text = keys[key])
  • Related