如果我有2个dicts如下:
d1 = {('unit1','test1'):2,('unit1','test2'):4}
d2 = {('unit1','test1'):2,('unit1','test2'):''}
Run Code Online (Sandbox Code Playgroud)
为了"合并"它们:
z = dict(d1.items() + d2.items())
z = {('unit1','test1'):2,('unit1','test2'):''}
Run Code Online (Sandbox Code Playgroud)
工作良好.另外要做什么,如果我想比较两个词典的每个值,如果d1中的值为空/无/',则只将d2更新为d1?
[编辑] 问题:当将d2更新为d1时,当存在相同的键时,我想仅保持数值(来自d1或d2)而不是空值.如果两个值都为空,那么保持空值没有问题.如果两者都有值,则应保留d1值.:)(lota if-else ..我会在此期间尝试自己)
即
d1 = {('unit1','test1'):2,('unit1','test2'):8,('unit1','test3'):''}
d2 = {('unit1','test1'):2,('unit1','test2'):'',('unit1','test3'):''}
#compare & update codes
z = {('unit1','test1'):2,('unit1','test2'):8, ('unit1','test2'):''} # 8 not overwritten by empty.
Run Code Online (Sandbox Code Playgroud)
请帮忙建议.
谢谢.
我有一个字典列表如下:
list = [ { 'a':'1' , 'b':'2' , 'c':'3' }, { 'd':'4' , 'e':'5' , 'f':'6' } ]
Run Code Online (Sandbox Code Playgroud)
如何将列表中每个字典的值转换为int/float?
所以它变成:
list = [ { 'a':1 , 'b':2 , 'c':3 }, { 'd':4 , 'e':5 , 'f':6 } ]
Run Code Online (Sandbox Code Playgroud)
谢谢.
我有几个csv文件,我想作为excel工作簿(xls/xlsx)中的新工作表转储.我该如何实现这一目标?
谷歌搜索并发现'pyXLwriter',但似乎该项目已停止.虽然我尝试'pyXLwriter'想知道有没有替代品/建议/模块?
非常感谢.
[编辑]
这是我的解决方案:(任何人都有更精简,更多的pythonic解决方案?做评论.thx)
import glob
import csv
import xlwt
import os
wb = xlwt.Workbook()
for filename in glob.glob("c:/xxx/*.csv"):
(f_path, f_name) = os.path.split(filename)
(f_short_name, f_extension) = os.path.splitext(f_name)
ws = wb.add_sheet(str(f_short_name))
spamReader = csv.reader(open(filename, 'rb'), delimiter=',',quotechar='"')
row_count = 0
for row in spamReader:
for col in range(len(row)):
ws.write(row_count,col,row[col])
row_count +=1
wb.save("c:/xxx/compiled.xls")
print "Done"
Run Code Online (Sandbox Code Playgroud) 假如我有以下四个功能:
def foo():
subprocess.Popen('start /B someprogramA.exe', shell=True)
def bar():
subprocess.Popen('start /B someprogramB.exe', shell=True)
def foo_kill():
subprocess.Popen('taskkill /IM someprogramA.exe')
def bar_kill():
subprocess.Popen('taskkill /IM someprogramB.exe')
Run Code Online (Sandbox Code Playgroud)
如何将foo和bar功能交替运行,比如30分钟?含义:第一个30分钟 - 跑步foo,第二个30分钟 - 跑步bar,第三个30分钟 - 跑步foo,依此类推.每次新的运行应该"杀死"前一个线程/ func.
我有一个倒数计时器线程,但不知道如何"交替"这些功能.
class Timer(threading.Thread):
def __init__(self, minutes):
self.runTime = minutes
threading.Thread.__init__(self)
class CountDownTimer(Timer):
def run(self):
counter = self.runTime
for sec in range(self.runTime):
#do something
time.sleep(60) #editted from 1800 to 60 - sleeps for a minute
counter -= 1
timeout=30
c=CountDownTimer(timeout)
c.start()
Run Code Online (Sandbox Code Playgroud)
编辑:我的解决方案与尼古拉斯奈特的投入......
import …Run Code Online (Sandbox Code Playgroud) 是否有类似于normplotMATLAB 的python等效函数?也许在matplotlib?
MATLAB语法:
x = normrnd(10,1,25,1);
normplot(x)
Run Code Online (Sandbox Code Playgroud)
得到:

我已经尝试使用matplotlib&numpy模块来确定数组中值的概率/百分位数,但输出图y轴比例与MATLAB的图相比是线性的.
import numpy as np
import matplotlib.pyplot as plt
data =[-11.83,-8.53,-2.86,-6.49,-7.53,-9.74,-9.44,-3.58,-6.68,-13.26,-4.52]
plot_percentiles = range(0, 110, 10)
x = np.percentile(data, plot_percentiles)
plt.plot(x, plot_percentiles, 'ro-')
plt.xlabel('Value')
plt.ylabel('Probability')
plt.show()
Run Code Online (Sandbox Code Playgroud)
得到:

