无法从我的函数返回字典

时间:2014-06-21 16:56:37

标签: python dictionary return

我收到无效的语法错误

 SyntaxError: invalid syntax
 root@collabnet:/home/projects/twitterBot# python twitterBot2.py
 File "twitterBot2.py", line 58
 return screenNames

从此函数返回字典时:

def getUserName(lookupIds):
     l = len(lookupIds) # length of list to process
     i = 0 #setting up increment for while loop 
     screenNames = {}#output dictionary
     count = 0 #count of total numbers processed
     print 'fetching usernames'
     while i < l:
         toGet = []
         toAppend = []
         if l - count > 100:#blocks off in chunks of 100
             for m  in range (0,100):
                toGet.append(lookupIds[count])
                count = count + 1
                print toGet
         else:#handles the remainder 
             print 'last run'
             r = l - count 
             print screenNames
             for k  in range (0,r):#takes the remainder of the numbers 
                 toGet.append(lookupIds[count])
                 count = count + 1
             i = l   # kills loop

         toAppend = api.lookup_users(user_ids=toGet)
         print toAppend
         screenNames.append(zip(toGet, toAppend)

         #creates a dictionary screenNames{user_Ids, screen_Names}

     #This logic structure breaks up the list of numbers in chunks of 100 or their
     #Remainder and addes them into a dictionary with their count number as the 
     #index value   
     #print str(len(toGet)), 'screen names correlated'
     return screenNames

我正在运行这样的功能:

 toPrint = {}#Testing Only
 print "users following", userid 
 toPrint = getUserName(followingids)#Testing Only

我尝试过注释并只打印screenNames但我仍然得到相同的错误,而不是print语句。我很确定我正在运行返回权利,谢谢你的样子。

1 个答案:

答案 0 :(得分:2)

您忘记了前一行的右括号:

screenNames.append(zip(toGet, toAppend)
#                 ^   ^               ^^?
#                 |   \---- closed ---/|
#                 \----- not closed ---/

此处还有其他问题,因为screenNames dict 对象,而不是列表,并且没有.append()方法。如果您想使用键值对更新字典,请改用update()

screenNames.update(zip(toGet, toAppend))