简体   繁体   English

从具有相同名称字段的字典列表中删除键值对

[英]Delete a key-value pair from a list of dictionary having field of same name

newlist= [{'row': 4, 'col': 4}, {'row': 4, 'col': 5}, {'row': 4, 'col': 6}, {'row': 4, 'col': 7}, {'row': 4, 'col': 8}, {'row': 4, 'col': 9}, {'row': 4, 'col': 10}, {'row': 5, 'col': 4}, {'row': 5, 'col': 5}, {'row': 5, 'col': 6}, {'row': 5, 'col': 7}, {'row': 5, 'col': 8}, {'row': 5, 'col': 9}, {'row': 5, 'col': 10}, {'row': 6, 'col': 4}, {'row': 6, 'col': 5}, {'row': 6, 'col': 6}, {'row': 6, 'col': 7}, {'row': 6, 'col': 8}, {'row': 6, 'col': 9}, {'row': 6, 'col': 10}, {'row': 7, 'col': 4}, {'row': 7, 'col': 5}, {'row': 7, 'col': 6}, {'row': 7, 'col': 7}, {'row': 7, 'col': 8}, {'row': 7, 'col': 9}, {'row': 7, 'col': 10}, {'row': 8, 'col': 11}, {'row': 9, 'col': 11}]

I want to delete the key value pair having row 4,col 5 how to do that我想删除第 4 行第 5 列的键值对,该怎么做

                        for lines in newlist :
                            if lines['row'] == 4and lines['col'] == 5:
                                print("delete")
                                del added['row']['col']

trie above code but not worked.it shows an error TypeError: list indices must be integers or slices, not str尝试上面的代码但没有工作。它显示错误TypeError: list indices must be integers or slice, not str

The requirement is for below code要求是针对以下代码

          row=4
          cols=4
           l=9
            m=11
            added=[]
            while row <= l:
                print("row", row)
                m = 11
                cols= 4
                while cols <= m:
                    print("column",cols)
                    for li in newlist:
                        print("li", li)
                        if li['row'] == row and li['col']!= cols:
                            added.append({
                                'row':row,
                                'col':cols,
                                # 'id':li['id']
                            })
                            print(row, "", cols)
                            print("new")

                        print("lop",added)

                        new_added = [x for x in added if x['row'] != row or x['col'] != cols]


                    cols = cols + 1
                row = row + 1

so I require only所以我只需要

newlist= [{'row': 4, 'col': 11}, {'row': 5, 'col': 11}, 
{'row':'6','col:'11'} ,{'row':'7','col:'11'}{'row': 8, 'col': 4}, 
{'row': 8, 'col': 5}, {'row': 8, 'col': 6}, {'row': 8, 'col': 7}, 
{'row': 8, 'col': 8}, {'row': 8, 'col': 9}, {'row': 8, 'col': 10}, 
{'row': 9, 'col': 4}, {'row': 9, 'col': 5}, {'row': 9, 'col': 6}, 
{'row': 9, 'col': 7}, {'row': 9, 'col': 8}, {'row': 9, 'col': 9}, 
{'row': 9, 'col': 10}]

so I want output as above所以我想要上面的 output

newlist 3=out [{'row': 4, 'col': 4}, {'row': 4, 'col': 5}, {'row': 4, 'col': 6}, {'row': 4, 'col': 7}, {'row': 4, 'col': 8}, {'row': 4, 'col': 9}, {'row': 4, 'col': 10}, {'row': 4, 'col': 11}, {'row': 5, 'col': 4}, {'row': 5, 'col': 5}, {'row': 5, 'col': 6}, {'row': 5, 'col': 7}, {'row': 5, 'col': 8}, {'row': 5, 'col': 9}, {'row': 5, 'col': 10}, {'row': 5, 'col': 11}, {'row': 6, 'col': 4}, {'row': 6, 'col': 5}, {'row': 6, 'col': 6}, {'row': 6, 'col': 7}, {'row': 6, 'col': 8}, {'row': 6, 'col': 9}, {'row': 6, 'col': 10}, {'row': 6, 'col': 11}, {'row': 7, 'col': 4}, {'row': 7, 'col': 5}, {'row': 7, 'col': 6}, {'row': 7, 'col': 7}, {'row': 7, 'col': 8}, {'row': 7, 'col': 9}, {'row': 7, 'col': 10}, {'row': 7, 'col': 11}, {'row': 8, 'col': 4}, {'row': 8, 'col': 5}, {'row': 8, 'col': 6}, {'row': 8, 'col': 7}, {'row': 8, 'col': 8}, {'row': 8, 'col': 9}, {'row': 8, 'col': 10}, {'row': 8, 'col': 11}, {'row': 9, 'col': 4}, {'row': 9, 'col': 5}, {'row': 9, 'col': 6}, {'row': 9, 'col': 7}, {'row': 9, 'col': 8}, {'row': 9, 'col': 9}, {'row': 9, 'col': 10}, {'row': 9, 'col': 11}]

try via list comprehension:尝试通过列表理解:

checker=[{'row':x,'col':y} for x in {x['row'] for x in newlist} for y in range(4,12)]
#created a list of dict by the given range provided by you
out=[x for x in checker if x not in newlist]
#only get those elements of checker list which are not present in newlist

output of out: output 出:

[{'row': 4, 'col': 11},
 {'row': 5, 'col': 11},
 {'row': 6, 'col': 11},
 {'row': 7, 'col': 11},
 {'row': 8, 'col': 4},
 {'row': 8, 'col': 5},
 {'row': 8, 'col': 6},
 {'row': 8, 'col': 7},
 {'row': 8, 'col': 8},
 {'row': 8, 'col': 9},
 {'row': 8, 'col': 10},
 {'row': 9, 'col': 4},
 {'row': 9, 'col': 5},
 {'row': 9, 'col': 6},
 {'row': 9, 'col': 7},
 {'row': 9, 'col': 8},
 {'row': 9, 'col': 9},
 {'row': 9, 'col': 10}]

Use the remove method for lists:对列表使用 remove 方法:

for dic in newList:
    if dic['row'] == 4 and dic['col'] == 5:
        newList.remove(dic)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据来自单独列表的匹配项,使用来自字典列表中的值的键值对创建一个新字典 - Create a new dictionary with the key-value pair from values in a list of dictionaries based on matches from a separate list 如何将现有列表转换为 Python 中字典的键值对? - How to turn an existing list into a key-value pair for a dictionary in Python? 从列列表中的元素中获取字典,其中键值对是 pandas 中元素的数量和元素值 - Get the dictionary from elements in the list of a column where key-value pair are number of elements and element value in pandas 从字典的键值对中提取值 - Extract value from key-value pair of dictionary 列表理解 - 尝试从键值对数组的数组中创建字典 - List comprehension - attempting to create dictionary from array of key-value pair arrays 如何从基于另一个键值对的列表中的字典中访问值? - How to access values from a dictionary inside a list based on another key-value pair? 在不使用列表、迭代器的情况下从 Python 中的字典中检索第一个键值对 - Retrieve first key-value pair from a dictionary in Python without using list, iter 如何从列表中提取值并将其存储为字典(键值对)? - How to extract values from list and store it as dictionary(key-value pair)? 将特定键值对从一个字典复制到另一个字典? - Copy specifc key-value pair from one dictionary into another? 给定键值对,从字典中派生项目 - Given key-value pair(s), derive the item from a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM