我一直在网上研究一种创建有序字典的简单方法,并使用 OrderedDict 及其更新方法,我已经成功实现了一次,但是现在代码往往不会对添加的术语进行排序,例如放置的项目是:
Doc1: Alpha, zebra, top
Doc2: Andres, tell, exta
Output: Alpha, top, zebra, Andres, exta, tell
My goal is to have Alpha, Andres......, top, zebra
Run Code Online (Sandbox Code Playgroud)
这是代码:
finalindex= collections.OrderedDict()
ctr=0
while ctr < docCtr:
filename = 'dictemp%d.csv' % (ctr,)
ctr+=1
dicTempList = io.openTempDic(filename)
print filename
for key in dicTempList:
if key in finalindex:
print key
for k, v in finalindex.items():
newvalue = v + "," + dicTempList.get(key)
finalindex.update([(key, newvalue)])
else:
finalindex.update([(key, dicTempList.get(key))])
io.saveTempDic(filename,finalindex)
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我吗?
OrderedDict我正在尝试更新具有相同值的键列表int,例如
for idx in indexes:
res_dict[idx] = value
Run Code Online (Sandbox Code Playgroud)
其中value是一个int变量,indexes是slist的inta,充当键,res_dict是 an OrderedDict,尝试在一行中解决上述问题,
res_dict[indexes]=value
Run Code Online (Sandbox Code Playgroud)
但得到了错误:
TypeError: unhashable type: 'list'
Run Code Online (Sandbox Code Playgroud)
循环或列表理解是在这里进行此更新的唯一方法吗?
能OrderedDict拿到关键位置吗?
就像是list的index()
test = ['a', 'b', 'c', 'd', 'e']
test.index('b') # return 1
Run Code Online (Sandbox Code Playgroud) 我正在尝试获取在字典中插入的第一个项目(尚未重新排序)。例如:
_dict = {'a':1, 'b':2, 'c':3}
Run Code Online (Sandbox Code Playgroud)
我想得到元组('a',1)
我怎样才能做到这一点?
我有一个字典,其中包含我想要在模板中显示的列表:
from django.utils.datastructures import SortedDict
time_filter = SortedDict({
0 : "Eternity",
15 : "15 Minutes",
30 : "30 Minutes",
45 : "45 Minutes",
60 : "1 Hour",
90 : "1.5 Hours",
120 : "2 Hours",
150 : "2.5 Hours",
180 : "3 Hours",
210 : "3.5 Hours",
240 : "4 Hours",
270 : "4.5 Hours",
300 : "5 Hours"
})
Run Code Online (Sandbox Code Playgroud)
我想在模板中创建一个下拉列表:
<select id="time_filter">
{% for key, value in time_filter.items %}
<option value="{{ key }}">{{ value }}</option>
{% endfor %}
</select> …Run Code Online (Sandbox Code Playgroud) python django ordereddictionary django-templates sorteddictionary
我需要更改有序字典的值.我使用了for循环,但值没有变化.我发现这是因为我在循环中分配变量名而不是直接使用括号[]表示法.为什么我不能通过循环变量名来引用循环中的值?
尝试:
idx = 0
for name,val in settings.items():
idx += 1
val = idx
print name,val
Run Code Online (Sandbox Code Playgroud)
结果: OrderedDict([('Accel X Bias', None), ('Mag X Bias', None)])
预期: OrderedDict([('Accel X Bias', 1), ('Mag X Bias', 2)])
完整代码:
import collections
settings = collections.OrderedDict([('Accel X Bias', None), ('Mag X Bias', None)])
idx = 0
print "\nIn loop, values are changed:"
for name,val in settings.items():
idx += 1
val = idx
print name,val
print "\nAfter Loop, values didn't change:\n",settings
for …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
self.statusIcons = collections.OrderedDict
for index in guiConfig.STATUS_ICON_SETS:
self.statusIcons[index] = {condition:\
wx.Image(guiConfig.STATUS_ICON_STRING.format(index, condition),wx.BITMAP_TYPE_PNG).ConvertToBitmap() \
for condition in guiConfig.STATUS_ICON_CONDITIONS}
Run Code Online (Sandbox Code Playgroud)
它建立了wx.Image对象的常规字典的有序字典,这些字典通过理解来设置。我最初嵌套了dict理解,但效果很好,但决定我需要订购顶级dict,所以最终以这种方式结束。问题是,现在我收到此错误:
TypeError: 'type' object does not support item assignment
Run Code Online (Sandbox Code Playgroud)
将相关代码归零。我无法弄清楚我做错了什么。即使它不是顶级的,ordersdict是否也不允许理解?也许它会尝试对有序词典中的所有词典进行排序,并且因为理解力处于较低水平而不能这样做吗?不确定,也许这是因为隧道视觉我无法发现的荒谬之处。
PS:如果您需要了解我在上面引用的全局变量中包含的内容:
STATUS_ICON_SETS = ("comp", "net", "serv", "audio", "sec", "ups", "zwave", "stats")
STATUS_ICON_CONDITIONS = ("on", "off")
STATUS_ICON_STRING = "images/{0}_{1}.png"
Run Code Online (Sandbox Code Playgroud) python for-loop ordereddictionary typeerror dictionary-comprehension
我的节目正在制作:
ValueError:要解压缩的值太多.
我复制了在其他实例中工作的代码行.
new_dict = (("data", 0))
new_dict = collections.OrderedDict(new_dict) #the line producing the error
Run Code Online (Sandbox Code Playgroud)
这个和其他似乎有效的区别在于它们有更多的价值.
我想添加两个这样的OrderedDict对象:
dict1 = OrderedDict([('Table', [10, 20, 30, 'wood']), ('Chair', [200, 300, 400, 'wood'])])
dict2 = OrderedDict([('Table', ['red', 55]), ('Chair', ['blue', 200])])
Run Code Online (Sandbox Code Playgroud)
然后创建一个新的 OrderedDict(顺序很重要):
dict3 = OrderedDict([('Table', [10, 20, 30, 'wood', 'red', 55]), ('Chair', [200, 300, 400, 'wood', 'blue', 200])])
Run Code Online (Sandbox Code Playgroud)
如果另一个中有任何键dict1或dict2其他中不存在,则应将其忽略,仅将匹配的键用于输出。所有值都是列表。
我正在尝试使用 OrderedDict 打印有序字典,但是当我打印它时,“OrderedDict”也会打印。仅供参考,这只是一个代码段,而不是整个代码。我能做些什么来解决这个问题?我正在使用 Python 3.2
看起来像这样:
def returnAllStats(ints):
choices = ["Yes","No"]
dictInfo = {"Calories":ints[2], "Servings per Container":ints[0], "Amount per Serving":ints[1], "Total Fat":(ints[3]/100)*ints[2], "Saturated Fat":(ints[4]/100)*(ints[3]/100)*ints[2], "Cholesterol":ints[5], "Fiber":ints[6], "Sugar":ints[7], "Protein":ints[8], "Sodium":ints[9], "USA":choices[ints[10]], "Caffeine":ints[11]}
dictInfo = collections.OrderedDict(dictInfo)
return dictInfo
Run Code Online (Sandbox Code Playgroud)
我在写入的文本文件中得到了这个:
('snack', 'bananana')OrderedDict([('USA', 'No'), ('Sodium', 119), ('Calories', 479), ('Servings per Container', 7), ('Sugar', 49), ('Saturated Fat', 37.553599999999996), ('Total Fat', 234.71), ('Cholesterol', 87), ('Amount per Serving', 40), ('Fiber', 1), ('Caffeine', 7), ('Protein', 53)])
Run Code Online (Sandbox Code Playgroud)
谢谢!