问题:Python:如何打印范围az?

1.打印: abcdefghijklmn

2.每秒: acegikm

3.将url索引附加到{ hello.com/、hej.com/、…、hallo.com/}:hello.com/a hej.com/b … hallo.com/n

1. Print a-n: a b c d e f g h i j k l m n

2. Every second in a-n: a c e g i k m

3. Append a-n to index of urls{hello.com/, hej.com/, …, hallo.com/}: hello.com/a hej.com/b … hallo.com/n


回答 0

>>> import string
>>> string.ascii_lowercase[:14]
'abcdefghijklmn'
>>> string.ascii_lowercase[:14:2]
'acegikm'

要执行网址,您可以使用类似以下内容的网址

[i + j for i, j in zip(list_of_urls, string.ascii_lowercase[:14])]
>>> import string
>>> string.ascii_lowercase[:14]
'abcdefghijklmn'
>>> string.ascii_lowercase[:14:2]
'acegikm'

To do the urls, you could use something like this

[i + j for i, j in zip(list_of_urls, string.ascii_lowercase[:14])]

回答 1

假设这是一项家庭作业;-)-无需调用库等-它可能希望您将chr / ord与range()一起使用,如下所示:

for i in range(ord('a'), ord('n')+1):
    print chr(i),

对于其余的内容,只需要使用range()多一点

Assuming this is a homework ;-) – no need to summon libraries etc – it probably expect you to use range() with chr/ord, like so:

for i in range(ord('a'), ord('n')+1):
    print chr(i),

For the rest, just play a bit more with the range()


回答 2

提示:

import string
print string.ascii_lowercase

for i in xrange(0, 10, 2):
    print i

"hello{0}, world!".format('z')

Hints:

import string
print string.ascii_lowercase

and

for i in xrange(0, 10, 2):
    print i

and

"hello{0}, world!".format('z')

回答 3

for one in range(97,110):
    print chr(one)
for one in range(97,110):
    print chr(one)

回答 4

获取具有所需值的列表

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']

Get a list with the desired values

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))

or

import string
string.letters
string.uppercase
string.digits

This solution uses the ASCII table. ord gets the ascii value from a character and chr vice versa.

Apply what you know about lists

>>> 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']

回答 5

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']
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']

回答 6

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']

and

for c in list(string.ascii_lowercase)[:5]:
    ...operation with the first 5 characters

回答 7

myList = [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']
myList = [chr(chNum) for chNum in list(range(ord('a'),ord('z')+1))]
print(myList)

Output

['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']

回答 8

#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)]
#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)]

回答 9

这个问题的答案很简单,只需列出一个名为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

The answer to this question is simple, just make a list called ABC like so:

ABC = ['abcdefghijklmnopqrstuvwxyz']

And whenever you need to refer to it, just do:

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)

Also try this to break ur device :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

回答 10

尝试:

strng = ""
for i in range(97,123):
    strng = strng + chr(i)
print(strng)

Try:

strng = ""
for i in range(97,123):
    strng = strng + chr(i)
print(strng)

回答 11

这是您的第二个问题:string.lowercase[ord('a')-97:ord('n')-97:2]因为97==ord('a')-如果您想学习一点,您应该自己弄清楚其余的部分;-)

This is your 2nd question: string.lowercase[ord('a')-97:ord('n')-97:2] because 97==ord('a') — if you want to learn a bit you should figure out the rest yourself ;-)


回答 12

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']
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']

回答 13

我希望这有帮助:

import string

alphas = list(string.ascii_letters[:26])
for chr in alphas:
 print(chr)

I hope this helps:

import string

alphas = list(string.ascii_letters[:26])
for chr in alphas:
 print(chr)

回答 14

关于狼吞虎咽的答案。

邮编功能,充分说明,返回a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. [...]构造称为列表理解,很酷的功能!

About gnibbler’s answer.

Zip -function, full explanation, returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. [...] construct is called list comprehension, very cool feature!


回答 15

另一种方式

  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结合列表理解和str.format

Another way to do it

  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

Result

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

How this differs from the other replies

  • iterate over an arbitrary number of base urls
  • cycle through the urls and do not stop until we run out of letters
  • use enumerate in conjunction with list comprehension and str.format

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。