简体   繁体   中英

python dictionary string operations

Currently I have my data in a directory

myDict = {'a':'Andy','b':'Bill','c':'Carly' }

I want to achieve something like

input = a --> output = Andy

input = ab --> output = Andy Bill

input = abc --> output = Andy Bill Carly

How can I do that ? Please help me

  • Iterate through the individual letters of the input string.
  • Look up each item in the dictionary.
  • Concatenate the results as you go.

Can you handle each of those capabilities? If not, please post your best attempt and a description of the problem.

isim is not a function, and all you need to do is access that key in the dictionary

def girdi_al():
    isim = input("isim giriniz: ")
    return isim

def isim_donustur(isim):
    cikti = isim

    donusum = {'a':'Ankara','b':'Bursa','c':'Ceyhan'}

    #How can I do?
    result = []
    for letter in cikti:
        result.append(donusum[cikti])
    print(' '.join(result))


def main():
    isim_donustur(girdi_al)

# === main ===
main() 
def girdi_al():
    isim = input("isim giriniz: ")
    return isim

def isim_donustur(isim):
    cikti = isim()

    donusum = {'a':'Ankara','b':'Bursa','c':'Ceyhan'}

    string = ''

    for char in cikti:
        string += donusum[char] + ' '

    print(string )

def main():
    isim_donustur(girdi_al)

# === main ===
main() 

somthing like this

myDict = {'a':'Andy','b':'Bill','c':'Carly' }
input_str = "abc"

" ".join([v for k,v in myDict.items() if k in list(input_str)])

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