小编Dav*_*son的帖子

如何使用Django创建类别和子类别系统?

如何使用Django创建类别和子类别系统?有申请吗?

django

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

算法具有不同的复杂性

算法的复杂性可以在O(n ^ 2)和O(n logn)中吗?我很确定这个.但是在Ω(n ^ 2)和O(n logn)中,还是在Θ(n ^ 2)和Ω(n logn)中呢.谢谢

algorithm optimization complexity-theory

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

如何生成指定长度的星号字符串?

我怎么会产生指定长度的星号(所以我可以用它旁边一个数组项的.长度属性)的字符串?

vb.net string visual-studio-2010

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

在R中使用if语句的double for循环出错

我逐个循环遍历矩阵,我想将所有小于1的值更改为负倒数.这是我目前拥有的循环

x <- 1:rowlength
y <- 1:colLength
for (i in x) {
    for (j in y) {
        Cell <- Nmatrix[i,j]
        if (Cell<=1) {Cell = -1/Cell}
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试运行代码时不断收到以下错误:

Error in if (Cell <= 1) { : missing value where TRUE/FALSE needed
Run Code Online (Sandbox Code Playgroud)

我不确定我做错了什么.任何帮助深表感谢.

谢谢

for-loop if-statement r

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

Python:如何仅使用while循环返回列表而不修改原始列表?

假设我有一个名为everythird的函数,它以列表作为参数,并返回一个包含原始列表的每个第三个元素的新列表,从索引0开始.我知道如何使用切片表示法执行此操作(返回everythird [0 :: 3] ]),但我们只能使用while循环.如果我输入每一个([1,2,3,4,5,6,7,8]),我希望它返回[1,4,7].我尝试了几种不同的方式,但我没有得到一个列表,或者我只得到一个值.如何退回清单?另外,您如何确定某些内容是修改还是不修改原始列表?

谢谢.

这是我尝试这种方式之一:every_third([1,2,3,4,5,6,7,8,9,10,11])

def everythird(l):
    '''(list) -> list
    Returns every third element of original list, starting at index 0'''

    i = 0
    while i < len(l):
        print(l[i])
        i += 3
Run Code Online (Sandbox Code Playgroud)

这打印

1
4
7
Run Code Online (Sandbox Code Playgroud)

python list while-loop python-3.x

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

程序无法从arg中读取路径名."没有相应的文件和目录"

我有一个python脚本的新问题.当我尝试运行它将路径作为参数传递给程序时,它返回错误消息:" No such file or directory".该程序应该遍历路径名指定的目录,以查找文本文件并打印出前两行.

是的确,这是家庭作业,但我看了很多关于os和sys的内容,但仍然没有得到它.你们有些退伍军人能帮助新手吗?谢谢

    #!/usr/bin/python2.7
    #print2lines.py
    """
    program to find txt-files in directory and
    print out the first two lines
    """
    import sys, os

    if (len(sys.argv)>1):
        path = sys.argv[0]
        if os.path.exist(path):
            abspath = os.path.abspath(path):
                dirlist = os.listdir(abspath)
                for filename in dirlist:
                    if (filename.endswith(".txt")):
                        textfile = open(filename, 'r')
                        print filename + ": \n"
                        print textfile.readline(), "\n"
                        print textfile.readline() + "\n"

                    else:   
                        print "passed argument is not valid pathname"
    else:   
        print "You must pass path to directory as …
Run Code Online (Sandbox Code Playgroud)

python

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

为什么我在这个脚本中得到一个NameError?

我正在从Wikibooks进行非程序员教程Python2.6的练习.

我有这个脚本:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

print("Program to calculate the area of square, rectangle and circle.")

def areaOfSquare():
    side = input("What is the length of one side of the square? ")
    area = side ** 2
    return area

def areaOfRectangle():
    width = input("What is the width of the rectangle? ")
    height = input("What is the height of the rectangle? ")
    area = 2*width+2*height
    return area

def areaOfCircle():
    radius = input("What is the radius of the circle? …
Run Code Online (Sandbox Code Playgroud)

python python-2.7

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

我的代码运行不正常,那里有什么问题?

hand = ['A','Q']
points = 0
player_cards1 = ['2']
value1 = 2
player_cards2 = ['3']
value2 = 3
player_cards3 = ['4']
value3 = 4
player_cards4 = ['5']
value4 = 5
player_cards5 = ['6']
value5 = 6
player_cards6 = ['7']
value6 = 7
player_cards7 = ['8']
value7 = 8
player_cards8 = ['9']
value8 = 9
player_cards9 = ['T']
value9 = 10
player_cards10 = ['Q']
value10 = 10
player_cards11 = ['K']
value11 = 10
player_cards12 = ['J']
value12 = 10
ACE11 = …
Run Code Online (Sandbox Code Playgroud)

python list python-2.7

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

计算循环中的对象

这是我写的:

#include <iostream> 
using namespace std; 

struct vetura{
    char ngjyra[10];
    char tipi[10];
};

int main(){
    int i,j;
    vetura v[4];

    for(i=0;i<4;i++){
        cout << "Ngjyra:"<<endl;
        cin >> v[i].ngjyra;
        cout << "tipi:"<<endl;
        cin >> v[i].tipi;
    }
    j=0;
    for(i=0;i<4;i++){
        if(v[i].ngjyra == "kuqe" && v[i].tipi == "passat")
        j+1;
    }
    cout<<"kemi "<<j<<" vetura passat me ngjyre te kuqe";


    cin.get();cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我需要计算我为v [i] .ngjyra写了"kuqe"的次数和v [i] .tipi的"passat".我认为变量j每次都会变得越来越大但是它不起作用我将在接下来的几个小时进行考试可以有人帮助我吗?

c++

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

zlib,c和gzread

所以我在Ubuntu上使用zlib包.我正在试图弄清楚如何正确使用gzopen和gzread,这是我到目前为止所拥有的

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <zlib.h>

#define NUM_BUFFERS 8
#define BUFFER_LENGTH 1024

char buf[BUFFER_LENGTH];
int main(int argc, const char* argv[])
{
    int status;
    gzFile file;
    file = gzopen("beowulf.txt", "w");
    int counter = 0; /*when the counter reachers BUFFERS_FULL, stop*/
    if(file == NULL)
    {
        printf("COULD NOT OPEN FILE\n");
        return 1;
    }
    while(counter < NUM_BUFFERS)
    {
        status = gzread(file, buf, BUFFER_LENGTH - 2);
        printf("STATUS: %d\n", status);
        buf[BUFFER_LENGTH - 1] = "\0";
        printf("%s\n", buf);
        counter++;
    }
    gzclose(file);
    printf("STATUS: %d\n", status); …
Run Code Online (Sandbox Code Playgroud)

c zlib

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