创建您自己的自定义字符串类:
class MyString:
def __init__(self, word):
self.word = word
def __lt__(self, other):
return self.word > other.word
def __eq__(self, other):
return self.word == other.word
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
heapq.heappush(heap, MyString(word))
Run Code Online (Sandbox Code Playgroud)
通过将字符值设为负数来反转字符串?全小写字符串的键示例:
lambda s: ''.join(chr(ord('z') - ord(c) + ord('a')) for c in s)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1554 次 |
| 最近记录: |