说我有一个项目列表:
x = [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
我需要为每个项目执行一些功能.在某种情况下,我需要返回一个项目的索引.
哪种方式最好,效率最高?
for item in list:
....
Run Code Online (Sandbox Code Playgroud)
要么
for i in range(len(list)):
....
Run Code Online (Sandbox Code Playgroud) 我在Coding Bat做了一些练习问题,遇到了这个问题..
Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.
lone_sum(1, 2, 3) ? 6
lone_sum(3, 2, 3) ? 2
lone_sum(3, 3, 3) ? 0
Run Code Online (Sandbox Code Playgroud)
我的解决方案如下.
def lone_sum(a, b, c):
sum = a+b+c
if a == b:
if a == c:
sum -= 3 * a
else:
sum -= 2 * a
elif b == …Run Code Online (Sandbox Code Playgroud) 我正在使用面板设计多页窗体.
我正在显示登录表单并验证按钮单击,并希望隐藏登录面板并显示主面板.
但是,当我单击按钮时,登录面板会消失,但主面板不会出现.由于没有显示任何内容,表单窗口缩小到最小化/最大化/关闭按钮.
这是按钮的代码:
private void btn_login_Click(object sender, EventArgs e)
{
if (pwdBox.Text == optopwd)
{
MessageBox.Show("Good Morning!!");
loginpanel.Visible = false;
mainpanel.Visible = true;
}
else
{
MessageBox.Show("Incorrect password!");
}
pwdBox.Text = "";
}
Run Code Online (Sandbox Code Playgroud)
请让我知道我错过/误解了什么.谢谢!
编辑:屏幕截图:登录屏幕:http: //img641.imageshack.us/img641/9310/loginscreenj.jpg
[编辑]更改return 0为return。Beaa Python n00b的副作用。:)
我正在定义一个函数,正在执行约20行处理。在处理之前,我需要检查是否满足特定条件。如果是这样,那么我应该绕过所有处理。我已经用这种方式定义了函数。
def test_funciton(self,inputs):
if inputs == 0:
<Display Message box>
return
<20 line logic here>
Run Code Online (Sandbox Code Playgroud)
请注意,20行逻辑不返回任何值,并且我不使用第一个“ if”中返回的0。
我想知道这是否比使用以下类型的代码更好(就性能,可读性或任何其他方面而言),因为上述方法对我来说很好,因为它减少了一个缩进:
def test_function(self,inputs):
if inputs == 0:
<Display Message box>
else:
<20 line logic here>
Run Code Online (Sandbox Code Playgroud)