【发布时间】:2014-09-24 05:54:40
【问题描述】:
我要做的是将输入的以逗号分隔的字符串更改为列表,然后为每个列表作为键打印关联的值。
def main():
myDict = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5....}
u_input = input("Enter a letter")
myList = [x.strip() for x in u_input.split(',')]
result = searchDict(myList)
print(result)
def searchDict(key):
for ml in key:
value = myDict.get(ml, "not found")
re = []
re.append(value)
print('-'.join(re)) #this one shows each value for each entered key separated by comma but it does not print it in one line also prints 'None' on the end
#res = '-'.join(re)
#return res //this only shows the value for the first key only even if I enter multiple letter
main();
问题是,如果我返回 'res' 而不是打印它,我只会得到第一个键值。
用打印输出(如果我输入a,b):1, 2, none
输出并返回1
【问题讨论】:
-
如果你显示你的输出,这可能是一个更好的问题!
-
@Kasra,我已经包含了我得到的内容。
-
另外,您必须注意代码的缩进、语法和验证!拒绝投票!
-
@Kasra,好的。
标签: python