Python Can';t从我的函数返回字典
我得到一个无效的语法错误Python Can';t从我的函数返回字典,python,dictionary,return,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
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
我试过注释,只是打印
屏幕名
,但除了打印语句之外,我仍然收到相同的错误。我很确定我是正确的,谢谢你的关注 您忘记了前一行的右括号:
screenNames.append(zip(toGet, toAppend)
# ^ ^ ^^?
# | \---- closed ---/|
# \----- not closed ---/
这里还有一个问题,因为screenNames
是一个dict
对象,而不是列表,并且没有.append()
方法。如果要使用键值对更新字典,请改用update()
:
screenNames.update(zip(toGet, toAppend))
您提供的缩进是否与代码中的缩进相同?另外,
屏幕名称
被定义为set/dict(因为您使用的是空的{}
初始值设定项,所以它是不明确的),但您正在使用append
向其中添加元素,这对于dict
或set
@aruistante都无效:它肯定是一个dict,没有歧义。不能以这种方式定义空集。这一行上的注释甚至清楚地表明了OP在这一行上预期会发生什么。@MartijnPieters哦,你是对的,我忘了Python不允许使用空集文本来解决上述歧义;)尽管由于屏幕名
是一个set/dict
,因此没有append
screenNames.update(zip(toGet, toAppend))