小编Reb*_*que的帖子

为什么有时保持订单顺序?

运行此代码时,结果会按预期更改,因为集合是无序的:

my_set_1 = {'a','b','c',}
print([i for i in my_set_1])
Run Code Online (Sandbox Code Playgroud)

也就是说,多次运行会给出不同的列表,例如

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

等等

(注意:您可能会获得相同的结果,如果您没有PYTHONHASHSEED=random,请按照注释中的建议.另外,如果您使用控制台进行复制,请确保Rerun每次运行代码时都使用控制台.)


但是,将上面的代码放在for循环中时,结果相当令人惊讶:

for i in range(10):
    my_set_1 = {'a','b','c',}
    print([i for i in my_set_1])
# Prints: 
# ['a', 'c', 'b']
# ['a', 'c', 'b']
# ['a', 'c', 'b']
# ....
Run Code Online (Sandbox Code Playgroud)

for循环的单次运行将打印相同的列表.重新运行for循环可以打印不同的列表(例如['c', 'b', 'a']),但仍然可以打印10次而不更改.

为什么不改变?

python for-loop set unordered python-3.x

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

jupyter:没有可用的内核

有人可以告诉我为什么我没有选择python3作为内核的选项吗?

# python3 -m ipykernel install --user
Installed kernelspec python3 in /root/.local/share/jupyter/kernels/python3

# python3 -m pip install ipykernel
Requirement already satisfied: ipykernel in /usr/lib/python3.4/site-packages
Requirement already satisfied: tornado>=4.0 in /usr/lib64/python3.4/site-packages (from ipykernel)
Requirement already satisfied: ipython>=4.0.0 in /usr/lib/python3.4/site-packages (from ipykernel)
Requirement already satisfied: jupyter-client in /usr/lib/python3.4/site-packages (from ipykernel)
Requirement already satisfied: traitlets>=4.1.0 in /usr/lib/python3.4/site-packages (from ipykernel)
Requirement already satisfied: backports_abc>=0.4 in /usr/lib/python3.4/site-packages (from tornado>=4.0->ipykernel)
Requirement already satisfied: pexpect; sys_platform != "win32" in /usr/lib/python3.4/site-packages (from ipython>=4.0.0->ipykernel)
Requirement …
Run Code Online (Sandbox Code Playgroud)

ipython-notebook jupyter jupyter-notebook

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

在 Ubuntu 上的 Opera 中打开 jupyter 笔记本

我已经将 jupyter 笔记本设置为在 Ubuntu 上的 Opera 中打开(Chrome 和 Lastpass 不适用于 jupyter)。我遵循了这个指南。

但是,当我从终端打开 jupyter 笔记本时,它会将我带到而http://openurl%28http//localhost:8888/tree,new-page)不是http://localhost:8888/tree.

正确的网址在 Chrome 上仍然可以正常打开。在 Ubuntu 16.04 上运行 Anaconda Python 3。

有任何想法吗?

python ubuntu jupyter

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

确保每个字母仅在密码中出现一次

刚开始学习python并尝试使用循环编写代码来检查唯一字母,如下所示:

def has_unique_letters(x):
  count = 0
  letters = set(x)
  while count < len(x):
    if x[count] in letters:
       return False
    count += 1
  return True
Run Code Online (Sandbox Code Playgroud)

但无论我在x中输入什么,它都返回false.代码有什么问题?谢谢!

python set while-loop

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

Tkinter 的按钮无法更改边框颜色

这是我的 Tkinter 代码:

Photoshop = Tkinter.Button(root, 
    text = 'Photoshop',
    fg = '#37d3ff',
    bg = '#001d26',
    bd =  10, 
    highlightthickness=4, 
    highlightcolor="#37d3ff", 
    highlightbackground="#37d3ff", 
    borderwidth=4)
Run Code Online (Sandbox Code Playgroud)

但是,在我对 Button 进行网格化后,边框的颜色没有显示出来。相反,它使用默认的灰色。

python tkinter button

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

python词典中的常见价值

map1 = { 1: "Sam", 2: "Rich", 3 : "Mike", 4: "Bob", 6: "Donald"}
map2 = { 5: "Sally", 6 : "Donald", 7: "Roger", 1: "Sam"}
Run Code Online (Sandbox Code Playgroud)

我想在两个字母中打印常用名称,任何人都可以帮助我

python dictionary

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

Python与numpy.where的多个条件

