我一直在学习,工作和玩Python一年半.随着生物学家慢慢转向生物信息学,这种语言一直是我在实验室做出的所有主要贡献的核心.我或多或少地爱上了Python允许我表达漂亮解决方案的方式,以及语言的语义,允许从思想到可行代码的这种自然流动.
我想知道的是你对我在这个论坛或其他论坛中很少见到的一个问题的答案.对于那些正在改进Python的人来说,这个问题对我来说似乎很重要,但是他想知道他的下一步应该是什么.
让我总结一下我不想先问的问题;)
我想知道你的意见,是:
您将向Python熟练人员推荐哪些步骤,从学徒到大师状态(随意停止,无论您的专业知识在哪里),以便一个人不断改进,成为更好,更好的Python编码器,一步一步.SO上的一些人几乎看起来值得为他们的Python实力而敬拜,请赐教:)
我会喜欢的那种答案(但随意给读者带来惊喜:P)的格式或多或少是这样的:
我非常关心在不同的阶段知道你应该注意什么的意见,以便不断进步(当然有适当的努力).如果您来自特定的专业领域,请在此字段中讨论您认为合适的路径.
编辑:感谢您的好评,我又回到了Python改进的轨道!我真的很感激!
感谢SO的一些优秀人员,我发现了collections.defaultdict可见性和速度提供的可能性.我已经把它们用于成功.
现在我想实现三个级别的词典,两个顶级词典defaultdict和最低词典int.我找不到合适的方法来做到这一点.这是我的尝试:
from collections import defaultdict
d = defaultdict(defaultdict)
a = [("key1", {"a1":22, "a2":33}),
("key2", {"a1":32, "a2":55}),
("key3", {"a1":43, "a2":44})]
for i in a:
d[i[0]] = i[1]
Run Code Online (Sandbox Code Playgroud)
现在这可行,但以下,这是所需的行为,不会:
d["key4"]["a1"] + 1
Run Code Online (Sandbox Code Playgroud)
我怀疑我应该声明第二级defaultdict是类型的int,但我没有找到在哪里或如何这样做.
我defaultdict首先使用的原因是避免为每个新密钥初始化字典.
还有更优雅的建议吗?
谢谢pythoneers!
我需要计算在Python combinatorials(NCR),但无法找到的功能做在math,numpy或stat 图书馆.类似于类型函数的东西:
comb = calculate_combinations(n, r)
Run Code Online (Sandbox Code Playgroud)
我需要可能的组合数量,而不是实际的组合,所以itertools.combinations我不感兴趣.
最后,我想避免使用阶乘,因为我将计算组合的数字可能变得太大而且阶乘将变得非常可怕.
这似乎是一个非常容易回答的问题,但是我被淹没在关于生成所有实际组合的问题中,这不是我想要的.:)
非常感谢
在使用函数时,我希望确保变量的类型符合预期.怎么做对了?
这是一个假冒函数的示例,在继续其角色之前尝试执行此操作:
def my_print(begin, text, end):
"""Print 'text' in UPPER between 'begin' and 'end' in lower
"""
for i in (begin, text, end):
assert isinstance(i, str), "Input variables should be strings"
out = begin.lower() + text.upper() + end.lower()
print out
def test():
"""Put your test cases here!
"""
assert my_print("asdf", "fssfpoie", "fsodf")
assert not my_print("fasdf", 33, "adfas")
print "All tests passed"
test()
Run Code Online (Sandbox Code Playgroud)
断言是正确的方法吗?我应该使用try/except吗?
此外,我的断言测试集似乎不能正常工作:S
谢谢pythoneers
我正在尝试Bjarne Stroustrup的C++书籍第三版.在实现一个相当简单的函数时,我得到以下编译时错误:
error: ISO C++ forbids comparison between pointer and integer
Run Code Online (Sandbox Code Playgroud)
可能是什么导致了这个?这是代码.错误在于if:
#include <iostream>
#include <string>
using namespace std;
bool accept()
{
cout << "Do you want to proceed (y or n)?\n";
char answer;
cin >> answer;
if (answer == "y") return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在尝试在linux中运行我的第一个c ++程序(linux mint 8).我使用gcc或g ++,都有同样的问题:编译器找不到我试图导入的库.
我怀疑我应该在工作文件夹中复制iostream.h文件(我不知道在哪里查找),将我的文件移动到其他地方编译或使用某种选项.
谢谢你的建议.
这是gcc命令,c ++代码和错误消息:
gcc -o addition listing2.5.c
Run Code Online (Sandbox Code Playgroud)
.
#include <iostream.h>
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
cout << "Resultat : " << Addition(2, 4) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
.
listing2.5.c:1:22: error: iostream.h: No such file or directory
listing2.5.c: In function ‘main’:
listing2.5.c:10: error: ‘cout’ undeclared (first use in this function)
listing2.5.c:10: error: (Each undeclared identifier is reported only once
listing2.5.c:10: error: for each function it …Run Code Online (Sandbox Code Playgroud) 以下代码返回: error: expected unqualified-id before ‘for’
我找不到导致错误的原因.谢谢您的帮助!
#include<iostream>
using namespace std;
const int num_months = 12;
struct month {
string name;
int n_days;
};
month *months = new month [num_months];
string m[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int n[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int i=0; i<num_months; i++) {
// will initialize the months
}
int main() {
// will print name[i]: days[i] …Run Code Online (Sandbox Code Playgroud) 我在Linux下从命令行(Bash)启动Python脚本.我需要打开Python,导入一个模块,然后解释代码行.然后控制台必须保留在Python中(不要退出).我怎么做?
我尝试过像这样的别名:
alias program="cd /home/myname/programs/; python; import module; line_of_code"
Run Code Online (Sandbox Code Playgroud)
但这只会启动python并且命令不会执行(没有模块导入,没有处理代码行).
如果我需要在脚本执行后保持Python打开(不退出),那么这样做的正确方法是什么?非常感谢!
我需要在Python中进行二项式测试,允许计算10000的数量级别的'n'.
我已经使用scipy.misc.comb实现了一个快速的binomial_test函数,但是,它在n = 1000附近非常有限,我想因为它在计算阶乘或组合本身时达到了最大可表示的数字.这是我的功能:
from scipy.misc import comb
def binomial_test(n, k):
"""Calculate binomial probability
"""
p = comb(n, k) * 0.5**k * 0.5**(n-k)
return p
Run Code Online (Sandbox Code Playgroud)
我怎么能使用本机python(或numpy,scipy ...)函数来计算二项式概率?如果可能的话,我需要scipy 0.7.2兼容代码.
非常感谢!
我在python中有一个小程序,它包含一个.py文件和一个程序使用的数据文件目录.
我想知道为Linux上具有管理员权限的用户创建安装过程的正确方法,以便他可以在他的系统上安装程序并从命令行使用它,包括选项和参数.编辑:我遇到问题的部分是让程序安装后检索包含在"数据"子文件夹中的数据文件.
安装可执行程序文件的安装脚本/usr/local/bin和数据文件夹/usr/share/my_program/data是否是可接受的解决方案?就像是:
#!/bin/bash
# Launch with sudo
chmod +x program.py
cp program.py /usr/local/bin
cp -r data /usr/share/my_program
echo Installation complete
Run Code Online (Sandbox Code Playgroud)
现在,为了做到这一点,我必须在程序中假设数据文件将进入/usr/share/my_program/data.但我也会让用户选择使用该程序而无需安装它.然后我将不得不假设数据相对于可执行程序文件是'./data'.我该如何解决这个问题?我可以想到几个方面,但我的感觉是,我正在创造一个应该有一个明确和正确答案的混乱.
目前,我正在考虑使用try except子句:
try:
find data from /usr/share/my_program && set path accordingly
except:
set path to the data as './data'
Run Code Online (Sandbox Code Playgroud)
再一次,我觉得这有点令人费解.你将如何进行安装?
非常感谢
编辑:解决方案已采用
根据这个问题的答案,以及FakeRainBrigand提出的问题(如何知道Python中运行脚本的路径?),我创建了一个如下所示的安装脚本:
#!/bin/bash
mkdir /usr/share/my_program
chmod +x my_program.py
cp my_program.py /usr/local/bin
cp -r data /usr/share/my_program
echo Installation completed
Run Code Online (Sandbox Code Playgroud)
并在我的程序中添加了以下代码:
if os.path.dirname(__file__) == "/usr/local/bin":
DATA_PATH = "/usr/share/my_program/data"
elif os.path.dirname(__file__) …Run Code Online (Sandbox Code Playgroud) python ×7
c++ ×3
linux ×2
alias ×1
assert ×1
bash ×1
combinations ×1
dictionary ×1
gcc ×1
installation ×1
nested ×1
statistics ×1
testing ×1