Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据Python中列表中存在的值从列表中删除词典?_Python_List_Csv_Dictionary - Fatal编程技术网

如何根据Python中列表中存在的值从列表中删除词典?

如何根据Python中列表中存在的值从列表中删除词典?,python,list,csv,dictionary,Python,List,Csv,Dictionary,我是Python新手,如果我的代码不是以最“Python”的方式编写的,请提前道歉 我正在将一个CSV文件上载到脚本中,如果行符合某些条件,我希望筛选该CSV 我有两张单子,a和b。一旦字典位于a_-lst中,我将检查b_-lst中是否有具有相应键:value的字典。如果存在匹配项,则会将其打印到控制台。我不想打印到控制台,而是想从列表中删除该项。我该怎么做 a_lst = [] b_lsts = [] with open(file_name, 'rt') as f: read

我是Python新手,如果我的代码不是以最“Python”的方式编写的,请提前道歉

我正在将一个CSV文件上载到脚本中,如果行符合某些条件,我希望筛选该CSV

我有两张单子,a和b。一旦字典位于a_-lst中,我将检查b_-lst中是否有具有相应键:value的字典。如果存在匹配项,则会将其打印到控制台。我不想打印到控制台,而是想从列表中删除该项。我该怎么做

a_lst = []
b_lsts = []

with open(file_name, 'rt') as f:
        reader = csv.DictReader(f)
        for row in reader:
            if row['Minutes'] == '0' and row['MB'] == '0' and row['Calls'] == '1':
                a_lst.append(row)
            elif row['Minutes'] == '0' and row['MB'] == '' and row['Calls'] == '1':
                a_lst.append(row)
            elif row['Minutes'] == '' and row['MB'] == '0' and row['Calls'] == '1':
                a_lst.append(row)
            elif row['Minutes'] == '' and row['MB'] == '' and row['Calls'] == '1':
                a_lst.append(row)
            else:
                b_lst.append(row)

i = 0
while i < len(a_lst):
    if not any(d['Name'] == a_lst[i]['Name'] for d in b_lst):
        print a_lst[i]['Name']+"(Row"+str(i)+") is not b_lst."
    else:
        print a_lst[i]['Name']+"(Row"+str(i)+") is present."
            i+=1

如果上面的数据是我插入的,我只想看到John的名字,因为他是唯一一个他的名字的所有行都包含值“0,0,1”的人。

只要从列表中删除该元素,如果它具有相同的键/值,您还想删除它,而不是在任何之前,因为如果有匹配项,我们想删除:

for ele in a_lst[:]:
    if  any(d['Name'] == ele['Name'] for d in b_lst):
        a_lst.remove(ele)
或者在添加之前忘记使用any和filter,将行['Name']添加到集合并检查我们是否已经看到它:

seen = set()
with open(file_name, 'rt') as f:
        reader = csv.DictReader(f)
        for row in reader:
           if row['Name'] in seen:
               continue
           if all((row['Minutes'] == '0', (row['MB'] == '0' or not row['MB']), row['Calls'] == '1')):
               a_lst.append(row)
           elif all((not row['Minutes'], (row['MB'] or not row['MB']), row['Calls'] == '1')):
                a_lst.append(row)
           else:
               seen.add(row['Name']) 
         # remove "else:" and just use seen.add(row['Name']) outside the elif if you want all dups removed
根据您的编辑:

seen = set()
with open(infile, 'rt') as f:
    reader = csv.reader(f,delimiter=",")
    for row in reader:
        if row[0] in seen:
            continue
        if all(x in {"0", "1"} for x in row[2:]):
            print(row)
        seen.add(row[0])
输出:

['Steve', '0777777777', '0', '0', '1']
['John', '078888888', '0', '0', '1']
['John', '078888888', '0', '0', '1']
Steve和John在其相对列中都只有0和1

如果您只希望名称的列中仅包含0和1:

from collections import defaultdict
d = defaultdict(list)

with open(infile, 'rt') as f:
    reader = csv.reader(f,delimiter=",")
    for row in reader:
        d[row[0]].append([row, set(row[2:])])

print([v[0][0] for k, v in d.items() if all(sub[1] == {"0","1"} for sub in v)])

[['John', '078888888', '0', '0', '1']]
如果您的姓名始终分组在一起,则使用集合:

seen = set()
temp = set()

with open(infile, 'rt') as f:
    reader = csv.reader(f,delimiter=",")
    next(reader)
    prev = None
    for row in reader:
        # found new name and it is not the first
        if row[0] not in seen and temp:
            # set should only hav  and 1 if all columns only contain 0,1
            if temp == {"0", "1"}:
                print(prev)  # print previous row
            # reset temp
            temp = set()
        seen.add(row[0])
        temp.update(row[2:])
        # need to keep track of previous row 
        prev = row
输出:

['Steve', '0777777777', '0', '0', '1']
['John', '078888888', '0', '0', '1']
['John', '078888888', '0', '0', '1']

向我们展示一个输入示例,以及处理后您希望如何查看输出。我怀疑您的第二种方法可能会错过这样的情况:您已经将值添加到\u lst中,并且在第二种方法中已经获得了“Name”元素condition@volcano,这正是OP的代码所做的。如果他们想捕获更多,他们所要做的就是在每次追加后添加到集合中。正如我已经说过的那样,在之后做这项工作是徒劳的,没有必要添加一些东西来稍后将其带走。想象一下,您会遇到一个名为“Padraic Cunnigham”的行,该行与add条件匹配,之后您会找到一个与remove条件匹配的同名记录。它会一直存在的好吧,我的错。我是否删除任何索引2、3或4中的对象?如果temp=={0,1}或temp=={0,1,1},则可能: