猿问

尝试按数字和字母顺序对这个地址列表进行排序

我已经设法对这个地址列表进行了数字排序,但我的问题是我试图按数字和字母顺序对它们进行排序。


 address_list = ['100 Parker St, Springfield, MA','131 Parker St, Springfield, MA', '102 Boston Rd, Springfield, MA', '181 Boston Rd, Springfield, MA', '150 King St, Springfield, MA','103 King St, Springfield, MA']


address_list.sort()


for address in sorted(address_list):

    print(address)


凤凰求蛊
浏览 131回答 2
2回答

千万里不及你

编辑:看来您甚至不需要实施__eq__. 下面的示例无需实现即可工作__eq__。您可以将每个地址映射到实现的单独类,__lt__然后__eq__根据该类进行排序。例如:from __future__ import annotationsfrom typing import Text, Tupledef parse_address(address: Text) -> Tuple[int, Text]:&nbsp; &nbsp; """I haven't extensively tested this yet.."""&nbsp; &nbsp; first_space = address.find(' ')&nbsp; &nbsp; if first_space == -1:&nbsp; &nbsp; &nbsp; &nbsp; raise ValueError('Invalid address')&nbsp; &nbsp; return int(address[:first_space]), address[first_space + 1:]class CustomComparator(object):&nbsp; &nbsp; def __init__(self, address: Text):&nbsp; &nbsp; &nbsp; &nbsp; self._full_address = address&nbsp; &nbsp; &nbsp; &nbsp; self._number, self._address = parse_address(address)&nbsp; &nbsp; def __lt__(self, other: CustomComparator) -> bool:&nbsp; &nbsp; &nbsp; &nbsp; if self._number != other._number:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self._number < other._number&nbsp; &nbsp; &nbsp; &nbsp; return self._address < other._address&nbsp; &nbsp; # __str__ and __repr__ are just so printing appears cleaner in this case.&nbsp; &nbsp; def __str__(self) -> Text:&nbsp; &nbsp; &nbsp; &nbsp; return self._full_address&nbsp; &nbsp; def __repr__(self) -> Text:&nbsp; &nbsp; &nbsp; &nbsp; return str(self)address_list = ['100 Parker St, Springfield, MA', '131 Parker St, Springfield, MA', '102 Boston Rd, Springfield, MA',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '181 Boston Rd, Springfield, MA', '150 King St, Springfield, MA', '103 King St, Springfield, MA',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '103 Boston Rd, Spingfield, MA']address_list = [CustomComparator(address) for address in address_list]address_list.sort()print('\n'.join([str(address) for address in address_list]))这将首先按数字排序地址,然后按字母顺序排序。在此示例中,它打印:100 Parker St, Springfield, MA102 Boston Rd, Springfield, MA103 Boston Rd, Spingfield, MA103 King St, Springfield, MA131 Parker St, Springfield, MA150 King St, Springfield, MA181 Boston Rd, Springfield, MA

紫衣仙女

你是什么意思按数字和字母顺序如果您的意思是您想按数字排序第一个,然后按数字旁边的字母排序,并且数字具有固定长度(如您的示例中的 3)sorted功能会自动执行此操作。如果数字长度不相等,您可以在前面填充一些额外的零并将其用作排序函数的键address_list = ['100 Parker St, Springfield, MA','13 Parker St, Springfield, MA', '102 Boston Rd, Springfield, MA', '18 Boston Ad, Springfield, MA','103 Boston St, Springfield, MA','103 Aing St, Springfield, MA', '150 King St, Springfield, MA','103 King St, Springfield, MA']pad_zeros = lambda x: "0"*(6-len(x[1])) + x[1]+ " "key = lambda s: re.sub("(^\d*)\s?", pad_zeros, s)sorted(address_list, key=key)输出['13 Parker St, Springfield, MA',&nbsp;'18 Boston Ad, Springfield, MA',&nbsp;'100 Parker St, Springfield, MA',&nbsp;'102 Boston Rd, Springfield, MA',&nbsp;'103 Aing St, Springfield, MA',&nbsp;'103 Boston St, Springfield, MA',&nbsp;'103 King St, Springfield, MA',&nbsp;'150 King St, Springfield, MA']
随时随地看视频慕课网APP

相关分类

Python
我要回答