【问题标题】:How to make a list of characters in Python [closed]如何在Python中制作字符列表[关闭]
【发布时间】:2012-12-03 21:31:39
【问题描述】:

我的考试问题要求我输入两个字母,并显示两个输入之间的字母表中的所有字母(包括)。它还需要按照用户输入它们的顺序执行此操作(因此 GA 生成 GFEDCBA 而不是 ABCDEFG)。我将如何处理这项任务?

【问题讨论】:

  • comments like "learn to do it yourself" are not helpful. 是的,我想你是对的。如果您正在考试中,那么您就没有多少时间可以学习任何东西了。
  • How do I iterate through the alphabet in Python? 可能会对您有所帮助 - 但请尝试在考试前澄清这些问题...
  • One liner... print "%s" % list((chr(i) for i in range(ord(begin), ord(end) + 1 * (ord(end)-ord (开始))/abs(ord(end)-ord(begin)), (ord(end)-ord(begin))/abs(ord(end)-ord(begin)))))
  • 哦,对不起,我好像引起了一些混乱......我不是在考试,它只是作为一些家庭作业的练习试卷:)
  • 试图在人们会说话之前堵住他们并不是提问的好方法。 -1 仅此而已。

标签: python list character


【解决方案1】:

我真的不认为这值得回答,但正如@martineau 在his comment 中所说,在评论中放置这么多代码不是一个好主意......所以就这样吧:

>>> begin="A"
>>> end="G"
>>> print "%s" % list((chr(i) for i in range(ord(begin), ord(end) + 1 * (ord(end)-ord(begin))/abs(ord(end)-ord(begin)), (ord(end)-ord(begin))/abs(ord(end)-ord(begin))))) 
['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> begin="G"
>>> end="A"
>>> print "%s" % list((chr(i) for i in range(ord(begin), ord(end) + 1 * (ord(end)-ord(begin))/abs(ord(end)-ord(begin)), (ord(end)-ord(begin))/abs(ord(end)-ord(begin))))) 
['G', 'F', 'E', 'D', 'C', 'B', 'A']

唯一稍微相关的部分是chrord,以及(ord(end)-ord(begin))/abs(ord(end)-ord(begin))开始> 结束 时得到-1 的“技巧”,不过...

编辑:正如@martineau 在another comment 中指出的那样...您可以使用join 制作更大的(!)一个衬里并获得一个字符串(而不是列表) .

>>> begin="G"
>>> end="A"
>>> print "".join((chr(i) for i in range(ord(begin), ord(end) + 1 * (ord(end)-ord(begin))/abs(ord(end)-ord(begin)), (ord(end)-ord(begin))/abs(ord(end)-ord(begin)))))
GFEDCBA

...这是一小段代码... :D

【讨论】:

  • 如果将list(...) 更改为''.join(...),结果将是一个类似ABCDEFG 的字符串。
  • 你不需要"".join(list((chr(...中的list(
  • @martineau:没错……已编辑:)
【解决方案2】:
>>> import string
>>> def letterList (start, end):
        # add a character at the beginning so str.index won't return 0 for `A`
        a = ' ' + string.ascii_uppercase

        # if start > end, then start from the back
        direction = 1 if start < end else -1

        # Get the substring of the alphabet:
        # The `+ direction` makes sure that the end character is inclusive; we
        # always need to go one *further*, so when starting from the back, we
        # need to substract one. Here comes also the effect from the modified
        # alphabet. For `A` the normal alphabet would return `0` so we would
        # have `-1` making the range fail. So we add a blank character to make
        # sure that `A` yields `1-1=0` instead. As we use the indexes dynamically
        # it does not matter that we have changed the alphabet before.
        return a[a.index(start):a.index(end) + direction:direction]

>>> letterList('A', 'G')
'ABCDEFG'
>>> letterList('G', 'A')
'GFEDCBA'
>>> letterList('A', 'A')
'A'

请注意,此解决方案允许使用任何类型的字母。我们可以设置a = ' ' + string.ascii_uppercase + string.ascii_lowercase 并得到这样的结果:

>>> letterList('m', 'N')
'mlkjihgfedcbaZYXWVUTSRQPON'

当您完全支持 unicode 时,谁需要 ASCII?

>>> a = ' あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわを'
>>> letterList('し', 'ろ')
'しすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろ'

【讨论】:

  • 似乎真正的“完全 unicode 支持”意味着不必预先定义要使用的字母表......
  • @martineau 如果你想使用 Unicode 的排序并接受有很多代码点是空的,当然。
  • 是的,我知道。我写了一个,但是当我准备提交时,问题就关闭了。无论如何,可能比 OP 感兴趣的要多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多