我有一个类似于以下的代码段,它将引发无效的语法错误:
static void Main(string[] args)
{
var bob = new Dictionary<string, Dictionary<string, int>>();
bob["10003"] = new Dictionary<string, int> {
{ "computer", 25 },
{ "desk", 23 },
{ "chair", 7 },
{ "score", 44} };
bob["10004"] = new Dictionary<string, int> {
{ "computer", 35 },
{ "desk", 23 },
{ "chair", 7 },
{ "score", 77} };
bob["10005"] = new Dictionary<string, int> {
{ "computer", 85 },
{ "desk", 23 },
{ "chair", 7 },
{ "score", 10} };
var ordered = bob.OrderBy(z => z.Value["score"]).ToList();
Console.WriteLine(ordered.First().Value["computer"]); // 85 since 10 is the lowest score
Console.WriteLine(ordered.Last().Value["computer"]); // 35 since 77 is the highest score
Console.ReadLine();
}
基本上,我希望lst = [0, 0, 0]
new_lst = [lst[-1] = i + 1 for i in range(3)]
是列表的列表,其中每个列表都是new_lst
,但最后一个元素已更改。有任何想法如何在Python中执行此操作吗?
答案 0 :(得分:9)
您只能在它们是独立语句时进行分配,即不在列表理解范围之内。在控制语句中,例如for
或In [945]: lst = [0, 0, 0]
In [946]: new_lst = [lst[:-1] + [i + 1] for i in range(3)]
In [947]: new_lst
Out[947]: [[0, 0, 1], [0, 0, 2], [0, 0, 3]]
s
lst[:-1]
lst
提取[i + 1]
中的每个元素,但最后一个元素除外,^((?!550)\d{8}|(?!393)\d{5})$
将附加到列表的末尾