小编c3n*_*ury的帖子

尝试在Python中使用带有列表的'pop'方法时出错

我正在尝试编写一个简单的过程,它只保留一个输入列表(包括嵌套列表).

程序本身 -

def is_list(p):
    return isinstance(p, list)

def deep_reverse(input):
    result = []
    if len(input) == 0:
        return []
    for element in input:
        if is_list(element) == True:
            deep_reverse(element)
        else:
            appended = input.pop
            result.append(appended)
            print result
    return result
Run Code Online (Sandbox Code Playgroud)

我正在输入什么来执行程序 -

p = [1, [2, 3, [4, [5, 6]]]]
print deep_reverse(p)
print p
Run Code Online (Sandbox Code Playgroud)

结果 -

[built-in method pop of list object at 0xb752b0cc]
[built-in method pop of list object at 0xb7529c6c]
[built-in method pop of list object at 0xb7529cec]
[built-in method pop …
Run Code Online (Sandbox Code Playgroud)

python list

3
推荐指数
1
解决办法
1817
查看次数

优化代码以找到1-20之间所有整数可整除的最小数字

我目前正试图解决问题"从1到20的所有数字可以被整除的最小正数是多少?"

到目前为止,我编写了一些似乎有用的内容,但需要很长时间.此外,我需要在if中使用大量的'和'语句,这看起来效率不高也不专业.

我该怎么做才能优化这些代码并使其更整洁?

number = 1
result = 0

def divide(candidate):
    if candidate % 2 == 0 and candidate % 3 == 0 and candidate % 4 == 0 and candidate % 5 == 0 and candidate % 6 == 0 and candidate % 7 == 0 and candidate % 8 == 0 and candidate % 9 == 0 and candidate % 10 == 0 and candidate % 11 == 0 and candidate % 12 == 0 and candidate % …
Run Code Online (Sandbox Code Playgroud)

python math divide

1
推荐指数
1
解决办法
4709
查看次数

如何生成指定长度的星号字符串?

我怎么会产生指定长度的星号(所以我可以用它旁边一个数组项的.长度属性)的字符串?

vb.net string visual-studio-2010

1
推荐指数
1
解决办法
1188
查看次数

如何在循环中使用'if'?

我正试图解决一个问题,但是我觉得我得到的循环在某个地方有一个缺失的链接......

我已经给出了简报 - "现在编写一个程序,计算所需的最低固定月付款,以便在12个月内偿还信用卡余额."

从本质上讲,我所做的远远不是代码,它会取一个基本价值(例如10),从信用卡余额(考虑到利息)中拿走它,如果是它的总月数将余额变为负数(例如已付清)高于12,它会增加'minmonth'(每月还清金额),直到月数等于或低于12.

提前道歉,我实际上只学习了2天的Python!

我哪里错了?

balance = float(raw_input('Enter the outstanding balance on your creditcard: '))
interest = float(raw_input('Enter the annual credit card interest rate as a decimal:     '))

minmonth = 10
months = 0
monthlyinterest =  interest / 12

while(balance > 0):
    balance = balance * (1 + monthlyinterest) - minmonth
    months = months + 1

    if(months > 12):
        months = 0
        minmonth = minmonth + 10

else:
    print 'RESULT!'
    print 'Total amount to pay per month would …
Run Code Online (Sandbox Code Playgroud)

python loops

0
推荐指数
1
解决办法
690
查看次数

标签 统计

python ×3

divide ×1

list ×1

loops ×1

math ×1

string ×1

vb.net ×1

visual-studio-2010 ×1