小编alw*_*btc的帖子

是否有python函数可以使用未指定数量的参数?

可以有python函数,其中包含未指定数量的参数,例如

myfunc(a, b, c)

myfunc(a, b, c, d, e)
Run Code Online (Sandbox Code Playgroud)

哪两个都有效?

python arguments function unspecified

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

如何根据列表项对列表列表进行排序,列表项是python中表示为字符串的数字?

我有3个列表:

>>> a = ["12", "a"]
>>> b = ["123", "b"]
>>> c = ["4", "c"]
Run Code Online (Sandbox Code Playgroud)

我把它们放在一个新的列表中:

>>> d = [a,b,c]
Run Code Online (Sandbox Code Playgroud)

当我根据每个内部列表的第一项对它们进行排序时:

>>> sorted(d, key=itemgetter(0))
[['12', 'a'], ['123', 'b'], ['4', 'c']]
Run Code Online (Sandbox Code Playgroud)

但我想要:

[['4', 'c'], ['12', 'a'], ['123', 'b']]
Run Code Online (Sandbox Code Playgroud)

此外,第一项可能具有前导零:

>>> a = ["012", "a"]
>>> b = ["0123", "b"]
>>> c = ["04", "c"]
Run Code Online (Sandbox Code Playgroud)

再次,我希望以这种方式看到排序列表:

[['04', 'c'], ['012', 'a'], ['0123', 'b']]
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

python sorting string list

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

Python:如何以HH:MM格式区分2个日期?

我有2个日期时间对象:

a 
datetime.datetime(2013,2,11,15,35)

b
datetime.datetime(2013,2,11,18,55)
Run Code Online (Sandbox Code Playgroud)

差异是3:20

我怎么能在python中获得这个?

我做:

(b-a).seconds/3600
Run Code Online (Sandbox Code Playgroud)

但我得到3而不是3:20

python time date subtraction

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

Python:tkinter askyesno方法打开一个空窗口

我使用它从用户获得是/否但它打开一个空窗口:

from Tkinter import *
from tkMessageBox import *
if askyesno('Verify', 'Really quit?'):
    print "ok"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

而这个空窗口不会消失.我怎么能阻止这个?

这不起作用:

    Tk().withdraw()
    showinfo('OK', 'Select month')
    print "line 677"
    root = Tk()
    root.title("Report month")
    months = ["Jan","Feb","Mar"]
    sel_list = []
    print "line 682"

    def get_sel():
        sel_list.append(Lb1.curselection())
        root.destroy()

    def cancel():
        root.destroy()

    B = Button(root, text ="OK", command = get_sel)
    C = Button(root, text ="Cancel", command = cancel)
    Lb1 = Listbox(root, selectmode=SINGLE)

    for i,j in enumerate(months):
        Lb1.insert(i,j)


    Lb1.pack()
    B.pack()
    C.pack()
    print "line 702"
    root.mainloop()

    for i in …
Run Code Online (Sandbox Code Playgroud)

python user-interface dialog tkinter

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

2个笛卡儿积分在python中

你知道如何在python中循环100和200的笛卡尔积,例如:

for cartesian(100,200):
 print result

1,1
1,2
.
.
.
123,197
.
.
100,200
Run Code Online (Sandbox Code Playgroud)

python cartesian-product

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

如何在python中有条理地跳过for循环中的迭代步数?

我们有一个清单item_list,

item_list = ["a", "b", "XYZ", "c", "d", "e", "f", "g"]
Run Code Online (Sandbox Code Playgroud)

我们使用for循环遍历其项目,如果是item "XYZ",则跳过项目"c", "d", "e"并继续"f":

for item in item_list:
    if item == "XYZ":
       do_something()
       skip_3_items() ----> skip items "c", "d", "e"
    else:
        do_something_else()
Run Code Online (Sandbox Code Playgroud)

什么可能是实现这一目标的最pythonic方式?

python for-loop list

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

如何在python中的for循环中跳过函数内的迭代步骤?

功能:

def function_1(value):
    if value < 5:
        continue
    else:
        print "value: ", value
Run Code Online (Sandbox Code Playgroud)

环:

for value in xrange(10):
    function_1(value)
Run Code Online (Sandbox Code Playgroud)

for上面的循环中,如果value小于5,function_1将使for循环跳转到下一次迭代,否则它将打印该值.如何在循环内的函数的帮助下跳过迭代步骤,如上所述?

python iteration loops skip

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

运行python程序时出现“ SyntaxError:文件中非ASCII字符'\ xfe'”错误

虽然我包括

# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)

在python文件的第一行中,我不断

SyntaxError: Non-ASCII character '\xfe' in file C:\Users\user\PycharmProjects\my_project\my_script.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Run Code Online (Sandbox Code Playgroud)

我在Windows 7上使用PyCharm Community Edition。请提供帮助。

python windows encoding

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

如何检查python中字典内的内部字典中是否存在键?

有一个python字典:

a = {b:{c:{"x":1, "y":2, "z":3}}}
Run Code Online (Sandbox Code Playgroud)

我想知道是否a[b][c]["z"]存在了,但我不知道是否a[b][c]a[b]任何存在.

所以,如果我这样做:

if "z" in a[b][c]:
Run Code Online (Sandbox Code Playgroud)

我可能会得到一个"key c doesn't exist in a[b]""key b doesn't exist in a"错误.

在这种情况下如何正确检查[b] [c]中是否存在z?

python dictionary

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

如何根据python中的列表列表对列表进行排序?

我有一个清单:

a = ["x", "y", "z"]
Run Code Online (Sandbox Code Playgroud)

我有另一个列表列表:

b = [["y", 99], ["w", 65], ["z", 150]]
Run Code Online (Sandbox Code Playgroud)

所以,我想a按照b降序排列的值对项目进行排序,因此排序a应该如下:

sorted_a = ["z", "y", "x"]
Run Code Online (Sandbox Code Playgroud)

"x"是最后一个,因为它没有值b.

我想知道实现这一目标的最快方法,因为我的真实清单很大.

python sorting list

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