尝试使用 OrderedDict 数据结构编写复杂算法
Trying to code a complex algorithm with `OrderedDict` data Structure
我正在尝试编写算法:
我有这个 OrderedDict
数据类型的输入,如下所示:
odict_items([(3, [(0, 1), (1, 1), (1, 1)]), (11, [(0, 0), (1, 1), (1, 1)]), (12, [(0, 0), (1, 1), (1, 1)])])
我正在尝试编写一个函数来在每个 key
中添加相同元组的数量,例如预期输出如下所示:如果有 (1,1)
个相同的元组,则 [=16] =] 如果是 2
的两倍,那么一个:
odict_items([(3, [(0, 1), (1, 1), (1, 1)],2), (11, [(0, 0), (1, 1), (1, 1)],2), (12, [(0, 0), (1, 0), (1, 1)]),1])
这是我的尝试,但如何改进它并将其添加到 OrderedDict
?
def foo(OrderedDict):
listOfDic = list(makeDataStruc().items())
tupleCounter = 0
for i in range(len(listOfDic[1])):
if listOfDic[1][1][i][0] == 1 and listOfDic[1][1][i][1] == 1:
tupleCounter += 1
return tupleCounter
我哪里出错了?
我做出以下假设,
- 您只想将
(1,1)
的计数添加到 OrderedDict
值
- 您不想创建覆盖当前
OrderedDict
的新数据结构。
- 可以修改原词典
现在根据问题中可用的信息和上述假设,一种可能的解决方案是用包含两个元素的列表替换每个值,即`[原始值,(1, 1) 的计数]
from collections import OrderedDict
odict_items = [(3, [(0, 1), (1, 1), (1, 1)]),
(11, [(0, 0), (1, 1), (1, 1)]),
(12, [(0, 0), (1, 1), (1, 1)])]
my_odict = OrderedDict()
for item in odict_items:
k, v = item
my_odict[k] = v
现在计算每个值中 (1,1)
的出现次数并相应地更新值
pattern_to_find = (1, 1)
for key, value in my_odict.items():
tuple_count = value.count(pattern_to_find)
new_value = [value, tuple_count]
my_odict[key] = new_value
现在词典有以下内容:
OrderedDict([(3, [[(0, 1), (1, 1), (1, 1)], 2]),
(11, [[(0, 0), (1, 1), (1, 1)], 2]),
(12, [[(0, 0), (1, 1), (1, 1)], 2])])
现在您可以创建附加函数来仅访问值或元组计数
# Returns the count of (1, 1) only
def get_count(my_dict, key):
return my_dict[key][1]
# Return the original value only
def get_tuple(my_dict, key):
return my_dict[key][0]
所以你可以像这样使用它们
print(my_odict[3])
# [[(0, 1), (1, 1), (1, 1)], 2]
print(get_count(my_odict,3))
# 2
print(get_tuple(my_odict, 3))
# [(0, 1), (1, 1), (1, 1)]
我正在尝试编写算法:
我有这个 OrderedDict
数据类型的输入,如下所示:
odict_items([(3, [(0, 1), (1, 1), (1, 1)]), (11, [(0, 0), (1, 1), (1, 1)]), (12, [(0, 0), (1, 1), (1, 1)])])
我正在尝试编写一个函数来在每个 key
中添加相同元组的数量,例如预期输出如下所示:如果有 (1,1)
个相同的元组,则 [=16] =] 如果是 2
的两倍,那么一个:
odict_items([(3, [(0, 1), (1, 1), (1, 1)],2), (11, [(0, 0), (1, 1), (1, 1)],2), (12, [(0, 0), (1, 0), (1, 1)]),1])
这是我的尝试,但如何改进它并将其添加到 OrderedDict
?
def foo(OrderedDict):
listOfDic = list(makeDataStruc().items())
tupleCounter = 0
for i in range(len(listOfDic[1])):
if listOfDic[1][1][i][0] == 1 and listOfDic[1][1][i][1] == 1:
tupleCounter += 1
return tupleCounter
我哪里出错了?
我做出以下假设,
- 您只想将
(1,1)
的计数添加到OrderedDict
值 - 您不想创建覆盖当前
OrderedDict
的新数据结构。 - 可以修改原词典
现在根据问题中可用的信息和上述假设,一种可能的解决方案是用包含两个元素的列表替换每个值,即`[原始值,(1, 1) 的计数]
from collections import OrderedDict
odict_items = [(3, [(0, 1), (1, 1), (1, 1)]),
(11, [(0, 0), (1, 1), (1, 1)]),
(12, [(0, 0), (1, 1), (1, 1)])]
my_odict = OrderedDict()
for item in odict_items:
k, v = item
my_odict[k] = v
现在计算每个值中 (1,1)
的出现次数并相应地更新值
pattern_to_find = (1, 1)
for key, value in my_odict.items():
tuple_count = value.count(pattern_to_find)
new_value = [value, tuple_count]
my_odict[key] = new_value
现在词典有以下内容:
OrderedDict([(3, [[(0, 1), (1, 1), (1, 1)], 2]),
(11, [[(0, 0), (1, 1), (1, 1)], 2]),
(12, [[(0, 0), (1, 1), (1, 1)], 2])])
现在您可以创建附加函数来仅访问值或元组计数
# Returns the count of (1, 1) only
def get_count(my_dict, key):
return my_dict[key][1]
# Return the original value only
def get_tuple(my_dict, key):
return my_dict[key][0]
所以你可以像这样使用它们
print(my_odict[3])
# [[(0, 1), (1, 1), (1, 1)], 2]
print(get_count(my_odict,3))
# 2
print(get_tuple(my_odict, 3))
# [(0, 1), (1, 1), (1, 1)]