用PHP或Python生成一个从a到可能ZZZ的数组

-2 投票
5 回答
826 浏览
提问于 2025-04-16 22:44

假设我有一个数组,里面有一个未知的字符串,它的样子是这样的:

$arr = array(
'string 1',
'string 2',
'string 3',
'string 4',
'string 5',
// up to a certain unknown amount

1. 我们该怎么给它们赋值呢?

'string 1' => a, 
'string 2' => b,
'string 26' => z, 
'string 27' => A,
'string 52' => Z,
'string 53' => aa,
// up to a certain unknown amount possibly until ZZZZ, or maybe more.

更新一下,这是我到目前为止做的事情:

function num2a($a){

    # A-Z
    for ($i=65; $i < 91 ; $i++) { 
        #echo chr($i);
        $arr[] = chr($i);
    }

    # a-z
    for ($i=97; $i < 123 ; $i++) { 
        #echo chr($i);
        $arr[] = chr($i);
    }

    // a-Z
    if ( 0 <= $a && $a <= 51 ) {
        echo $arr[$a];
    }

    // aa-ZZ not done
    if ( 52 <= $a && $a <= 53 ) {

        for ($x=0; $x < 1; $x++) {
            # A = 0  
            # a = 26
            # somehow if i put $a = 52 it will do a aa
            $a = $a - 26;
            $arr[$a] = $arr[$a] . $arr[$a];
            echo $arr[$a];
        }
    }
}

5 个回答

0

好吧,你可以很简单地给它数字键:

$strArray = explode(",",$string);
1

警告,这里是伪代码。这只是一个想法,首先创建一个像这样的地图

map['1'->'26'] = 'a'->'z'
map['27'->'52'] = 'A'->'Z'

现在,从数据列表中你只需要数字。你可以用这个数字来获取相应的值,像这样

int numFromData = getNumber('string 53')
while( numFromData > 0){
   int mapKey = numFromData % 52; //since we have 52 mapped values
   print( map[ mapKey] ); 
   numFromData /= 52; 
}

我觉得这样应该可以,但不能保证,反正这个想法是对的。

2

在Python中,使用生成器可以很容易地创建无限数组,以及任意长度的数组和延迟计算的数组等。你想要做的就是将这种东西映射到键上,得到一个无限长的、延迟计算的列表,或者说是一个生成器

import itertools
import string

首先,你需要用到itertools库,它里面有很多方便的函数,可以用来处理无限大小的数组。string库则包含了一些标准的字符串常量。

def lower_then_upper():
    return iter(string.letters)

这里的iter并不是绝对必要的,但它可以把标准列表转换成生成器,以保持一致性。

def get_n(n=2):
    generators = []
    for gen in range(n):
        generators.append(lower_then_upper())
    tuples = itertools.product(*generators)
    return itertools.imap(''.join, tuples)

接下来我们开始把这些东西组合在一起,这个函数会返回由字母组成的固定长度字符串列表。逐行来看,tuples之前的部分确保每个字母位置都有一个生成器来生成所有可能的字符串。也就是说,如果n=3,就会创建三个a-Z的列表。

下面,itertools.product会对所有可用的东西进行笛卡尔积运算,幸运的是在Python中,它会先迭代最后一个,所以对于n=2,你会得到:`['aa', 'ab', ..., 'ZX', 'ZY', 'ZZ']

最后一行,itertools.imap会将''.join这个函数应用到tuples中的每一项。到现在为止,它们的形式是(('a', 'a'), ('a', 'b'), …,这个步骤确保它们变成字符串,就像''.join(['a', 'a']) == 'aa'一样。

def get_infinite():
    n = 0
    while True:
        n += 1
        for item in get_n(n):
            yield item

这里就是魔法发生的地方。它会先处理完n=1的迭代器,然后再处理n=2,以此类推。

def inf_hello():
    while True:
        yield "hello world"

这只是一个演示用的无限列表。

mapped = itertools.izip(get_infinite(), inf_hello())

现在,我们把它们组合在一起,得到一个无限的键/值对数组:

>>> list(itertools.islice(mapped, 1000, 1010))
[('sm', 'hello world'), ('sn', 'hello world'), ('so', 'hello world'), ('sp', 'hello world'), ('sq', 'hello world'), ('sr', 'hello world'), ('ss', 'hello world'), ('st', 'hello world'), ('su', 'hello world'), ('sv', 'hello world')]

如我们所见,如果我们查看1000个元素的内容。

另外,我们也可以给它一个长列表,得到一个字典:

>>> numbers = dict(itertools.izip(get_infinite(), range(0, 10000)))
>>> print numbers['a'], numbers['aa'], numbers['caW']
0 52 8212

希望这对你有帮助。

撰写回答