小编Ble*_*der的帖子

string.charAt(x)或string [x]?

有什么理由我应该使用string.charAt(x)而不是括号表示法string[x]吗?

javascript string

233
推荐指数
6
解决办法
7万
查看次数

CSS中的'property:0'或'property:0px'?

我已经看过这个符号使用了很多,我想知道,这两个符号之间有什么明显的区别吗?

element#id
{
  property: 0;
}
Run Code Online (Sandbox Code Playgroud)

element#id
{
  property: 0px;
}
Run Code Online (Sandbox Code Playgroud)

property: 0px;一直都在使用,因为我觉得它更干净,但我不确定浏览器的解释是否与之0px不同0.

有谁知道哪一个更好还是更正确?

css properties zero

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

熊猫:采样数据框架

我正在尝试用Pandas读取一个相当大的CSV文件并将其分成两个随机块,其中一个是10%的数据,另一个是90%.

这是我目前的尝试:

rows = data.index
row_count = len(rows)
random.shuffle(list(rows))

data.reindex(rows)

training_data = data[row_count // 10:]
testing_data = data[:row_count // 10]
Run Code Online (Sandbox Code Playgroud)

出于某种原因,sklearn当我尝试在SVM分类器中使用这些结果DataFrame对象之一时抛出此错误:

IndexError: each subindex must be either a slice, an integer, Ellipsis, or newaxis
Run Code Online (Sandbox Code Playgroud)

我想我做错了.有一个更好的方法吗?

python partitioning pandas

68
推荐指数
5
解决办法
8万
查看次数

Python检查网站是否存在

我想检查某个网站是否存在,这就是我正在做的事情:

user_agent = 'Mozilla/20.0.1 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent':user_agent }
link = "http://www.abc.com"
req = urllib2.Request(link, headers = headers)
page = urllib2.urlopen(req).read() - ERROR 402 generated here!
Run Code Online (Sandbox Code Playgroud)

如果页面不存在(错误402,或其他任何错误),我该怎么做page = ...才能确保我正在阅读的页面退出?

html python urlopen

50
推荐指数
7
解决办法
9万
查看次数

JavaScript相当于Python的format()函数?

Python有这个美丽的功能来解决这个问题:

bar1 = 'foobar'
bar2 = 'jumped'
bar3 = 'dog'

foo = 'The lazy ' + bar3 + ' ' + bar2 ' over the ' + bar1
# The lazy dog jumped over the foobar
Run Code Online (Sandbox Code Playgroud)

进入:

bar1 = 'foobar'
bar2 = 'jumped'
bar3 = 'dog'

foo = 'The lazy {} {} over the {}'.format(bar3, bar2, bar1)
# The lazy dog jumped over the foobar
Run Code Online (Sandbox Code Playgroud)

JavaScript有这样的功能吗?如果没有,我将如何创建一个遵循与Python实现相同的语法?

javascript python format

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

使用可变宽度div时的CSS文本省略号

我想知道当父div和相邻div没有足够的空间时,是否有任何方法确实在浮动div增益省略号中有文本.例如:

<style>
.parent-div {
    width: 100%;
    border: 1px;
    padding: 4px;
}
.text-div {
    float: right;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;    
}
.icon-div {
    float: left;
}
</style>
<div class="parent-div">
  <div class="text-div">This is text I'd like to truncate when space doesn't permit</div>
  <div class="icon-div">X</div>
</div>
Run Code Online (Sandbox Code Playgroud)

到目前为止,如果我关闭浏览器窗口,父div将崩溃,然后text-div中的空格将消失,但是当没有更多空间时,省略号永远不会启动.

我唯一能想到的就是在窗口调整大小并在text-div上动态设置一个新的固定宽度时触发一个事件,但这只是感觉不够优雅,特别是考虑到填充和其他相邻的工件我不得不减去适当的宽度.

对这个有什么想法吗?

这是一个jsFiddle演示:http://jsfiddle.net/Blender/kXMz7/

html css ellipsis css-float

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

尝试/捕获或验证速度?

我正在使用Python,每当我必须验证函数输入时,我认为输入有效,然后发现错误.

就我而言,我有一个通用Vector()类,我用它来做一些不同的事情,其中​​一个是补充.它既可以作为Color()类也可以作为a Vector(),因此当我向其添加标量时Color(),它应该将该常量添加到每个单独的组件中.Vector()Vector()添加所需的组分添加.

此代码用于光线跟踪器,因此任何速度提升都很棒.

这是我Vector()班级的简化版本:

class Vector:
  def __init__(self, x, y, z):
    self.x = x
    self.y = y
    self.z = z

  def __add__(self, other):
    try:
      return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
    except AttributeError:
      return Vector(self.x + other, self.y + other, self.z + other)
Run Code Online (Sandbox Code Playgroud)

我目前正在使用这种try...except方法.有人知道更快的方法吗?


编辑:由于答案,我尝试并测试了以下解决方案,在添加Vector()对象之前专门检查类名:

class Vector:
  def __init__(self, x, y, z):
    self.x = x
    self.y = y …
Run Code Online (Sandbox Code Playgroud)

python performance exception-handling typechecking

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

JavaScript:使用加号显示正数

我如何显示正数,如3为+3,负数如-5为-5?所以,如下:

1,2,3进入+1,+ 2,+ 3

但如果是的话

-1,-2,-3然后进入-1,-2,-3

javascript numbers

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

Flask重定向多条路线

我正在尝试实现重定向模式,类似于StackOverflow的功能:

@route('/<int:id>/<username>/')
@route('/<int:id>/')
def profile(id, username=None):
    user = User.query.get_or_404(id)

    if user.clean_username != username:
        return redirect(url_for('profile', id=id, username=user.clean_username))

    return render_template('user/profile.html', user=user) 
Run Code Online (Sandbox Code Playgroud)

这是一个应该发生什么的简单表格:

URL                         Redirects/points to
====================================================
/user/123                   /user/123/clean_username
/user/123/                  /user/123/clean_username
/user/123/foo               /user/123/clean_username
/user/123/clean_username    /user/123/clean_username
/user/123/clean_username/   /user/123/clean_username/
/user/125698                404
Run Code Online (Sandbox Code Playgroud)

现在,我可以访问配置文件/user/1/foo,但/user/1产生一个BuildError.我已经尝试了alias=True关键字参数和一些东西defaults,但我不太确定什么是无效的.

我怎样才能让一条路线像这样重定向到另一条路线?

python redirect routing flask

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

确定给定的类属性是否是属性,Python对象

这一切都在标题中.以下是以下示例:

class A(object):
    my_var = 5

    def my_method(self, drink='beer'):
        return 'I like %s' % drink

    @property
    def my_property(self):
        return 'I do not drink coffee'
Run Code Online (Sandbox Code Playgroud)

我实例化一个A对象,我想知道每个属性的类型,以及它是否可调用.为此,我正在使用dir().

obj = A()

for attr in dir(obj):
    print 'Type: %s' % type(obj)
    print 'Is callable: %s' % callable(attr)
Run Code Online (Sandbox Code Playgroud)

我还必须知道属性是否属性.我确信有办法知道这一点.所有建议将不胜感激.

python

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