简体   繁体   English

从列表中为Python字典分配和显示值的正确方法是什么?

[英]What is the proper way to assign and display values to a Python dictionary from a list?

I am reading a list of measured values (Results) from a piece of equipment, 20 measurements at a time. 我正在读取一台设备的测量值(结果)列表,一次测量20次。 I need to break these down into 2 separate groups and place them in 2 different dictionaries, do some Pass/Fail calculations, and then display them in a GUI. 我需要将它们分为2个单独的组,并将它们放在2个不同的词典中,进行一些“通过/失败”计算,然后在GUI中显示它们。 What I have so far is this: 我到目前为止所拥有的是:

import sys

from collections import OrderedDict

from tkinter import *

x=[]
x1=[]

Results = ([1.50229780e+00, 4.94528400e-01,-4.74700000e-04,-3.42900000e- 
04,-2.24200000e-04,-1.71400000e-04,-1.71400000e-04,-1.84600000e-04,
-1.31900000e-04,-1.45100000e-04,-6.59000000e-05, 1.32000000e-05,
0.00000000e+00,-1.32000000e-05,-1.32000000e-05,-3.96000000e-05,
-1.32000000e-05,-6.59000000e-05,-3.96000000e-05,-6.59000000e-05])

# DUT1, DUT2 are dictionary....  Access by 'CH1' or 'CH9'....


DUT1 = OrderedDict()
DUT1['CH1']=0
DUT1['CH2']=0
DUT1['CH3']=0
DUT1['CH4']=0
DUT1['CH5']=0
DUT1['CH6']=0
DUT1['CH7']=0
DUT1['CH8']=0
DUT1['CH9']=0
DUT1['CH10']=0

DUT2 = OrderedDict()
DUT2['CH1']=0
DUT2['CH2']=0
DUT2['CH3']=0
DUT2['CH4']=0
DUT2['CH5']=0
DUT2['CH6']=0
DUT2['CH7']=0
DUT2['CH8']=0
DUT2['CH9']=0
DUT2['CH10']=0


i=0

for key in DUT1:
    DUT1[key]=Results[i]
    i=i+1

i=len(DUT1)

for key in DUT2:
    DUT2[key]=Results[i]
    i=i+1

root = Tk()

for key,items in DUT1.items():
    y=("%s = %s\n" % (key,items))
    sys.stdout.write("%s %s\n" % (key,items));sys.stdout.flush()
    x.append(y)

for key,items in DUT2.items():
    y1=("%s = %s\n" % (key,items))
    sys.stdout.write("%s %s\n" % (key,items));sys.stdout.flush()
    x1.append(y1)

Label(root, text = "DUT1 Results").grid(row=0,ipadx = 10,ipady = 10)
Label(root, text = x ).grid(row=1,ipadx = 10,ipady =10)

Label(root, text = "DUT2 Results").grid(row=2,ipadx = 10,ipady = 10)
Label(root, text = x1 ).grid(row=3,ipadx = 10,ipady =10)

mainloop()

What I get for an output (printed to the console) is: 我得到的输出(打印到控制台)是:

CH1 1.5022978
CH2 0.4945284
CH3 -0.0004747
CH4 -0.0003429
CH5 -0.0002242
CH6 -0.0001714
CH7 -0.0001714
CH8 -0.0001846
CH9 -0.0001319
CH10 -0.0001451

CH1 -6.59e-05
CH2 1.32e-05
CH3 0.0
CH4 -1.32e-05
CH5 -1.32e-05
CH6 -3.96e-05
CH7 -1.32e-05
CH8 -6.59e-05
CH9 -3.96e-05
CH10 -6.59e-05

Why is DUT1 in non-scientific notation, but DUT2 is? 为什么DUT1是非科学的表示法,而DUT2是?

And when the Labels are updated, I get: 当标签更新时,我得到:

{CH1 = 1.5022978
}{CH2 = 0.4945284
}{CH3 = -0.0004747
}{CH4 = -0.0003429
}{CH5 = -0.0002242
}{CH6 = -0.0001714
}{CH7 = -0.0001714
}{CH8 = -0.0001846
}{CH9 = -0.0001319
}{CH10 = -0.0001451
}

