小编Ahm*_*mad的帖子

Python:将数组定义为实例类属性

我需要有几个对象,每个对象都有一个单独的数组。我写了这段代码:

    read_values = list()
    for read_unit in read_units:
        read_value = ReadValues.objects.all().filter(ReadID=read_unit.ReadID)

        element = TempObjectForReadValues()
        for read_element in read_value:
            element.read_elements[read_element.Code] = read_element.ReadValue
        read_values.append(element)
        print(element.read_elements)

    print('  ')
    for test_element in read_values:
        print(test_element.read_elements)
Run Code Online (Sandbox Code Playgroud)

这就是我定义该类的方式:

    class TempObjectForReadValues():
        read_elements = [None] * 10
Run Code Online (Sandbox Code Playgroud)

结果是:

[None, None, 16.0, None, 189.0, 345.0, None, None, None, None]
[None, None, 16.0, 43.0, 876.0, 345.0, None, None, None, None]

[None, None, 16.0, 43.0, 876.0, 345.0, None, None, None, None]
[None, None, 16.0, 43.0, 876.0, 345.0, None, None, None, None]
Run Code Online (Sandbox Code Playgroud)

这意味着数据将被覆盖以前的数据。另外,如果我不向新对象中的数组分配任何内容,它会保存前一个对象的结果。:( …

arrays attributes class overwrite python-3.x

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

Angular指令:只允许数字

这是一个示例angular指令,用于防止键入非数字键(StackOverflow应答).我想写一些类似这个小提琴的东西,在几个输入中使用is-number指令.请注意,因为我的输入中有各种不同的指令,所以我不能使用上述答案更新中建议的相同模板.

var $scope;
var app = angular.module('myapp', []);

app.controller('Ctrl', function($scope) {
    $scope.myNnumber1 = 1;
    $scope.myNnumber2 = 1;
});

app.directive('isNumber', function () {
    return {
        require: 'ngModel',
        link: function (scope, element) {   
            scope.$watch(element.ngModel, function(newValue,oldValue) {
                newValue = String(newValue);
                newValue = newValue.replace('?', '0').replace('?', '1').replace('?', '2').replace('?', '3').replace('?', '4').replace('?', '5').replace('?', '6').replace('?', '7').replace('?', '8').replace('?', '9');
                var arr = String(newValue).split("");
                if (arr.length === 0) return;
                if (arr.length === 1 && (arr[0] == '-' || arr[0] === '.' )) return;
                if (arr.length === …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-directive

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

python XlsxWriter:设置字体不影响输出Excel文件中单元格的外观

我使用 XlsxWriter python 包从我的数据生成 Excel 文件。我尝试了几种方法将字体“B Nazanin”应用到单元格:

title_cell_format.set_font_name('B Nazanin')
title_cell_format.set_font_family('B Nazanin')
title_cell_format.set_font('B Nazanin')
Run Code Online (Sandbox Code Playgroud)

当我使用其中一种方法时,我可以看到字体名称,但字体外观不像输出 Excel 文件中的“B Nazanin”:在此输入图像描述

我猜想解决方案应该是设置文本方向,可以在下面的菜单中在 Excel 中手动完成。现在的问题是我如何在我的python程序中设置这个属性! 在此输入图像描述

excel fonts python-3.x xlsxwriter

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

当localhost工作时,我无法通过IP看到我的python django服务器

我用python和django编写了一个服务器程序,我在几台计算机上测试过它.现在我已将其安装在HP服务器上.虽然我在浏览器地址中输入localhost时可以看到它正常工作,但当我输入服务器的IP时,我看到"此网页不可用"(ERR_CONNECTION_REFUSED).

操作系统是Windows Server 2008.没有安装任何防病毒或防火墙,并且禁用了Windows防火墙.

值得注意的是,我从服务器程序发送到网络中其他设备的消息已成功传送.换句话说,连接是单向的(不是双工的)

python django

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

比较字符串中的字符

我正在尝试创建一个函数,该函数比较两个长度相同的字符串的相同位置的字符并返回它们的差异计数。

例如,

a = "HORSE"
b = "TIGER"
Run Code Online (Sandbox Code Playgroud)

它会返回 5(因为同一位置的所有字符都不同)

这就是我一直在做的事情。

def Differences(one, two):
    difference = []
    for i in list(one):
        if list(one)[i] != list(two)[i]:
            difference = difference+1
    return difference
Run Code Online (Sandbox Code Playgroud)

这给出了一个错误“列表索引必须是整数而不是字符串”

所以我尝试使用 int(ord(

def Differences(one, two):
    difference = 0
    for i in list(one):
        if int(ord(list(one)[i])) != int(ord(list(two)[i])):
            difference = difference+1
    return difference
Run Code Online (Sandbox Code Playgroud)

这也返回相同的错误。

当我打印 list(one)[1] != list(two)[1] 时,它要么返回 True 要么返回 False,因为比较是正确进行的。

你能告诉我如何为此目的更正我的代码吗?

python string comparison character python-3.x

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

Python Django:用数据库字段注释同名

我需要注释一些必须命名为“id”的东西,它与表“id”的主键相同。最终输出应该是一个字典数组。我知道我可以使用另一个名称(如“id_for_tree”)进行注释,然后在使用之后values()制作字典并在循环中更改键。就像是:

item_list = MyTable.objects.annotate(id_for_tree=Concat(F('id'), F('Code'), output_field=CharField())).values('Name', 'id_for_tree')
for any_item in item_list:
    any_item['id'] = any_item.pop('id_for_tree')
Run Code Online (Sandbox Code Playgroud)

很明显,这个解决方案没有很好的性能。我怎样才能摆脱

“注释‘id’与模型上的字段冲突”

通过丢弃以前的异常'id'并注释新的'id'?

python django orm annotations

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