小编Jos*_*osh的帖子

是否可以在python列表理解中使用'else'?

这是我试图变成列表理解的代码:

table = ''
for index in xrange(256):
    if index in ords_to_keep:
        table += chr(index)
    else:
        table += replace_with
Run Code Online (Sandbox Code Playgroud)

有没有办法将else语句添加到此理解中?

table = ''.join(chr(index) for index in xrange(15) if index in ords_to_keep)
Run Code Online (Sandbox Code Playgroud)

python list-comprehension

190
推荐指数
4
解决办法
10万
查看次数

通过SSH克隆Mercurial存储库

我在使用ssh克隆我的mercurial存储库时遇到了一些困难.

这是我尝试过的:

hg clone ssh://username@username.webfactional.com/path/to/projectname projectname
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误:

remote: bash: hg: command not found
abort: no suitable response from remote hg!
Run Code Online (Sandbox Code Playgroud)

但是,hg安装在服务器上.

我试图按照本网站上的说明进行操作.

ssh mercurial webfaction

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

在使用C++之后如何用Python思考?

我是Python的新手,并试图通过将以下C++函数复制到python中来学习它

// determines which words in a vector consist of the same letters
// outputs the words with the same letters on the same line
void equivalentWords(vector <string> words, ofstream & outFile) {
    outFile << "Equivalent words\n";

    // checkedWord is parallel to the words vector. It is
    // used to make sure each word is only displayed once.
    vector <bool> checkedWord (words.size(), false);

    for(int i = 0; i < words.size(); i++) {
        if (!checkedWord[i]){
            outFile << "  ";
            for(int j …
Run Code Online (Sandbox Code Playgroud)

c++ python

7
推荐指数
1
解决办法
463
查看次数

为什么将管理器声明为Django模型,使"对象"无效

我已经向以下模型宣布了一个经理,但在这样做之后,我再也无法使用了List.objects.get().谁知道为什么?

class List(models.Model):
  title = models.CharField(max_length=20, unique=True)
  archived = models.BooleanField()

  archived_lists = ArchivedListManager()
  active_lists = ActiveListManager()
Run Code Online (Sandbox Code Playgroud)

经理们:

class ArchivedListManager(models.Manager):
  def get_query_set(self):
    return super(ArchivedListManager, self).get_query_set().filter(archived=True)

class ActiveListManager(models.Manager):
  def get_query_set(self):
    return super(ActiveListManager, self).get_query_set().filter(archived=False)
Run Code Online (Sandbox Code Playgroud)

错误是 type object 'List' has no attribute 'objects'

django django-managers

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

使用JQuery和AJAX在Django中刷新div

我试图用AJAX和JQuery(在Django中)刷新页面的某个部分.我怎样才能让它重新显示div而不是整个页面.

    // In my template
    var tag_cloud_floor = function(floor) {
    $.ajax({ url: "/{{ user }}/{{ tag }}/",
                     data: {tag_cloud_floor: floor},
                     type: 'POST',
                     success: function(data) {
                         $('#tag_cloud).html(data);
                     },
    });

};
Run Code Online (Sandbox Code Playgroud)

这是我的看法.

@login_required
def tag_page(request, username, tag):
  if username == request.user.username:
    tags = request.user.userprofile.tag_set.all()

    if request.is_ajax() and request.POST:
      floored_tags = []
      for t in tags:
        if t.item_set.all().count() >= int(request.POST['tag_cloud_floor']):
          floored_tags.append(t)
      tags = floored_tags

    tag = Tag.objects.get(title=tag)
    items = tag.item_set.all()
    return render_to_response("tag_page.html", { 'user': request.user , 
                                              'tag': tag,
                                             'tags': tags,
                                            'items': items }) …
Run Code Online (Sandbox Code Playgroud)

django ajax jquery

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

为什么结构在Xcode中不起作用,当它们在Visual C++中时呢?需要帮助!

出于某种原因,这个非常基本的代码将在Visual C++中编译时没有错误,但在XCode中会出错.我需要知道为什么,为了继续在Xcode中为我的计算机科学课程工作.

#include <iostream>
#include <string>

using namespace std;

struct acct {        // bank account data
    int     num;      // account number
    string name;      // owner of account
    float   balance; // balance in account
};

int main() {

    acct account;

    cout << "Enter new account data: " << endl;
    cout << "Account number: ";
    cin  >> account.num;
    cout << "Account name: ";
    cin  >> account.name;
    cout << "Account balance: ";
    cin  >> account.balance;

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

它给出了两个错误,一个说它预期';' 在帐户之前(在声明主要之后),并且第二个帐户未被声明为cin >> account.num;

c++ xcode

3
推荐指数
1
解决办法
1262
查看次数

等于登录django-registration激活网址

我正在使用django-registration,出于某种原因,当它发送激活邮件时,它会在第三个到最后一个字符中插入一个等号:http://example.com/accounts/activate/a65b4aca5156211bc522e29f3e872290544d14= e4 /

这意味着URL调度程序不会捕获URL(正则表达式是^activate/(?P<activation_key>\w+)/$.无论如何,url是不正确的,因为它应该没有等号.

谁知道为什么会这样?

django django-registration

3
推荐指数
1
解决办法
586
查看次数

为什么win32com比xlrd慢得多?

我有相同的代码,使用win32com和xlrd编写.xlrd在不到一秒的时间内完成算法,而win32com需要几分钟.

这是win32com:

def makeDict(ws):
"""makes dict with key as header name, 
   value as tuple of column begin and column end (inclusive)"""
wsHeaders = {} # key is header name, value is column begin and end inclusive
for cnum in xrange(9, find_last_col(ws)):
    if ws.Cells(7, cnum).Value:
        wsHeaders[str(ws.Cells(7, cnum).Value)] = (cnum, find_last_col(ws))
        for cend in xrange(cnum + 1, find_last_col(ws)): #finds end column
            if ws.Cells(7, cend).Value:
                wsHeaders[str(ws.Cells(7, cnum).Value)] = (cnum, cend - 1)
                break
return wsHeaders
Run Code Online (Sandbox Code Playgroud)

和xlrd

def makeDict(ws):
"""makes dict with key as …
Run Code Online (Sandbox Code Playgroud)

python xlrd win32com

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

在python中使用多个excel工作簿

使用 win32com,我打开了两个工作簿。

  1. 你怎么知道哪个是活跃的?
  2. 你如何改变哪个是活跃的?
  3. 你怎么能关闭一个而不是另一个?(不是 Application.Quit())

python com excel win32com

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

在Mac OS X上编辑C++的好方法是什么?

我是Comp的第一年.科学.学生,我正在寻找在Mac上开发C++的最佳方法.我有Xcode和Textmate.

每个的好处/负面是什么?还有更好的吗?

我不喜欢使用整个项目来运行Xcode程序.这是唯一的方法吗,还是我弄错了?

另外,有没有办法更改xcode中.cpp文件中包含的默认文本?

c++ macos xcode textmate

1
推荐指数
3
解决办法
2150
查看次数

win32com相当于xlrd的sheet.ncols

xlrd使得很容易知道最后一列是什么.

使用win32com有一个简单的方法吗?

我尝试过使用ws.UsedRange.Rows.Count,但这似乎没有给出正确的答案.

python com excel xlrd win32com

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

有没有办法让XCode Brackets出现在下一行?

在XCode中,自动完成的第一个括号出现在同一行:

    if (<#condition#>) {
        <#statements#>
    }
Run Code Online (Sandbox Code Playgroud)

有没有办法让它看起来像这样,所以我不必每次都改变它?

    if (<#condition#>)
    {
        <#statements#>
    }
Run Code Online (Sandbox Code Playgroud)

macos xcode

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

在 Django 中使用 AJAX/jQuery 删除对象

我有一个显示数据库中对象列表的模板。

{% for item in items %}
<li>
    {{ item }} - Tags: 
    {% for tag in item.tags.all %}
        <a href="/{{ user }}/{{ tag }}/">{{ tag }}</a>
    {% endfor%}
<br>
{{ item.notes }}
<br>
{{ item.date_modified|humanize_time_diff }}
<a href="">delete</a>
</li>
{% endfor%}
Run Code Online (Sandbox Code Playgroud)

如何允许用户删除这些对象之一,同时停留在同一页面上?

这是我的视图函数:

def tag_page(request, username, tag=None):
if username == request.user.username:
    if tag and tag in request.user.userprofile.tag_set.all():
        tag = Tag.objects.get(title=tag)
        items = tag.item_set.all()
        return render_to_response("tag_page.html", { 'user': request.user , 
                                                      'tag': tag,
                                                    'items': items })
    else:
        items = request.user.userprofile.item_set.all()
        return …
Run Code Online (Sandbox Code Playgroud)

django ajax jquery

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