I am trying to get a list/array of the columns of an Excel spreadsheet, from A until ZZ.
["A","B","C", ... , "ZX","ZY","ZZ"]
What I have tried so far:
(Javascript)
const alphabetList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
const alphabetListCopy = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
for (var i=0; i < alphabetListCopy.length; i ){
for (var j=0; i < alphabetListCopy.length; j ){
alphabetList.push(alphabetList[i][j]);
}
}
console.log(alphabetList);
and this returns Error: Invalid array length
(I did the loop with alphabetListCopy to avoid this error https://forum.freecodecamp.org/t/push-new-element-into-array-using-for-loop/225401#:~:text=Your code crashed,push an element.)
In python I tried:
alphabet = "A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z".split("-")
for letter in alphabet:
for a in alphabet:
alphabet.append(letter a)
print(alphabet)
and this returns a MemoryError (I assumed it created an infinite loop)
CodePudding user response:
The code you are providing is making an infinite loop because you are appending values to an array and you are trying to add a value to a 2-dimension array while alphabetList
is a 1-dimansion.
If you want to have a list of the values from A to ZZ in Javascript you can do this instead :
const alphabetList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
// here we do not want to make it const since we are populating it
let alphabetListCopy = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
for(i = 0; i < alphabetList.length; i ) {
for(j = 0; j < alphabetList.length; j ) {
alphabetListCopy.push(`${alphabetList[i]}${alphabetList[j]}`) // we add to the array the new value
}
}
CodePudding user response:
For an easy approach to get the alphabet:
alphabet = [chr(i) for i in range(65, 91)]
To get all combinations, itertools has a nice collection of tools:
import itertools
alpha_combi = [''.join(subset) for subset in itertools.combinations_with_replacement(alphabet, 2)]
alphabet = alpha_combi
where each subset
is a tuple of two letters.
CodePudding user response:
Code:
import string
alphabet = list(string.ascii_uppercase)
new= []
for alpha in alphabet:
for a in range(len(alphabet)):
new.append(alpha alphabet[a])
len(alphabet new)
ternary:
alphabet [alphabet[a] alpha for a in range(len(alphabet)) for alpha in alphabet]
New list length 702