另外,如何在第一个情节中调整尺度?
谢谢.
我有一本字典如下:
dictA = { ('unit1','test1') : 'alpha' , ('unit1','test2') : 'beta', ('unit2','test1') : 'alpha', ('unit2','test2') : 'gamma' , ('unit3','test1') : 'delta' , ('unit3','test2') : 'gamma' }
Run Code Online (Sandbox Code Playgroud)
如何计算每个测试的重复值的数量,与单位无关?
即
'test1'中有2x'alpha',1x'delta'
'test2'中有1x'beta',2x'gamma'
有什么投入?
非常感谢.
我有几个数据集(分布)如下:
set1 = [1,2,3,4,5]
set2 = [3,4,5,6,7]
set3 = [1,3,4,5,8]
Run Code Online (Sandbox Code Playgroud)
如何使用上面的数据集绘制散点图,y轴是概率(即集合中的分布的百分位数:0%-100%),x轴是数据集名称?在JMP中,它被称为"分位数图".
像图像附加的东西:

请教育.谢谢.
[编辑]
我的数据在csv中是这样的:

使用JMP分析工具,我能够绘制概率分布图(QQ图/正常分位图如下图所示):

我相信Joe Kington几乎解决了我的问题但是,我想知道如何将原始csv数据处理成probalility或百分位数的数组.
我这样做是为了在Python中自动化一些统计分析,而不是依靠JMP进行绘图.
另一个列表字典问题.
我有一个dict如下,列表中的单位名称和测试名称:
dictA = {('unit1', 'test1'): 10, ('unit2', 'test1'): 78, ('unit2', 'test2'): 2, ('unit1', 'test2'): 45}
units = ['unit1', 'unit2']
testnames = ['test1','test2']
Run Code Online (Sandbox Code Playgroud)
我们如何在测试名中找到每个测试的最大值:
我尝试如下:
def max(dict, testnames_array):
maxdict = {}
maxlist = []
temp = []
for testname in testnames_array:
for (xunit, xtestname), value in dict.items():
if xtestname == testname:
if not isinstance(value, str):
temp.append(value)
temp = filter(None, temp)
stats = corestats.Stats(temp)
k = stats.max() #finds the max of a list using another module
maxdict[testname] = k
maxlist.append(maxdict)
maxlist.insert(0,{'Type':'MAX'}) …Run Code Online (Sandbox Code Playgroud) 我有一个列表如下:
lst = [-1.33, '', -1.33, -1.33 -2.62, 0, -2.66, 1.41, 0, 0, 1.40, '', 1.37, 0]
Run Code Online (Sandbox Code Playgroud)
列表中有两个空元素'',其中包含多个零和浮点数.
如何删除空元素但保留零?如下...
lst2 = [-1.33, -1.33, -1.33 -2.62, 0, -2.66, 1.41, 0, 0, 1.40, 1.37, 0]
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
lst2 = filter(None, lst)
Run Code Online (Sandbox Code Playgroud)
和
lst2 = [x for x in lst if x if isinstance(x,str) == False]
Run Code Online (Sandbox Code Playgroud)
但是,它也删除了零.
我知道浮点数返回12位小数,请忽略例如目的.
有什么建议?谢谢.
如何将以下字典打印到csv文件中?
maxDict = {'test1': ('alpha', 2), 'test2': ('gamma', 2)}
Run Code Online (Sandbox Code Playgroud)
因此,输出CSV如下所示:
test1, alpha, 2
test2, gamma, 2
Run Code Online (Sandbox Code Playgroud) 我有一些'列表'包含很少的词典 - 比如3个词典.3个字典如下:
lstone = [{'dc_test': 1}, {'ac_test':2}, {'con_test':3}]
lsttwo = [{'dc_test': 4}, {'ac_test':5}, {'con_test':6}]
Run Code Online (Sandbox Code Playgroud)
如何创建新列表如下:
newlistone = ['dc_test',1,4]
newlisttwo = ['ac_test',2,5]
newlistthree = ['con_test',3,6]
Run Code Online (Sandbox Code Playgroud)
我的目标是编写一个新的csv文件,以便它显示如下:
dc_test,1,4
ac_test,2,5
con_test,3,5
Run Code Online (Sandbox Code Playgroud) 如果我有一个字典如下(有一些列表):
units = ['a','b']
nums = ['1','2']
ratios = ['alpha', 'beta']
d = {'a_1_alpha':4, 'a_1_beta' :1, 'a_2_alpha' :2, 'a_2_beta': 3, 'b_1_alpha':2}
Run Code Online (Sandbox Code Playgroud)
我如何从一本新词典:
即
new_d = { ('1','alpha'): 6, ('1','beta'): 1, ('2','alpha'): 2, ('2','beta'): 3}
Run Code Online (Sandbox Code Playgroud)
我有以下代码,但似乎不对.
new_d = {}
for num in nums:
for ratio in ratios:
for k,v in d.items():
if ratio in k:
try:
oldval = dict[num,ratio]
except:
oldval = 0
new_d[(num,ratio)] = oldval + v
for p,q in new_d.items():
print p,q
Run Code Online (Sandbox Code Playgroud)
请帮助评论/建议.谢谢 :).