1. 打印 a-n: a b c d e f g h i j k l m n
2. a-n 中的每秒元素: a c e g i k m
3. 将 a-n 添加到 urls{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])]
str.lowercase
是与语言环境相关的,所以一开始并不是最好的选择。我已经在我的答案中替换了它。 - John La Rooy假设这是一份作业 ;-) - 不需要调用库等其他东西 - 它可能希望您使用range()和chr/ord,像这样:
for i in range(ord('a'), ord('n')+1):
print chr(i),
对于其余部分,只需多使用一下range()函数即可。
提示:
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
这个解决方案使用了 ASCII 表。 ord
可以从一个字符获取 ASCII 值,而 chr
则相反。
>>> 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']
并且
for c in list(string.ascii_lowercase)[:5]:
...operation with the first 5 characters
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)
。 - Alex WillisonmyList = [chr(chNum) for chNum in list(range(ord('a'),ord('z')+1))]
print(myList)
输出
['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']
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
string.printable[10:36]
# abcdefghijklmnopqrstuvwxyz
string.printable[10:62]
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
range
和chr()
,另一种使用string
中的现成列表,许多人可能没有想到。 - Lennart Regebro