在下面的函数中有一个名为task_ranku{}
的字典。
我正在尝试按其值排序并打印字典。
但是,当我添加这两行进行排序和打印时,我收到错误Expected dict, got list
有人可以解释我做错了吗?
cdef dict task_ranku_sorted = sorted(task_ranku.values())
for key, value in task_ranku_sorted.iteritems():
print key, value
def heft_order(object nxgraph, PlatformModel platform_model):
"""
Order task according to HEFT ranku.
Args:
nxgraph: full task graph as networkx.DiGraph
platform_model: cscheduling.PlatformModel instance
Returns:
a list of tasks in a HEFT order
"""
cdef double mean_speed = platform_model.mean_speed
cdef double mean_bandwidth = platform_model.mean_bandwidth
cdef double mean_latency = platform_model.mean_latency
cdef dict task_ranku = {}
for idx, task in enumerate(list(reversed(list(networkx.topological_sort(nxgraph))))):
ecomt_and_rank = [
task_ranku[child] + (edge["weight"] / mean_bandwidth + mean_latency)
for child, edge in nxgraph[task].items()
] or [0]
task_ranku[task] = task.amount / mean_speed + max(ecomt_and_rank) + 1
# use node name as an additional sort condition to deal with zero-weight tasks (e.g. root)
return sorted(nxgraph.nodes(), key=lambda node: (task_ranku[node], node.name), reverse=True)
答案 0 :(得分:0)
task_ranku_sorted不是dictonary,它是一个列表:
print type(task_ranku_sorted)
在原始字典上调用values()并仅对值列表进行排序。
您可以查看:
#include <bits/stdc++.h>
#define m 21
using namespace std;
int isitoperattor(char c) //to find precdenec of operator
{
if(c=='+'||c=='-')
return 1;
if(c=='*' || c=='/')
return 2;
if(c=='('||c==')')
return 3;
}
void infixtopostfix(char str[m]){
char out[m];
cout<<m<<endl;
stack <char> s;
static int k;
for(int i=0;i<m;i++)
{
cout<<"hello";
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')){
out[k]=str[i];
k++;}
else if (str[i]=='(')
s.push(str[i]);
else if (str[i]==')'){
//int j=i-1;
while(s.top()!='('){
out[k]=s.top();
s.pop();
k++;
//-;
}
s.pop();
}
else
{
if(isitoperattor(str[i]) && (isitoperattor(str[i])<isitoperattor(s.top()))){
while(isitoperattor(s.top())>=isitoperattor(str[i])){
out[k]=s.top();
s.pop();
k++;
}
}
}
s.push(str[i]);
}
}
while(!s.empty())
{
out[k]=s.top();
s.pop();
k++;
}
//string out;
for(int j=0;j<k;j++)
cout<<out[j];
}
int main()
{
char str[m]="a+b*(c^d-e)^(f+g*h)-i";
infixtopostfix(str);
return 0;
}
答案 1 :(得分:0)
cdef dict task_ranku_sorted = sorted(task_ranku.values())
这将给出排序的值列表,因此task_ranku_sorted
是已排序值的列表。
以及您的使用功能iteritems()
。此功能仅适用于dict
类型变量,不适用于list
,并且您将list
用于task_ranku_sorted.iteritems()
import operator
sorted_dictinory = sorted(dictinory.items(), key=operator.itemgetter(1))
这就是您收到此错误的原因。
如果要对字典进行排序,请参阅以下功能:
sorted_dictinory = sorted(dictinory.items(), key=lambda x: x[1])
OR
from collections import OrderedDict
sorted_dictinory = OrderedDict(sorted(dictinory.items(), key=lambda x: x[1]))
OR
$a = array();
$new_data = array(
'name' => $_POST["product_name"],
'age' => $_POST["quantity_stock"],
'city' => $_POST["item_price"]
);
// pushing the post data each time the page reloads with post values
$data = file_get_contents("newfile.json");
$data = json_decode($data, true);
$lastIndex = count($data);
$data[$lastIndex] = $new_data;
$myfile = fopen("newfile.json", "w") or die("Unable to open file!");\
fwrite($myfile, json_encode($data));
fclose($myfile);
$data = file_get_contents("newfile.json");
$data = json_decode($data, true);
print_r($data);