在Turbo Pascal中是否有类似于[‘A’..’Z’]的Python等价物?

huangapple go评论89阅读模式
英文:

Is there a python equivalent of ['A'..'Z'] in turbo pascal

问题

在 Turbo Pascal 中,可以使用以下表示方式快速创建包含并介于 'A' 和 'Z' 之间所有元素的列表:['A'..'Z']。

英文:

Like the topic, is there a (quick) way, possibly a notation, to achieve the same effect as in turbo pascal to rapid make a list of all elements containing and between 'A' and 'Z'.

In turbo pascal it could be written as ['A'..'Z']

答案1

得分: 3

import string
print(string.ascii_uppercase)

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
x = list(string.ascii_uppercase)
print(x)
['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']

英文:

I think the most elegant, simple and pythonic way is to use string module:

import string
print(string.ascii_uppercase)
>>> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
x = list(string.ascii_uppercase)
print(x)
>>> ['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']

答案2

得分: 2

不幸的是,在Python中没有其他更简洁、优雅的方法来完成这个任务。正如@Soren所说,对于字母,您可以使用 string.ascii_uppercasestring.ascii_lowercase,然后进行切片。例如,print(string.ascii_uppercase[3:7]) 将打印 "DEFG"。

如果您想要更通用、更易读,并且不局限于拉丁字母表字母,您必须编写一个类来实现您想要的功能。
我编写了这个非常简单的示例作为概念验证(它忽略了许多细节)。它应该适用于任何可迭代对象(尽管我只是用字符串进行了测试)。

class Slicer:
    def __init__(self, content):
        self.content = content
    
    def __getitem__(self, key: slice):
        if key.step is not None:
            return self.content[self.content.index(key.start) : self.content.index(key.stop)+1 : key.step]
        return self.content[self.content.index(key.start) : self.content.index(key.stop)+1]
    
    def __repr__(self):
        return f"Slicer({self.content!r})"

import string
letters = Slicer(string.ascii_uppercase)
print(letters)
# Slicer('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
print(letters["A":"H"])
# ABCDEFGH
print(letters["A":"H":2])
# ACEG

如果您想深入了解,它使用了Python中的 slice 对象,表示对象的序列: https://docs.python.org/3/glossary.html#term-slice

对于数字,您可以直接使用 range() 函数: list(range(1, 7)) 返回 [1, 2, 3, 4, 5, 6](它还支持步长)。

英文:

Unfortunately, there's no other way to do this in Python that's as compact and elegant. As @Soren said, for letters, you can use string.ascii_uppercase or string.ascii_lowercase, on which you can do slicing. For instance, print(string.ascii_uppercase[3:7]) prints "DEFG".

If you want something more generic, more readable, and not limited to latin alphabet letters, you have to write a class to approach what you want.
I wrote this very simple example as a proof of concept (it misses many little details). It should work with any iterable (even though I've only tested it with strings).

class Slicer:
    def __init__(self, content):
        self.content = content
    
    def __getitem__(self, key: slice):
        if key.step is not None:
            return self.content[self.content.index(key.start) : self.content.index(key.stop)+1 : key.step]
        return self.content[self.content.index(key.start) : self.content.index(key.stop)+1]
    
    def __repr__(self):
        return f"Slicer({self.content!r})"

import string
letters = Slicer(string.ascii_uppercase)
print(letters)
# Slicer('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
print(letters["A":"H"])
# ABCDEFGH
print(letters["A":"H":2])
# ACEG

If you want to dig further, it uses the slice object in Python, representing sequences of objects: https://docs.python.org/3/glossary.html#term-slice

For numbers, you can just use the range() function: list(range(1, 7)) returns [1, 2, 3, 4, 5, 6] (it also supports steps).

答案3

得分: 2

你可以在字符编号范围上使用map函数:

*letters, = map(chr, range(65, 91))  # A-Z

print(letters)

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

你可以使用解包操作符将多个范围合并在一起:

*alphanum, = *map(chr, range(65, 91)), "0123456789", *map(chr, range(97, 122))

或者,你也可以创建一个自己的简便函数:

def CHR(a, *b):
    return [*map(chr, range(ord(a[0]), ord(a[-1]) + 1))] + (CHR(*b) if b else [])

然后你可以根据需要重复使用它:

codeChars = CHR('A..Z', '-', '0..9')

print(codeChars)

['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', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
英文:

You can use map on a range of character numbers:

*letters, = map(chr,range(65,91)) # A-Z

print(letters)

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

You can combine multiple ranges using unpacking:

*alphanum, = *map(chr,range(65,91)), *"0123456789", *map(chr,range(97,122))

Alternatively you could create a shorthand function of you own:

def CHR(a,*b):
    return [*map(chr,range(ord(a[0]),ord(a[-1])+1))] + (CHR(*b) if b else [])

Which you can reuse as needed:

codeChars = CHR('A..Z','-','0..9')

print(codeChars)

['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', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8',
 '9']

答案4

得分: 0

如果您拥有icu4c和PyICU可用,可以使用Unicode集构建字符列表:

from icu import UnicodeSet
chars = list(UnicodeSet('[A-Z]'))
print(chars)
# ['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']

使用Unicode集,可以开发更复杂和精密的集合。例如,所有大写拉丁字母:

upper_latin = list(UnicodeSet('[[\p{Lu}] & [\p{Script=Latn}]]'))
英文:

If you have icu4c and PyICU available, it is possible to construct a list of characters using Unicode sets:

from icu import UnicodeSet
chars = list(UnicodeSet('[A-Z]'))
print(chars)  
# ['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']                                                          

Using Unicode sets, a lot more sophisticated and complex sets can be developed. For instance, all uppercase Latin script letters:

upper_latin = list(UnicodeSet('[[\p{Lu}] & [\p{Script=Latn}]]'))

huangapple
  • 本文由 发表于 2023年7月11日 03:59:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定