give an array a-z until possibly ZZZ... key with php or python
Let's say i have an unknown string in an array, it will look like
$arr = array(
'string 1',
'string 2',
'string 3',
'string 4',
'string 5',
// up to a certain unknown amount
1. How can we give them a value like below?
'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.
update heres what ive done so far
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];
}
}
开发者_如何学编程}
In Python it's relatively easy to have an infinite array using generators, as well as arbitrarily long ones, lazily evaluated ones, etc. What you'd want to do to map this kind of thing to keys is to have an infinitely long, lazily evaluated list, or generator.
import itertools
import string
First you want itertools, which has lots of handy functions for manipulating infinite size arrays. string includes some standard string constants.
def lower_then_upper():
return iter(string.letters)
iter
here isn't strictly required, but it will turn the standard list into a generator, for consistency.
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)
Here we start putting things together, this function will return the list of strings of a fixed length made of up the letters. Line by line, the bits before tuples
ensures that there is a generator for all strings available for each letter position. i.e. three a-Z lists would be created if n=3.
Below, itertools.product
does a cartesian product of all the things available, which in Python luckily iterates the last first, so you get for n=2: `['aa', 'ab', ..., 'ZX', 'ZY', 'ZZ']
.
The final line, itertools.imap
applies the function ''.join
to every item in tuples. Until now they were of the form (('a', 'a'), ('a', 'b'), …
, this ensures they are a string, in the same way that ''.join(['a', 'a']) == 'aa'
.
def get_infinite():
n = 0
while True:
n += 1
for item in get_n(n):
yield item
Here is where the magic happens. It will exhaust the iterator for n=1 before going on to exhaust n=2, and so on.
def inf_hello():
while True:
yield "hello world"
This is just a demo infinite list
mapped = itertools.izip(get_infinite(), inf_hello())
Now, we zip them together to get an infinite array of key/value pairs:
>>> 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')]
as we can see if we introspect it 1000 elements in.
Alternatively, we can give it a long list and get a dictionary:
>>> numbers = dict(itertools.izip(get_infinite(), range(0, 10000)))
>>> print numbers['a'], numbers['aa'], numbers['caW']
0 52 8212
Hope this helps.
Warning, in psuedo-code. This is an idea, first create a map like so
map['1'->'26'] = 'a'->'z'
map['27'->'52'] = 'A'->'Z'
Now from the list of data all you need is the number. You can use the number to get appropriate values like so,
int numFromData = getNumber('string 53')
while( numFromData > 0){
int mapKey = numFromData % 52; //since we have 52 mapped values
print( map[ mapKey] );
numFromData /= 52;
}
I think that should work, no guarantees, but the idea is correct.
Well you can give it numeric keys easily:
$strArray = explode(",",$string);
$str = ""; // Your String
$results = array();
$input = explode(",", $str);
foreach($input as $v) {
$value = explode("=>", $v);
if(!isset($results[$value[0]])) {
$results[$value[0]] = $value[1];
}
}
1) If you don't really need upper case
for($i="a";$i!="bc";++$i)
print $i."\n";
精彩评论