小编Gun*_*nnm的帖子

为什么变量=对象不像变量=数字那样工作

这些变量赋值按我的预期工作:

>>> a = 3
>>> b = a
>>> print(a, b)
(3, 3)
>>> b=4
>>> print(a, b)
(3, 4)
Run Code Online (Sandbox Code Playgroud)

但是,这些分配的行为有所不同:

>>> class number():
...     def __init__(self, name, number):
...         self.name = name
...         self.number = number
... 
>>> c = number("one", 1)
>>> d = c
>>> print(c.number, d.number)
(1, 1)
>>> d.number = 2
>>> print(c.number, d.number)
(2, 2)
Run Code Online (Sandbox Code Playgroud)

与示例不同,为什么是c相同的?如何在类示例中执行类似的操作?也就是说,复制对象,然后更改它的一部分(这不会影响我借用属性的对象)?d(a, b)(a, b)(c, d)

python variables object python-2.7 python-3.x

19
推荐指数
5
解决办法
1831
查看次数

为什么'the'在追求之后继续存在?

这段代码中发生了一些奇怪的事:

fh = open('romeo.txt', 'r')
lst = list()

for line in fh:
    line = line.split()
    for word in line:
        lst.append(word)

for word in lst:
    numberofwords = lst.count(word)
    if numberofwords > 1:
        lst.remove(word)

lst.sort()

print len(lst)
print lst
Run Code Online (Sandbox Code Playgroud)

romeo.txt取自http://www.pythonlearn.com/code/romeo.txt

结果:

27
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'the', 'through', 'what', 'window', 'with', 'yonder']
Run Code Online (Sandbox Code Playgroud)

如你所见,有两个'the'.这是为什么?我可以再次运行这部分代码:

for word in lst:
    numberofwords = lst.count(word)
    if numberofwords > 1: …
Run Code Online (Sandbox Code Playgroud)

python string python-2.7 python-3.x

8
推荐指数
2
解决办法
413
查看次数

在 Python 中调整大图像大小的 CPU 效率最高的方法是什么

我正在寻找调整图像大小的最有效方法。如果图像相对较小(例如 3000x2000),PIL 效果很好,但如果分辨率很大(16000x12000),则需要很长时间来处理。图像不必看起来很漂亮我正在调整它们的大小以进行比较以找到带有 nrmse 的图像副本。

from PIL import Image

img1 = Image.open("img1.jpg")
img2 = Image.open("img2.jpg")

print img1.size
print img2.size

# add width to height to see which resolution is bigger
im1s = img1.size[0] + img1.size[1]
im2s = img2.size[0] + img2.size[1]

# if both images are bigger than 3000 pixels make them smaller for comparison
if im1s > 3000 and im2s > 3000:
    print("Width and height of both images is bigger than 3000 pixels resizing them for easier comparison")
    im1_resize = …
Run Code Online (Sandbox Code Playgroud)

python image python-imaging-library

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

为什么time()低于0.25会在Python中跳过动画?

此代码按预期工作.输出:

Loading 
Loading.
Loading..
Loading...
Run Code Online (Sandbox Code Playgroud)

码:

done = False
count = 0

while not done:
    print '{0}\r'.format("Loading"),
    time.sleep(0.25)
    print '{0}\r'.format("Loading."),
    time.sleep(0.25)
    print '{0}\r'.format("Loading.."),
    time.sleep(0.25)
    print '{0}\r'.format("Loading..."),
    time.sleep(0.25)
    count += 1
    if count == 5:
        done = True
Run Code Online (Sandbox Code Playgroud)

而这段代码没有.输出:

Loading.
Loading...
Run Code Online (Sandbox Code Playgroud)

码:

done = False
count = 0

while not done:
    print '{0}\r'.format("Loading"),
    time.sleep(0.125)
    print '{0}\r'.format("Loading."),
    time.sleep(0.125)
    print '{0}\r'.format("Loading.."),
    time.sleep(0.125)
    print '{0}\r'.format("Loading..."),
    time.sleep(0.125)
    count += 1
    if count == 5:
        done = True
Run Code Online (Sandbox Code Playgroud)

为什么时间函数似乎跳过每一个print语句,如果它低于0.25?

python time stdout python-2.7

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

如何在python中使用ctypes获取Windows窗口名称

我尝试通过长对象的句柄获取Windows窗口标题名称和pids.我的代码有效,但它有问题.当我应该获得10个或更多时,我只获得4个窗口标题.任何人都可以帮助并告诉我如何修复此代码?我认为问题在于我如何转换长对象(我不太了解它们,以及一般的ctypes).

from __future__ import print_function
from ctypes import *

psapi = windll.psapi
titles = []


# get window title from pid
def gwtfp():
    max_array = c_ulong * 4096
    pProcessIds = max_array()
    pBytesReturned = c_ulong()

    psapi.EnumProcesses(byref(pProcessIds),
                        sizeof(pProcessIds),
                        byref(pBytesReturned))

    # get the number of returned processes
    nReturned = pBytesReturned.value/sizeof(c_ulong())
    pidProcessArray = [i for i in pProcessIds][:nReturned]
    print(pidProcessArray)
    #
    EnumWindows = windll.user32.EnumWindows
    EnumWindowsProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int))
    GetWindowText = windll.user32.GetWindowTextW
    GetWindowTextLength = windll.user32.GetWindowTextLengthW
    IsWindowVisible = windll.user32.IsWindowVisible


    for process in pidProcessArray:
        #print("Process PID %d" …
Run Code Online (Sandbox Code Playgroud)

python windows ctypes long-integer psapi

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

tkinter滚动条在窗口中不起作用

我刚开始使用tkinter,我不知道如何使滚动条工作.我在stackoverflow上检查了许多关于tkinter的不同线程,我对我究竟需要做什么感到困惑.

我已经添加了滚动条,但它没有滚动任何内容(注释#*******Scrollbar*******).我希望它从内容框架滚动文本.

已经在#*******词典数据*******中插入了lorem ipsum.

from tkinter import *

class Search(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.minsize(380,444)
        self.maxsize(380,444)
        self.title("")

        self.search_start = StringVar()
        self.search_start.set('Enter your query')
        self.search_result = StringVar()

        self.bind("<Return>", self.search_button)

        self.scrollbar = Frame(self)
        self.scrollbar.pack(side=RIGHT, fill=Y)
        self.search_bar = Frame(self, bg="blue")
        self.search_bar.pack(side=TOP, fill=X)
        self.index = Frame(self)
        self.index.pack(side=LEFT)
        self.content = Frame(self)
        self.content.pack(side=TOP, fill=X)
        self.status_bar = Frame(self, bg="yellow")
        self.status_bar.pack(side=BOTTOM, fill=X)

# ******* Search Input *******

        self.entry = Entry(self.search_bar, textvariable = self.search_start)
        self.entry.pack(side=LEFT, padx=4, pady=4)

# ******* Search Button *******

        self.search = Button(self.search_bar, text="Search", command=self.search_button)
        self.search.pack(side=LEFT)

# ******* …
Run Code Online (Sandbox Code Playgroud)

python tkinter python-3.x

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

是否有比python中更好的方法来处理跨平台斜线?

就像有问题。以下是我必须处理跨平台路径时使用的代码。有没有比这更好的方法了?

import platform

my_os = platform.system()
if my_os == "Windows":
    slash = "\\"
else:
    slash = "/"
Run Code Online (Sandbox Code Playgroud)

代码中的随机示例:

source_path = ""
for part in __file__.split("/")[:-1]:
    source_path += (part + slash)

print(source_path)
Run Code Online (Sandbox Code Playgroud)

python path filepath python-3.x

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