小编Kim*_*Kim的帖子

不区分大小写的字典

我希望我的字典不区分大小写.

我有这个示例代码:

text = "practice changing the color"

words = {'color': 'colour',
        'practice': 'practise'}

def replace(words,text):

    keys = words.keys()

    for i in keys:
        text= text.replace(i ,words[i])
    return  text

text = replace(words,text)

print text
Run Code Online (Sandbox Code Playgroud)

输出=练习改变颜色

我想要另一个字符串,"practice changing the Color"(Color以一个大写字母开头)也给出相同的输出.

我相信有一种通用的方法可以转换为小写, mydictionary[key.lower()]但我不知道如何最好地将它集成到我现有的代码中.(如果这是一个合理,简单的方法,无论如何).

python

64
推荐指数
6
解决办法
5万
查看次数

我如何使用matplotlib autopct?

我想创建一个matplotlib饼图,其中每个楔形的值都写在楔形顶部.

文件建议我应该使用autopct这样做.

autopct:[无| 格式字符串| 格式函数]如果不是None,则是用于用其数值标记楔形的字符串或函数.标签将放在楔子内.如果是格式字符串,则标签为fmt%pct.如果它是一个函数,它将被调用.

不幸的是,我不确定这个格式字符串或格式函数应该是什么.

使用下面的这个基本示例,如何在其楔形顶部显示每个数值?

plt.figure()
values = [3, 12, 5, 8] 
labels = ['a', 'b', 'c', 'd'] 
plt.pie(values, labels=labels) #autopct??
plt.show()
Run Code Online (Sandbox Code Playgroud)

python matplotlib

45
推荐指数
6
解决办法
4万
查看次数

Scipy优化fmin ValueError:使用序列设置数组元素

当使用scipy.optimizefmin,我得到一个错误,我不明白:

ValueError: setting an array element with a sequence.
Run Code Online (Sandbox Code Playgroud)

这是一个简单的平方错误示例来演示:

import numpy as np
from scipy.optimize import fmin

def cost_function(theta, X, y):    
    m = X.shape[0]
    error = X.dot(theta) - y 
    J = 1/(2*m) * error.T.dot(error)  
    return J

X = np.array([[1., 1.],
              [1., 2.],
              [1., 3.],
              [1., 4.]])

y = np.array([[2],[4],[6],[8]])   
initial_theta = np.ones((X.shape[1], 1)) * 0.01

# test cost_function
print cost_function(initial_theta, X, y)
# [[ 14.800675]] seems okay...

# but then error here...   
theta = fmin(cost_function, …
Run Code Online (Sandbox Code Playgroud)

python numpy scipy

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

QLabel setText在运行其他方法之前不会立即显示文本

我有一个基本标签,应该向用户表明程序正在搜索目录几秒钟.所以它就像......

self.label.setText(QString("Searching..."))
# method to search directories goes here
self.label.setText(QString("Search Complete"))
Run Code Online (Sandbox Code Playgroud)

我的问题是标签从不显示"正在搜索...".执行总是似乎直接跳转到运行扫描目录的方法,然后在扫描目录的方法完成后标签文本设置为"搜索完成".

如果有人能够解释为什么会这样,或者建议更好的方法来解决问题,我将不胜感激.

非常感谢

qt pyqt

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

我可以更改firebug控制台背景颜色吗?

我更喜欢firebug窗口有深色背景颜色和浅色文本(甚至只是灰色背景而不是白色就足够了).

有没有办法通过调整firebug的原始文件或者通过使用扩展名来实现这一点?

firebug

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

将stdin键击发送到通道而不需要换行符

我想在每次单独击键到stdin之后将用户的击键直接发送到一个通道.

我尝试过下面的代码,但这并没有给出所需的结果,因为reader.ReadByte()方法会阻塞,直到输入换行符.

func chars() <-chan byte {
    ch := make(chan byte)
    reader := bufio.NewReader(os.Stdin)
    go func() {
        for {           
            char, err := reader.ReadByte()
            if err != nil {
                log.Fatal(err)
            }
            ch <- char
        }
    }()
    return ch
}
Run Code Online (Sandbox Code Playgroud)

感谢您提供有关如何让每个用户输入字符立即转到频道而不需要换行符的任何建议.

channel go

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

二进制数据的Python字符串表示

我试图理解Python显示表示二进制数据的字符串的方式.

这是使用os.urandom的一个例子

In [1]: random_bytes = os.urandom(4)

In [2]: random_bytes
Out[2]: '\xfd\xa9\xbe\x87'

In [3]: random_bytes = os.urandom(4)

In [4]: random_bytes
Out[4]: '\r\x9eq\xce'
Run Code Online (Sandbox Code Playgroud)

在第一个例子中random_bytes,在每个\ x之后似乎有十六进制形式的值:fd a9是87.

但是,在第二个例子中,我不明白为什么'\r\x9eq\xce'要显示.

为什么Python会在这个特定的表示中向我显示这些随机字节?我该怎么解释'\r\x9eq\xce'

python

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

美丽的汤 - 根据评论旁边的位置识别标签

我正在使用美丽的汤.

有没有什么方法可以根据它在评论旁边的位置(解析树中没有包含的内容)来获取标签?

例如,假设我有......

<html>
<body>
<p>paragraph 1</p>
<p>paragraph 2</p>
<!--text-->
<p>paragraph 3</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

在这个例子中,<p>paragraph 2</p>鉴于我正在搜索评论" <!--text-->" ,我如何识别?

谢谢你的帮助.

python beautifulsoup

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

如何将with-out-str与集合一起使用?

我可以用来with-out-str从中获取字符串值(doc func).

=> (with-out-str (doc first))
"-------------------------\nclojure.core/first\n([coll])\n  Returns the first item in the collection. Calls seq on its\n    argument. If coll is nil, returns nil.\n"    
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试使用函数集合执行相同的操作,我只能为每个函数返回空字符串:

=> (map #(with-out-str (doc %)) [first rest])
("" "")
Run Code Online (Sandbox Code Playgroud)

我在哪里错了?

clojure

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

无需压缩即可将文件添加到zip存档

在Go中,我们如何在没有压缩的情况下将文件添加到zip存档?

对于上下文,我将跟随IBM教程创建一个epub zip文件.它显示以下Python代码:

import zipfile, os

def create_archive(path='/path/to/our/epub/directory'):
    '''Create the ZIP archive.  The mimetype must be the first file in the archive 
    and it must not be compressed.'''

    epub_name = '%s.epub' % os.path.basename(path)

    # The EPUB must contain the META-INF and mimetype files at the root, so 
    # we'll create the archive in the working directory first and move it later
    os.chdir(path)    

    # Open a new zipfile for writing
    epub = zipfile.ZipFile(epub_name, 'w')

    # Add the mimetype file …
Run Code Online (Sandbox Code Playgroud)

zip go epub

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

标签 统计

python ×5

go ×2

beautifulsoup ×1

channel ×1

clojure ×1

epub ×1

firebug ×1

matplotlib ×1

numpy ×1

pyqt ×1

qt ×1

scipy ×1

zip ×1