1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python字典中给一个键增加值_在Python字典中向单个键添加多个值

python字典中给一个键增加值_在Python字典中向单个键添加多个值

时间:2022-10-31 18:36:51

相关推荐

python字典中给一个键增加值_在Python字典中向单个键添加多个值

Python字典今天真的很吸引我。我一直在堆栈上奔波,试图找到一种方法,将一个新值简单地附加到python字典中的现有键上,但每次尝试都失败,使用的语法与这里相同。在

我要做的是:#cursor seach a xls file

definitionQuery_Dict = {}

for row in arcpy.SearchCursor(xls):

# set some source paths from strings in the xls file

dataSourcePath = str(row.getValue("workspace_path")) + "\\" + str(row.getValue("dataSource"))

dataSource = row.getValue("dataSource")

# add items to dictionary. The keys are the dayasource table and the values will be definition (SQL) queries. First test is to see if a defintion query exists in the row and if it does, we want to add the key,value pair to a dictionary.

if row.getValue("Definition_Query") <> None:

# if key already exists, then append a new value to the value list

if row.getValue("dataSource") in definitionQuery_Dict:

definitionQuery_Dict[row.getValue("dataSource")].append(row.getValue("Definition_Query"))

else:

# otherwise, add a new key, value pair

definitionQuery_Dict[row.getValue("dataSource")] = row.getValue("Definition_Query")

我得到一个属性错误:AttributeError: 'unicode' object has no attribute 'append'

但我相信我所做的与提供的答案相同here

我尝试过其他各种方法,但没有成功地处理其他各种错误消息。我知道这可能很简单,也许我在网上找不到合适的来源,但我被卡住了。有人愿意帮忙吗?在

谢谢,

迈克

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。