我正在尝试对熊猫数据帧进行一些简单的操作.我已将pandas导入pd并将numpy导入为np,并导入csv以创建名为'dfe'的数据框.

我已成功使用以下代码根据一个条件填充新列:

dfe['period'] = np.where(dfe['Time'] >= "07:30:00.000" , '1', '2')
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用类似的技术根据两个条件填充同一列时,我得到一个错误(&''bool'和'str'的不支持的操作数类型)

这是我对多条件版本的尝试:

dfe['period'] = np.where(dfe['Time'] >= "07:30:00.000" & dfe['Time'] <= "10:00:00.000" , '1', '2')
Run Code Online (Sandbox Code Playgroud)

我已经看过很多针对类似问题的解决方案,但是鉴于我刚刚开始并且希望有人可以给我一些关于为什么这不起作用的线索,它们对我来说有点太复杂了.

谢谢

python pandas

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

错误“无法打开Jupyter Notebook:端口8888已在使用中”

运行Jupyter Notebook时出现以下错误:

(base) C:\Users\ramne>jupyter notebook
[I 01:19:00.648 NotebookApp] The port 8888 is already in use, trying another port.
Run Code Online (Sandbox Code Playgroud)

而当我做netstat -ano | 找到Str“ 8888”,得到以下信息:

C:\Users\ramne>netstat -ano | findStr "8888"
TCP    127.0.0.1:8888         0.0.0.0:0              LISTENING       16024
TCP    [::1]:8888             [::]:0                 LISTENING       16024
Run Code Online (Sandbox Code Playgroud)

我不知道端口8888的默认连接是什么或其行为应该是什么样的。如何解决此错误,以便可以打开Jupyter Notebook?

python port jupyter-notebook

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

未使用参数“事件”值

我正在用 python 2.7 开发一个程序,但在 Pycharm 上遇到了一个问题。在程序中,我希望用户能够通过单击按钮或按“输入”键来浏览不同的屏幕。我尝试将它实现到我的程序中,它可以工作,但是 Pycharm 给出了错误

未使用参数“事件”

    import Tkinter as Tk


    class MemoryGameApp(Tk.Tk):
        def __init__(self, *args, **kwargs):
            Tk.Tk.__init__(self, *args, **kwargs)
            Tk.Tk.wm_title(self, "2 screens")
            container = Tk.Frame(self)
            container.pack(side="top", fill="both", expand=True)
            container.grid_rowconfigure(0, weight=1)
            container.grid_columnconfigure(0, weight=1)
            self.frames = {}
            for F in (StartPage, PageTwo):
                frame = F(container, self)
                self.frames[F] = frame
                frame.grid(row=0, column=0, sticky="nsew")
            self.show_frame(StartPage)

            def show_frame(self, cont):
                frame = self.frames[cont]
                frame.tkraise()

    class StartPage(Tk.Frame):
        def __init__(self, parent, controller):
                Tk.Frame.__init__(self, parent)
                label = Tk.Label(self, text="Page 1")
                label.pack()
                global button
                button = Tk.Button(self, text="Next", …
Run Code Online (Sandbox Code Playgroud)

python user-interface button

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

在Python中的类之间进行通信

我正在尝试基于tkinter构建计算器.我也从那里按照提示.程序肯定仍在进行中.我目前因为这个错误而陷入困境:

AttributeError:'Calculator_GUI'对象没有属性'main'

我不知道如何在两个班级之间进行沟通.我还要求对已经完成的工作进行简短的审查.提前致谢!这是代码:

import tkinter as tk
from tkinter import ttk

class Calculator_Core():
    def __init__(self):
        self.input = tk.StringVar()

    def buttonClick(self, a):
        self.input.set(a)
        #DEBUG:
        print(self.input)


class Calculator_GUI(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master
        self.button = {}
        self.configure_gui()
        self.create_widgets()
        self.main = Calculator_Core()

    def configure_gui(self):
        self.master.title("Calculator")
        self.master.resizable(False, False)

    def create_widgets(self):
        self.configure_input_space()
        row_number = 1
        column_number = 0
        a = -1
        text_buttons = ('789+', '456-', '123*', '0=/C')
        for button_row in text_buttons:
            for text_button in button_row:
                a += 1
                self.configure_button(text_button, row_number, …
Run Code Online (Sandbox Code Playgroud)

python tkinter calculator python-3.x

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