折腾:
【未解决】更新ViVoGame的autoSearchGame脚本生成魔域游戏信息列表
期间,希望对于一个已有的dict字典:
{
"id": 62990,
"pkgName": "com.m37.dldl.vivo",
。。。
"apkurl": "https://dlweb.gamecenter.vivo.com.cn/clientRequest/gameDownload?id=62990&pkgName=com.m37.dldl.vivo&channel=h5",
"comment": 4.0,
...
想要插入:
"realApkUrl": "https://gameapktxdl.vivo.com.cn/appstore/developer/soft/20200831/202008311206380xgm8.apk"
到dict中apkurl的后面
而不是:默认只会插入到dict的最末尾:

注:此处用最新的3.7+的Python,知道Python的dict是默认可以保留key的顺序的
python dict insert key after
How to insert dict key after specific key – Users – Discussions on Python.org
记得新python的dict的key是有index的?
找到index后,insert或许就可以了?
python dict key index
OrderedDict 有:
x.keys().index("d")去试试
i={'foo':'bar', 'baz':'huh?'}
keys=i.keys() #in python 3, you'll need `list(i.keys())`
values=i.values()
print keys[values.index("bar")] #'foo'这个办法不错
python dict insert
Python : How to add / append key value pairs in dictionary – thispointer.com
dict没有:add(), append(), or insert()
算了,自己实现
def insertKeyValueAfterDictKey(curDict, afterKey, newKey, newValue):
"""Insert new key and new value after specific key
Args:
afterKey (str): name of after the key
newKey (str): new key to insert
newValue (Any?): new value to insert
Returns:
Raises:
"""
keyList = curDict.keys()
valuesList = curDict.values()
afterKeyIndex = keyList.index(afterKey)
keyList.insert(afterKeyIndex, newKey)
valuesList.insert(afterKeyIndex, newValue)
updatedDict = {}
for keyIdx, eachKey in enumerate(keyList):
eachValue = valuesList[keyIdx]
updatedDict[eachKey] = eachValue
return updatedDict去调试看看
发现insert是插入到上面了。此处选择去插入到下面
keyList = list(curDict.keys()) keyMaxNum = len(keyList) keyMaxIdx = keyMaxNum - 1 # valuesList = curDict.values() valuesList = list(curDict.values()) afterKeyIndex = keyList.index(afterKey) # 6 if afterKeyIndex < keyMaxIdx: toInsertIndex = afterKeyIndex + 1 keyList.insert(afterKeyIndex, newKey) valuesList.insert(afterKeyIndex, newValue) else: keyList.append(newKey) valuesList.append(newValue)
继续调试
调用代码:
detailGameInfo = insertKeyValueAfterDictKey(detailGameInfo, "apkurl", "realApkUrl", detailRealApkUrl)
可以在apkurl后面插入realApkUrl的key了:

也插入了对应的realApkUrl的值:

最后更新后的dict就是我们要的结果了:

【总结】
最后完整代码:
def insertKeyValueAfterDictKey(curDict, afterKey, newKey, newValue):
"""Insert new key and new value after specific key
Args:
afterKey (str): name of after the key in dict
newKey (str): new key to insert
newValue (Any?): new value to insert
Returns:
Raises:
"""
# keyList = curDict.keys()
keyList = list(curDict.keys())
keyMaxNum = len(keyList)
keyMaxIdx = keyMaxNum - 1
# valuesList = curDict.values()
valuesList = list(curDict.values())
afterKeyIndex = keyList.index(afterKey) # 6
if afterKeyIndex < keyMaxIdx:
toInsertIndex = afterKeyIndex + 1
keyList.insert(toInsertIndex, newKey)
valuesList.insert(toInsertIndex, newValue)
else:
keyList.append(newKey)
valuesList.append(newValue)
updatedDict = {}
for keyIdx, eachKey in enumerate(keyList):
eachValue = valuesList[keyIdx]
updatedDict[eachKey] = eachValue
return updatedDict调用:
detailGameInfo = insertKeyValueAfterDictKey(detailGameInfo, "apkurl", "realApkUrl", detailRealApkUrl)
即可。