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 3.x 我想将我的字典键与2d列表的值进行比较,如果该值正确,我想附加到一个新列表_Python 3.x_List_Dictionary_Append_Filtering - Fatal编程技术网

Python 3.x 我想将我的字典键与2d列表的值进行比较,如果该值正确,我想附加到一个新列表

Python 3.x 我想将我的字典键与2d列表的值进行比较,如果该值正确,我想附加到一个新列表,python-3.x,list,dictionary,append,filtering,Python 3.x,List,Dictionary,Append,Filtering,所以我想让这段代码在gff_lijst上循环,这是一个2d列表。它检查字典的键是否与列表的第8个值相同,如果相同,则将列表的值附加到 gff lijst_gefilterd 我希望你能帮助我 您最好只遍历2D列表,然后检查最后一个元素是否在dict中 以下是一个例子: gff_lijst_gefilterd = [] for x in range(1, len(gff_lijst)): if filter_genID[x] == x: gff_lijst_gefilter

所以我想让这段代码在gff_lijst上循环,这是一个2d列表。它检查字典的键是否与列表的第8个值相同,如果相同,则将列表的值附加到 gff lijst_gefilterd
我希望你能帮助我

您最好只遍历2D列表,然后检查最后一个元素是否在dict中

以下是一个例子:

gff_lijst_gefilterd = []
for x in range(1, len(gff_lijst)):
    if filter_genID[x] == x:
        gff_lijst_gefilterd.append(gff_lijst_gefilterd[x])
return gff_lijst_gefilterd

所以你有一个类似这样的列表
[[1,0,1,0,1,0,1,5],[1,0,1,0,1,4]
,你想看看
5
4
是否是你的dict中的一个键?是的,我想看看它们是否在我的dict中,然后我想把它们添加到一个新的列表中。它更像[[1,2,3],[3,4,5],[4,5,5],我想检查2d列表的最后一个值,例如它是5,我想作为一个新列表[[3,4,5],[4,5,5]]。你知道我现在的意思吗?我想是的。我会补充一个答案,如果我有误解,你可以纠正我。
# this is just a test list
gff_lijst = [[1, 0, 1, 0, 1, 0, 1, 5], 
             [1, 0, 1, 0, 1, 0, 1, 4],
             [1, 0, 1, 0, 1, 0, 1, 3],
             [1, 0, 1, 0, 1, 0, 1, 8],
             [1, 0, 1, 0, 1, 0, 1, 9],
             [1, 0, 1, 2, 1, 5, 1, 4],
             [1, 0, 1, 0, 1, 0, 1, 6]]

# and a test dict
filter_genID = {3 : 'test', 4 : 'test2'}

gff_lijst_gefilterd = []

# iterate through your 2d list
# in this case i will be each list in your 2d list
for i in gff_lijst:
    # check if the last element of the current list is in your dict keys
    if i[-1] in filter_genID:
        # if it is we add it to our list
        gff_lijst_gefilterd.append(i)

print(gff_lijst_gefilterd)