Python:如何打印范围az?
1.打印一个: abcdefghijklmn
2.每一秒: acegikm
3.附加一个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'
做url,你可以使用这样的东西
[i + j for i, j in zip(list_of_urls, string.ascii_lowercase[:14])]
假设这是一个家庭作业;-) – 不需要召唤图书馆等 – 它可能期望你用chr / ord使用范围(),如下所示:
for i in range(ord('a'), ord('n')+1): print chr(i),
剩下的就是用范围()
提示:
import string print string.ascii_lowercase
和
for i in xrange(0, 10, 2): print i
和
"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))
要么
import string string.letters string.uppercase string.digits
该解决scheme使用ASCII表 。 ord
从一个字符获取ascii值,反之亦然。
应用你对列表的了解
>>> small_letters = map(chr, range(ord('a'), ord('z')+1)) >>> an = small_letters[0:(ord('n')-ord('a')+1)] >>> print(" ".join(an)) abcdefghijklmn >>> print(" ".join(small_letters[0::2])) acegikmoqsuwy >>> s = small_letters[0:(ord('n')-ord('a')+1):2] >>> print(" ".join(s)) acegikm >>> 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']
#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)]
这是你的第二个问题: string.lowercase[ord('a')-97:ord('n')-97:2]
因为97==ord('a')
– 如果你想学习一点你应该找出其余的自己;-)
尝试:
str = "" for i in range(97,123): str = str + chr(i) print(str)
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']
这个问题的答案很简单,只需制作一个名为ABC的列表即可:
ABC = ['abcdefghijklmnopqrstuvwxyz']
而且每当你需要引用它,只要做:
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)
也试试这个打破你的设备: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
关于gnibbler的回答。
Zip函数, 完整的解释 ,返回a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
构造被称为列表理解 ,非常酷的function!