【发布时间】:2018-07-06 06:10:47
【问题描述】:
我有一个字符串列表,我必须根据一些搜索字符串对其进行排序。
My Search String is Home Depot
MyList = ['Depot Home','Rollins Home Furniture','HomeDepot 1346','Home Depot']
预期输出:
Sorted list: ['HomeDepot 1346','Home Depot','Depot Home','Rollins Home Furniture']
在排序列表中,第一个元素是搜索字符串删除空格的精确匹配,第二个也是与空格精确匹配的元素,第三个元素是 Depot 的部分匹配(按字母顺序),第四个元素也是 Home 的部分匹配(字母顺序)在仓库之后下订单)
到目前为止我做了什么:
searchquery_startswith=[w for w in Mylist if w.startswith('HOME DEPOT'.strip())]
searchquery_substring= [w for w in Mylist if ('HOME DEPOT' in w and w not in searchquery_startswith)]
我知道我可以做这样的事情,但我正在寻找更多的 Pythonic 方式来实现这一点。感谢所有帮助
【问题讨论】:
标签: python python-3.x list sorting