我只是为什么我的代码不起作用感到困惑,这是我到目前为止的问题和代码(测试运行说我的答案是错误的).
给定字典d,找到字典中最大的键并将相应的值与变量相关联val_of_max.例如,给定字典{5:3, 4:1, 12:2},2将与之关联val_of_max.假设d不是空的.
d = {5:3, 4:1, 12:2, 14:9}
val_of_max = max(d.keys())
print val_of_max
Run Code Online (Sandbox Code Playgroud) 在某些类中,我看到对函数的调用如下:
$this->ClearError();
Run Code Online (Sandbox Code Playgroud)
当函数驻留在该类本身时.上述方法与直接函数调用有何不同,如:
return ClearError();
Run Code Online (Sandbox Code Playgroud) 我需要能够告诉区别,可以包含一个字符串之间的字母和数字,而且可以包含一个字符串的数字,冒号和连字符.
>>> def checkString(s):
... pattern = r'[-:0-9]'
... if re.search(pattern,s):
... print "Matches pattern."
... else:
... print "Does not match pattern."
# 3 Numbers seperated by colons. 12, 24 and minus 14
>>> s1 = "12:24:-14"
# String containing letters and string containing letters/numbers.
>>> s2 = "hello"
>>> s3 = "hello2"
Run Code Online (Sandbox Code Playgroud)
当我checkString在上面的每个字符串上运行该方法时:
>>>checkString(s1)
Matches Pattern.
>>>checkString(s2)
Does not match Pattern.
>>>checkString(s3)
Matches Pattern
Run Code Online (Sandbox Code Playgroud)
s3是唯一一个没有做我想要的.我希望能够创建一个允许数字,冒号和连字符的正则表达式,但排除其他所有(或只是字母字符).谁能指出我正确的方向?
编辑:
因此,我需要一个接受的正则表达式:
229 // number …Run Code Online (Sandbox Code Playgroud) 当我执行以下列表理解时,我最终得到嵌套列表:
channel_values = [x for x in [ y.split(' ') for y in
open(channel_output_file).readlines() ] if x and not x == '\n']
Run Code Online (Sandbox Code Playgroud)
基本上我有一个由以下内容组成的文件:
7656 7653 7649 7646 7643 7640 7637 7634 7631 7627 7624 7621 7618 7615
8626 8623 8620 8617 8614 8610 8607 8604 8600 8597 8594 8597 8594 4444
<snip several thousand lines>
Run Code Online (Sandbox Code Playgroud)
此文件的每一行都以新行终止.
基本上我需要将每个数字(它们都由一个空格分隔)添加到列表中.
有更好的方法通过列表理解来做到这一点吗?
我是python的初学者,面临这个问题.那么我怎么能打破2-3行的下面的表达
totalIncome = (classACost * float(classASeatsSold)) + (classBCost * float(classBSeatsSold)) + (classCCost * float(classCSeatsSold))
Run Code Online (Sandbox Code Playgroud)
像这样.
totalIncome = (classACost * float(classASeatsSold)) +
(classBCost * float(classBSeatsSold)) +
(classCCost * float(classCSeatsSold))
Run Code Online (Sandbox Code Playgroud)
基本原因是我希望在80列中适应这一行.如果我对问题标题不对,请同时提出合适的标题.提前致谢.
我正在编写这段代码作为面向对象编程的练习.
在这里,我试图将房屋定义为房间列表,并将每个房间定义为设备列表(例如灯具).
首先,我创建了所有对象,并将两个房间附加到房屋,并为每个房间添加了不同的设备.很基本的.
问题是,似乎设备被附加到两个房间.这是为什么?
代码:
#! /usr/bin/python
class House:
def __init__(self, rooms = list()):
self.rooms = rooms
print('house created')
class Room:
def __init__(self, name = 'a room', devs = list()):
self.name = name
self.devs = devs
print('room ' + self.name + ' created')
class Device:
def __init__(self, name = 'a device'):
self.name = name
print('device ' + self.name + ' created')
def main():
#1
h = House()
r1 = Room(name = 'R1')
r2 = Room(name = 'R2')
d1 = Device(name …Run Code Online (Sandbox Code Playgroud) 我有2个变量,每个变量包含一个数字(整数).我想把它们排在第一和第二大的最低数量.例如:
$sortedVar = getSmaller(45, 62); // Will return 45
$sortedVar = getSmaller(87, 23); // Will return 23
Run Code Online (Sandbox Code Playgroud)
你知道我想做什么吗?你能帮我吗?谢谢 :)
我有Windows Vista 64位SP2.我正在尝试使用wxPython与Python进行GUI开发,因为我所有的研究都指出了这种方式.我已经下载并安装了win64 wxPython.我每次都得到同样的错误.
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\__init__.py", line 4
5, in <module>
from wx._core import *
File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 4, i
n <module>
import _core_
ImportError: DLL load failed: %1 is not a valid Win32 application.
Run Code Online (Sandbox Code Playgroud)
我在我的python上安装了c,我在c:\ program files(x86)中安装了python26并在那里安装了wxPython,我使用的是32bit,64bit.我试过到处寻找类似的问题,我几乎没有见过其他人这个问题.任何帮助将不胜感激.
如果可能的话,我想C:\abc.bmp变成abc.bmp,甚至更好abc.使用.NET很容易,因为这两个目标都有功能.python中有类似的东西吗?
我怎样才能切断最后的空地?
a = ['Hello ','everybody ','! ']
for i in range(len(a)):
a[i,-1]=''
print a
Run Code Online (Sandbox Code Playgroud)