{CH1 = -6.59e-05
}{CH2 = 1.32e-05
}{CH3 = 0.0
}{CH4 = -1.32e-05
}{CH5 = -1.32e-05
}{CH6 = -3.96e-05
}{CH7 = -1.32e-05
}{CH8 = -6.59e-05
}{CH9 = -3.96e-05
}{CH10 = -6.59e-05
}

Why are the brackets being printed? 为什么要打印括号?

Python 3 prints in scientific notation numbers < 1e-4. Python 3以科学计数法数字<1e-4打印。 Example: 例:

print(1e-4) #out: 0.0001
print(1e-5) #out: 1e-05

You can enforce a format using formatted printing 您可以使用格式化打印来强制使用格式

# for non-scientific:
print("{0:0.6f}".format(1e-5)) #out: 0.000010
# forcing scientific notation:
print('%.2E' % 40800000000.00000000000000) #out: 4.08E+10
print('%.4E' % 1e-2) #out: 1.0000E-02

scientific notation example taken from this answer . 这个答案中得出的科学符号例子。

Here is a working version of your code that I think does what you want. 这是您认为需要的代码的工作版本。 It creates a formatted string, via concatenation in a for loop, before creating a Label to put it. 它通过在for循环中串联来创建格式化的字符串,然后再创建放置它的Label。

import sys
from collections import OrderedDict
from tkinter import *

x=[]
x1=[]

Results = ([1.50229780e+00, 4.94528400e-01,-4.74700000e-04,-3.42900000e-04,-2.24200000e-04,-1.71400000e-04,-1.71400000e-04,-1.84600000e-04,-1.31900000e-04,-1.45100000e-04,-6.59000000e-05, 1.32000000e-05,0.00000000e+00,-1.32000000e-05,-1.32000000e-05,-3.96000000e-05,-1.32000000e-05,-6.59000000e-05,-3.96000000e-05,-6.59000000e-05])

# DUT1, DUT2 are dictionary....  Access by 'CH1' or 'CH9'....

DUT1 = OrderedDict()
DUT1['CH1']=0
DUT1['CH2']=0
DUT1['CH3']=0
DUT1['CH4']=0
DUT1['CH5']=0
DUT1['CH6']=0
DUT1['CH7']=0
DUT1['CH8']=0
DUT1['CH9']=0
DUT1['CH10']=0

DUT2 = OrderedDict()
DUT2['CH1']=0
DUT2['CH2']=0
DUT2['CH3']=0
DUT2['CH4']=0
DUT2['CH5']=0
DUT2['CH6']=0
DUT2['CH7']=0
DUT2['CH8']=0
DUT2['CH9']=0
DUT2['CH10']=0


i=0

for key in DUT1:
    DUT1[key]=Results[i]
    i=i+1

i=len(DUT1)

for key in DUT2:
    DUT2[key]=Results[i]
    i=i+1

root = Tk()

for key,items in DUT1.items():
    y=("%s = %s\n" % (key,items))
    sys.stdout.write("%s %.4E\n" % (key,items));sys.stdout.flush()
    x.append(y)

for key,items in DUT2.items():
    y1=("%s = %s\n" % (key,items))
    sys.stdout.write('%s %.4E\n' % (key,items));sys.stdout.flush()
    x1.append(y1)

Label(root, text = "DUT1 Results").grid(row=0,ipadx = 10,ipady = 10)
s = ''
for key,items in DUT1.items():
    s += '%s %.4E\n' % (key,items)
Label(root, text = s ).grid(row=1,ipadx = 10,ipady =10)

Label(root, text = "DUT2 Results").grid(row=2,ipadx = 10,ipady = 10)
s1 = ''
for key,items in DUT2.items():
    s1 += '%s %.4E\n' % (key,items)
Label(root, text = s1 ).grid(row=3,ipadx = 10,ipady =10)

mainloop()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM