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

Question:

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’?

Asked By: Myan Gybril Sykes

||

Answers:

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.

Answered By: circo

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

It’s an example of the Dictionary with fruit.

keys = {0: 'Apple', 1: 'Banana', 2: 'Coconut', 3:'Durian', 4: 'Elderberry', 5: 'Fig', 6: 'Grape', 7: 'Huckleberry', 8: 'Imbe'}

I have to create a program that outputs the value for the key of the dictionary without stop every time a button is pressed. At the same time, I have to be able to select and print only what I want.

Then I have to move the key one by one to output the value of the key, and I have to be able to select and output only the desired key. Suppose there is a button that outputs the next value by adding a key one by one, and a button that subtracts the key one by one and goes back.

And the keys I want to include are 0, 1, 2, 4, 8.

key_to_include = [0, 1, 2, 4, 8] 

key = 0

def Next():
    global key
    key += 1
    print(keys[key_to_include[key]])

def Back():
    global key
    key -= 1
    print(keys[key_to_include[key]])

If you press the button out of range, it will be outside the range allowed by the dictionary. Therefore, the conditional statement of ‘if 1 <= key:’ is used for the back button. It may be as follows.

def Back():
    global key
    if 1 <= key:
        key -= 1
        print(keys[key_to_include[key]])

By the way, what should be the range that outputs the following values? The number of elements you want to include cannot always be five. It may be three, or it may not contain anything.

In this case, len() for exporting the length of the list may be usefully used.

length_of_inclusion_list = len(key_to_include)

def Next():
    global key
    if key < length_of_inclusion_list - 1:
        key += 1
        print(keys[key_to_include[key]])

Implementing this as a direct program is as follows.

import tkinter
from tkinter import *

window = tkinter.Tk()

window.title("WN_Editor")
window.geometry("600x400")
window.resizable(False, False)

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 = 0
        
def UpCount():
    global key
    if key < lenghth_of_IncluList - 1 :
        key += 1
        label.config(text= keys[key_to_include[key]])

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

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

entry = Entry(window) 
entry.pack()

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

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

window.mainloop()
Answered By: Myan Gybril Sykes
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.