如何仅修改列表中的特定词典

时间:2019-07-10 04:54:34

标签: python

我只希望在满足条件的情况下更新列表中的特定词典。 例如,如果值等于无,则更改该值。 我尝试过的其他操作会影响列表中的所有词典,而这并不是我要尝试的操作。

clock = [{"date" : "07/09/2019", "clockIn" : 0500, "clockOut" : 0130}]
newtime = {"date" : "07/10/2019", "clockIn": 800, "clockOut": None}
clock.append(newtime)
print "first print", clock

for time in clock:
    print "second print" , time
    for k, y in time.values()

2 个答案:

答案 0 :(得分:1)

@Anthony您可以遍历列表和字典并有条件地更改各个元素。

renderItem={({item, index}) => {
    if (index == 0) {
        return <View style={{flex: 1, flexDirection: 'row'}}>
                <Image source={item.image} style={styles.flatList_imageView} resizeMode="contain"/>
                </View>
    }

    return <View style={{flex: 1, flexDirection: 'row'}}>
            <Text>{item.key}</Text>
            </View>
}

输出:

clock = [{"date" : "07/09/2019", "clockIn" : "0500", "clockOut" : "0130"}]
newtime = {"date" : "07/10/2019", "clockIn": "800", "clockOut": None}
clock.append(newtime)
print ( "first print", clock)
print("\n")

for time in clock:    
    for item in time :                
       if time[item] is None:
          time[item] = "0000"

print ( "Second print", clock)

答案 1 :(得分:0)

for time in clock:
    for i in time:
        if time[i] is None:
            time[i] = "changed"

print("second print", clock)

输出:

first print [{'date': '07/09/2019', 'clockIn': 500, 'clockOut': 130}, {'date': '07/10/2019', 'clockIn': 800, 'clockOut': None}]

second print [{'date': '07/09/2019', 'clockIn': 500, 'clockOut': 130}, {'date': '07/10/2019', 'clockIn': 800, 'clockOut': 'changed'}]