Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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字典中计数为0的元素_Python_Dictionary - Fatal编程技术网

提取python字典中计数为0的元素

提取python字典中计数为0的元素,python,dictionary,Python,Dictionary,这是数据帧代码: import pandas as pd storage = pd.DataFrame({'red_wire':[2,2,8,1,5],'white_wire':[5,5,3,5,2],'black_wire':[0,10,4,2,1]}) 以及包含数据的数据帧结构: red_wire white_wire black_wire 0 2 5 0 1

这是数据帧代码:

    import pandas as pd
    storage = pd.DataFrame({'red_wire':[2,2,8,1,5],'white_wire':[5,5,3,5,2],'black_wire':[0,10,4,2,1]})
以及包含数据的数据帧结构:

            red_wire  white_wire  black_wire
    0         2           5           0
    1         2           5          10
    2         8           3           4
    3         1           5           2
    4         5           2           1
我们有两本字典。它们的外部有标签,标签上的电线颜色必须在盒子内部:

    box1 = {'red_wire':2,'white_wire':5,'black_wire':10}
    box2 = {'red_wire':2,'white_wire':5,'black_wire':0}
我制作这个代码是为了打印盒子的内容,我的目的是控制和打印电线代码,一些盒子可能有不同的颜色标签。 这就是为什么我提出了这个代码:

    for key,value in box1.items():  #getting box tags and quantities

        col_name = [column for column in storage.columns if key == column] #columns in storage df

        for x,i in zip(storage.columns,storage.index): #accesing data in dataframe

            if x in col_name and value:

                print(x,col_name,value)
输出为:

red_wire ['red_wire'] 2
white_wire ['white_wire'] 5
black_wire ['black_wire'] 10        
呼叫box2:

for key,value in box2.items():

    col_name = [column for column in storage.columns if key == column]#columns in dict  

    for x,i in zip(storage.columns,storage.index):#accesing data in dataframe

        if x in col_name and value:

            print(x,col_name,value)

red_wire ['red_wire'] 2
white_wire ['white_wire'] 5             
在框2中,我希望得到black_-wire['black_-wire']0,但它假定这是一个要跳过的值。 我希望在字典评估中获得0篇文章的打印,并且仅在框中不存在标记时跳过值

一些用户为我提供了很好的解决方案,可以在条件句后使用“不是无”来获取0:

 if x in col_name and value is not None:
它打印:

    red_wire ['red_wire'] 2
    white_wire ['white_wire'] 5
    black_wire ['black_wire'] 0
red_wire ['red_wire'] 2
white_wire ['white_wire'] 5
但如果我在0之前特别问:

for key,value in box2.items():

col_name = [column for column in storage.columns if key == column]#columns in dict  

for x,i in zip(storage.columns,storage.index):#accesing data in dataframe

    if x in col_name and value == storage[x][i]:

            print(x,col_name,value)
它打印:

    red_wire ['red_wire'] 2
    white_wire ['white_wire'] 5
    black_wire ['black_wire'] 0
red_wire ['red_wire'] 2
white_wire ['white_wire'] 5
“不是没有”,没有任何区别

#is not None 1st
if x in col_name and value is not None and value == storage[x][i]:
            print(x,col_name,value)


red_wire ['red_wire'] 2
white_wire ['white_wire'] 5

#is not None after
if x in col_name and value == storage[x][i] and value is not None:

            print(x,col_name,value)


red_wire ['red_wire'] 2
white_wire ['white_wire'] 5

您的源代码中有两个问题

0是有效值 在第一个版本中,您在条件中使用了
value
,就像在注释中所说的那样,
bool(0)=false
,因此您添加了指定
不是None
来管理存在0值的事实,并且根本没有值

列和值索引混合 在最终版本中,您使用循环中名为
i
的熊猫索引系统对列进行迭代:

对于x,zip中的i(storage.columns、storage.index):#正在数据帧中访问数据

但您可以使用此索引在相应的数据帧中查找值:

值==存储[x][i]

幸运的是,它适用于
红色线
白色线
,甚至
黑色线
适用于
框1

也许您希望确保该值属于相应的数据帧部分;在这种情况下,您可以通过以下方式更改源代码:

存储器[x]中的值。值

这样,您根本不需要检查if value if not None;您也不需要在
索引中迭代;您的源代码可以简化为:

storage.columns中x的
:
如果列中有x,则存储中的名称和值[x]。值:


如果它达到您的需要,请告诉我。

值不是None
-->
如果列名称中的x和值不是None
bool(0)->False
那么当
值==0
时,列名称和值中的
x是
False
。零是一个数学值。但计算机程序中的变量并非如此。您的标题表明您有权期望值为
0
的Python变量具有数学定义的行为。你不是。你只能期待语言定义的行为。计算机变量是理想数学实体的近似值,在许多方面与精确数学行为不同。谢谢。你说得对,我将编辑标题。@powerPixie我的答案对你有帮助吗?