1. Imprime un: abcdefghijklmn
2. Cada segundo en un: acegikm
3. Agregue un al índice de URL {hello.com/, hej.com/, ..., hallo.com/}: hello.com/a hej.com/b ... hallo.com/n
>>> import string
>>> string.ascii_lowercase[:14]
'abcdefghijklmn'
>>> string.ascii_lowercase[:14:2]
'acegikm'
Para hacer las URL, podría usar algo como esto
[i + j for i, j in zip(list_of_urls, string.ascii_lowercase[:14])]
str.lowercase
depende de la configuración regional, por lo que no fue la mejor opción en primer lugar. Lo reemplacé en mi respuesta
Suponiendo que esto es una tarea ;-) - no es necesario convocar bibliotecas, etc. - probablemente espere que use range () con chr / ord, así:
for i in range(ord('a'), ord('n')+1):
print chr(i),
Por lo demás, juega un poco más con el rango ()
Sugerencias:
import string
print string.ascii_lowercase
y
for i in xrange(0, 10, 2):
print i
y
"hello{0}, world!".format('z')
for one in range(97,110):
print chr(one)
small_letters = map(chr, range(ord('a'), ord('z')+1))
big_letters = map(chr, range(ord('A'), ord('Z')+1))
digits = map(chr, range(ord('0'), ord('9')+1))
o
import string
string.letters
string.uppercase
string.digits
Esta solución utiliza la tabla ASCII . ord
obtiene el valor ascii de un carácter y chr
viceversa.
>>> small_letters = map(chr, range(ord('a'), ord('z')+1))
>>> an = small_letters[0:(ord('n')-ord('a')+1)]
>>> print(" ".join(an))
a b c d e f g h i j k l m n
>>> print(" ".join(small_letters[0::2]))
a c e g i k m o q s u w y
>>> s = small_letters[0:(ord('n')-ord('a')+1):2]
>>> print(" ".join(s))
a c e g i k m
>>> urls = ["hello.com/", "hej.com/", "hallo.com/"]
>>> print([x + y for x, y in zip(urls, an)])
['hello.com/a', 'hej.com/b', 'hallo.com/c']
import string
print list(string.ascii_lowercase)
# ['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']
tuple(string.ascii_lowercase)
import string
print list(string.ascii_lowercase)
# ['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']
y
for c in list(string.ascii_lowercase)[:5]:
...operation with the first 5 characters
myList = [chr(chNum) for chNum in list(range(ord('a'),ord('z')+1))]
print(myList)
Salida
['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']
#1)
print " ".join(map(chr, range(ord('a'),ord('n')+1)))
#2)
print " ".join(map(chr, range(ord('a'),ord('n')+1,2)))
#3)
urls = ["hello.com/", "hej.com/", "hallo.com/"]
an = map(chr, range(ord('a'),ord('n')+1))
print [ x + y for x,y in zip(urls, an)]
La respuesta a esta pregunta es simple, solo haga una lista llamada ABC así:
ABC = ['abcdefghijklmnopqrstuvwxyz']
Y siempre que necesite consultarlo, simplemente haga:
print ABC[0:9] #prints abcdefghij
print ABC #prints abcdefghijklmnopqrstuvwxyz
for x in range(0,25):
if x % 2 == 0:
print ABC[x] #prints acegikmoqsuwy (all odd numbered letters)
También intente esto para romper su dispositivo: D
##Try this and call it AlphabetSoup.py:
ABC = ['abcdefghijklmnopqrstuvwxyz']
try:
while True:
for a in ABC:
for b in ABC:
for c in ABC:
for d in ABC:
for e in ABC:
for f in ABC:
print a, b, c, d, e, f, ' ',
except KeyboardInterrupt:
pass
Tratar:
strng = ""
for i in range(97,123):
strng = strng + chr(i)
print(strng)
Esta es tu segunda pregunta: string.lowercase[ord('a')-97:ord('n')-97:2]
porque 97==ord('a')
, si quieres aprender un poco, deberías averiguar el resto tú mismo ;-)
Espero que esto ayude:
import string
alphas = list(string.ascii_letters[:26])
for chr in alphas:
print(chr)
Sobre la respuesta de gnibbler.
Función zip, explicación completa , la a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
[...]
construcción de devoluciones se llama comprensión de lista , ¡característica muy interesante!
list(string.ascii_lowercase)
['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']
import string
pass
aalist = list(string.ascii_lowercase)
aaurls = ['alpha.com','bravo.com','chrly.com','delta.com',]
iilen = aaurls.__len__()
pass
ans01 = "".join( (aalist[0:14]) )
ans02 = "".join( (aalist[0:14:2]) )
ans03 = "".join( "{vurl}/{vl}\n".format(vl=vjj[1],vurl=aaurls[vjj[0] % iilen]) for vjj in enumerate(aalist[0:14]) )
pass
print(ans01)
print(ans02)
print(ans03)
pass
abcdefghijklmn
acegikm
alpha.com/a
bravo.com/b
chrly.com/c
delta.com/d
alpha.com/e
bravo.com/f
chrly.com/g
delta.com/h
alpha.com/i
bravo.com/j
chrly.com/k
delta.com/l
alpha.com/m
bravo.com/n
enumerate
junto con la comprensión de listas y str.format
range
ychr()
otro las listas ya hechas, en lasstring
que mucha gente no pensaría.