在字典列表中增加键值

问题描述

我想在字典列表中添加一个id键,其中每个id代表枚举的嵌套字典。

当前词典列表:

current_list_d = [{'id': 0,'name': 'Paco','age': 18} #all id's are 0
                  {'id': 0,'name': 'John','age': 20}
                  {'id': 0,'name': 'Claire','age': 22}]

所需的输出

output_list_d = [{'id': 1,'age': 18} #id's are counted/enumerated
                  {'id': 2,'age': 20}
                  {'id': 3,'age': 22}]

我的代码

for d in current_list_d:
    d["id"]+=1

解决方法

您可以对enumerate使用一个简单的for循环,并在字典中就位更新rev键。

c(rev(cumprod(rev(v[-1]))),1)
#[1] 120  60  20   5   1
,

您可以使用变量。

private GUIStyle currentStyle = null;
 
void OnGUI()
{  
    InitStyles();
    GUI.Box( new Rect( 0,100,100 ),"Hello",currentStyle );
}
 
private void InitStyles()
{
    if( currentStyle == null )
    {
        currentStyle = new GUIStyle( GUI.skin.box );
        currentStyle.normal.background = MakeTex( 2,2,new Color( 0f,1f,0f,0.5f ) );
    }
}
 
private Texture2D MakeTex( int width,int height,Color col )
{
    Color[] pix = new Color[width * height];
    for( int i = 0; i < pix.Length; ++i )
    {
        pix[ i ] = col;
    }
    Texture2D result = new Texture2D( width,height );
    result.SetPixels( pix );
    result.Apply();
    return result;
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...