我尝试了"heapq"并得出结论,我的期望与我在屏幕上看到的不同.我需要有人解释它是如何工作的以及它在哪里有用.
从第2.2 节中的本周Python模块一书中可以看出
如果在添加和删除值时需要维护排序列表,请查看heapq.通过使用heapq中的函数来添加或删除列表中的项,您可以以较低的开销维护列表的排序顺序.
这就是我所做的和得到的.
import heapq
heap = []
for i in range(10):
heap.append(i)
heap
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
heapq.heapify(heap)
heapq.heappush(heap, 10)
heap
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
heapq.heappop(heap)
0
heap
[1, 3, 2, 7, 4, 5, 6, 10, 8, 9] <<< Why the list does not remain sorted?
heapq.heappushpop(heap, 11)
1
heap
[2, 3, 5, 7, 4, 11, 6, 10, 8, 9] …Run Code Online (Sandbox Code Playgroud) 在linux上,我们可以locale -a用来查看可用的语言环境列表.
$ locale -a
C
C.UTF-8
en_US.utf8
POSIX
Run Code Online (Sandbox Code Playgroud)
是否有可能从Windows上的python控制台做同样的事情?
当您尝试执行操作locale.setlocale(locale.LC_ALL, '???')并且根本不知道区域设置值的名称时,这可能很方便.
我需要帮助才能理解为什么用xml.etree.ElementTree解析我的xml文件*会产生以下错误.
*我的测试xml文件包含阿拉伯字符.
任务:
打开并解析utf8_file.xml文件.
我的第一次尝试:
import xml.etree.ElementTree as etree
with codecs.open('utf8_file.xml', 'r', encoding='utf-8') as utf8_file:
xml_tree = etree.parse(utf8_file)
Run Code Online (Sandbox Code Playgroud)
结果1:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 236-238: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我的第二次尝试:
import xml.etree.ElementTree as etree
with codecs.open('utf8_file.xml', 'r', encoding='utf-8') as utf8_file:
xml_string = etree.tostring(utf8_file, encoding='utf-8', method='xml')
xml_tree = etree.fromstring(xml_string)
Run Code Online (Sandbox Code Playgroud)
结果2:
AttributeError: 'file' object has no attribute 'getiterator'
Run Code Online (Sandbox Code Playgroud)
请解释上述错误并评论可能的解决方案.
在通过一个微小的2层神经网络的例子时,我注意到了我无法解释的结果.
想象一下,我们有以下数据集和相应的标签:
[0,1] -> [0]
[0,1] -> [0]
[1,0] -> [1]
[1,0] -> [1]
Run Code Online (Sandbox Code Playgroud)
让我们创建一个微小的2层NN,它将学习预测两个数字序列的结果,其中每个数字可以是0或1.我们将根据上面提到的数据集训练这个NN.
import numpy as np
# compute sigmoid nonlinearity
def sigmoid(x):
output = 1 / (1 + np.exp(-x))
return output
# convert output of sigmoid function to its derivative
def sigmoid_to_deriv(output):
return output * (1 - output)
def predict(inp, weigths):
print inp, sigmoid(np.dot(inp, weigths))
# input dataset
X = np.array([ [0,1],
[0,1],
[1,0],
[1,0]])
# output dataset
Y = np.array([[0,0,1,1]]).T
np.random.seed(1)
# init weights …Run Code Online (Sandbox Code Playgroud) 我想匹配字符串的一部分(一个特定的单词)并打印出来.究竟是什么呢grep -o.我的话就是"黄狗",它可以在一个跨越多行的字符串中找到.
[34343] | ****. "Example": <one>, yellow dog
tstring0 123
tstring1 456
tstring2 789
Run Code Online (Sandbox Code Playgroud)
让我们试试这个正则表达式mydog = re.compile(', .*\n')
,然后
if mydog.search(string):只打印匹配的单词.
如何在输出中只获得"黄狗"?
想象一下,我们有一个5x4矩阵.我们只需删除第一个维度.我们怎么能用numpy做到这一点?
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 12., 13., 14., 15.],
[ 16., 17., 18., 19.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
我试过了:
arr = np.arange(20, dtype=np.float32)
matrix = arr.reshape(5, 4)
new_arr = numpy.delete(matrix, matrix[:,0])
trimmed_matrix = new_arr.reshape(5, 3)
Run Code Online (Sandbox Code Playgroud)
它看起来有点笨重.我做得对吗?如果是,是否有更简洁的方法来移除尺寸而不重塑?
我试图添加一个tomcat7-maven-plugin依赖项到pom.xml文件并收到错误.我按照tomcat.apache.org上的说明进行操作.

cvc-complex-type.2.4.a: Invalid content was found starting with element 'configuration'. One of '{"http://maven.apache.org/POM/4.0.0":type, "http://
maven.apache.org/POM/4.0.0":classifier, "http://maven.apache.org/POM/4.0.0":scope, "http://maven.apache.org/POM/4.0.0":systemPath, "http://
maven.apache.org/POM/4.0.0":exclusions, "http://maven.apache.org/POM/4.0.0":optional}' is expected.
Run Code Online (Sandbox Code Playgroud)
我的完整pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tastyminerals.poems</groupId>
<artifactId>poemcollection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<hibernate.version>4.3.5.Final</hibernate.version>
<mysql.connector.version>5.1.30</mysql.connector.version>
<spring.version>4.0.3.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId> …Run Code Online (Sandbox Code Playgroud) 我正在创建一个setup.py来分发我的应用程序.这个应用程序有许多依赖项,可以通过pip安装,它也有一些自定义的依赖项,无法从PyPI安装.
所以,我已经创建了一个custom_package_0.1.whl将包含在发行版中,并且必须在setup.py安装所有内容之后作为依赖项安装install_requires.
想象一下以下app结构:
my_app/
win_deps/custom_package_0.1.whl
my_app/
__init__.py
main.py
setup.py
setup.cfg
Run Code Online (Sandbox Code Playgroud)
我怎么做?
1)当我启动我的gui应用程序时,我需要设置默认激活的三个ttk.Radiobuttons中的一个.
我该怎么做?
2)我还需要检查用户是否激活/点击了我的ttk.Radiobuttons之一.
我该怎么做?
rb1 = ttk.Radiobutton(self.frame, text='5', variable=self.my_var, value=5)
rb2 = ttk.Radiobutton(self.frame, text='10', variable=self.my_var, value=10)
rb3 = ttk.Radiobutton(self.frame, text='15', variable=self.my_var, value=15)
self.rb1.grid(row=0)
self.rb2.grid(row=1)
self.rb3.grid(row=2)
Run Code Online (Sandbox Code Playgroud) 我不太明白text.search方法是如何工作的.例如,有一句话:Today a red car appeared in the park.我需要找到a red car序列并突出显示它.它被发现,但这是我突出显示的样子:

我正在使用self.text.search(word, start, stopindex=END)这句话.看起来搜索方法与python的regexp搜索完全相同.添加exact=True没有改变任何东西,因为它是默认行为,这就是为什么我不明白究竟是什么究竟真实的意思.如何a red car正确突出显示?
python ×9
python-2.7 ×3
tkinter ×2
arrays ×1
elementtree ×1
heap ×1
java ×1
java-ee ×1
locale ×1
maven ×1
numpy ×1
regex ×1
search ×1
setup.py ×1
setuptools ×1
tkinter.text ×1
tomcat ×1
ttk ×1
xml ×1
xml-parsing ×1