小编Rya*_*yan的帖子

使用itertools.product并想要为一个值设定种子

所以我写了一个小脚本来从网站上下载图片.它通过一个7 alpha字符值,其中第一个字符始终是一个数字.问题是如果我想停止脚本并再次启动它我必须从头开始.

我可以用我得到的最后一个值以某种方式播种itertools.product,所以我不必再次通过它们了.

感谢您的任何意见.

这是代码的一部分:

numbers = '0123456789'
alnum = numbers + 'abcdefghijklmnopqrstuvwxyz'

len7 = itertools.product(numbers, alnum, alnum, alnum, alnum, alnum, alnum) # length 7

for p in itertools.chain(len7):
    currentid = ''.join(p) 

    #semi static vars
    url = 'http://mysite.com/images/'
    url += currentid

    #Need to get the real url cause the redirect
    print "Trying " + url
    req = urllib2.Request(url)
    res = openaurl(req)
    if res == "continue": continue
    finalurl = res.geturl()

    #ok we have the full url now time to if it …
Run Code Online (Sandbox Code Playgroud)

python image download seed python-itertools

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

如何迭代字母和数字

我想知道如何在Python中迭代一系列条件.

  1. 具有2-6个较低字母或数字字符的字符串
  2. 第一个字符总是一个数字

所以一个短暂的进展将是:

1a
1b
1c
...
1aa
1ab
1ac
...
2aaa
2aab
2aac

etc.
Run Code Online (Sandbox Code Playgroud)

一个可以做前两个的可怕例子是

##Loop through 1a-z0-9
start = '1'
l = 97
while l < 123:
    num = start
    num += chr(l)
    print num
    l += 1

l = 48
while l < 58:
    num = start
    num += chr(l)
    print num
    l += 1
Run Code Online (Sandbox Code Playgroud)

我找到了itertools,但找不到好的例子.

python loops numbers python-itertools letters

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

C++删除对象在需要返回时使用new创建

当我需要返回结果时,如何删除我用new分配的空间.我使用了这个,所以如果我找不到一个对象,我会返回一个带有伪标记值的对象.

ClassObject* ClassObjectTwo::find(string findid) {
        ClassObject *sendback;
        bool found;

        for(vector<ClassObject>::iterator it = data.begin(); it != data.end(); it++) {
                if ( it->get_id() == findid) {
                        found = true;
                        sendback = &(*it);
                }
        }

        if(!found) {
                sendback = new ClassObject;
                sendback->set_key(100);
        }

        return sendback;
}
Run Code Online (Sandbox Code Playgroud)

或者这不是一个问题,因为它超出范围时被销毁.我能想到的唯一其他解决方案是将对象放在构造函数中并通过构造函数进行删除.我只是不想为一个函数添加变量.

c++ return delete-operator

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

返回字符串向量

我对vector的所有命名空间以及如何在我的类中正确返回字符串向量感到困惑.这是代码:

main.cpp中

#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include "lab1.h"

using namespace std;
readwords wordsinfile;
words wordslist;

int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) {
            // Looks like we have no arguments and need do something about it
            // Lets tell the user
            cout << "Usage: " << argv[0] <<" <filename>\n";
            exit(1);
    } else {
            // Yeah we have arguements so lets make sure the file exists and …
Run Code Online (Sandbox Code Playgroud)

c++ string vector

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