简体   繁体   中英

how to store dictionary items in a list

This question is sorta hard to make into a one lined question so im going to have to explain. Im trying to check a given string to see if the contents in the string has whats inside the dictionary, if that situation is true for every letter in the dictionary store the keys in the list "Notes". But how do you store it in the list and how do I return it because I keep getting nothing in return heres what i have:

def text2notes (s):
    s = s.lower()
    noteBook = { "e":30, "t":31, "a":32, "o": 33, "i": 34, "n" :35, "h": 36}
    Notes = []
    for NoteBook in s:
        if s in noteBook:
            Notes.append(noteBook[s])
    return Notes

please help.

Try this instead:

for character in s:
    if character in noteBook:
        Notes.append(noteBook[character])

Also, make sure the return Notes is outside of the for loop.

Check this :

def text2notes(s):
  s = s.lower()
  noteBook = {"e": 30, "t": 31, "a": 32, "o": 33, "i": 34, "n": 35, "h": 36}
  Notes = []
  for NoteBook in s:
    print NoteBook
    if NoteBook in noteBook:
      Notes.append(noteBook[NoteBook])

  return Notes

Checking for the NoteBok not s in the loop

You could convert your check into a Boolean and then make a conditional statement :

    def text2notes(s):
      s = s.lower()
      noteBook = {"e": 30, "t": 31, "a": 32, "o": 33, "i": 34, "n": 35, "h": 36}
      Notes = []
      Note = (notebook in s)
      print Note
      if Note is True:
         Notes.append(noteBook[NoteBook])

      return Notes

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM