按字符串中的数字排序字符串列表?

时间:2018-01-24 01:56:56

标签: python python-3.x sorting

我有以下字符串列表。然后,我想用每个元素中的数字对它进行排序。 sorted失败,因为它无法处理103之间的订单。我可以想象,如果我使用re,我可以做到。但它并不有趣。你们有很好的实施想法吗?我想这个代码是python 3.x.

names = [
'Test-1.model',
'Test-4.model',
'Test-6.model',
'Test-8.model',
'Test-10.model',
'Test-20.model'
]
number_sorted = get_number_sorted(names)
print(number_sorted)
'Test-20.model'
'Test-10.model'
'Test-8.model'
'Test-6.model'
'Test-4.model'
'Test-1.model'

7 个答案:

答案 0 :(得分:4)

关键是......密钥

import UIKit

class List: NSObject {

    var name: String
    var items: [String?]
    var shared: String?

    init(name: String, items: [String], shared: String?) {
        self.name = name
        self.items = items
        self.shared = shared
    }

}

enum ListType {
    case apply
    case view
}

class ListViewController: UITableViewController {

    //Initialize Variables Here
    let cellId = "cellId"

    var lists: [List] = [
        List(name: "Veggies", items: ["Fruit Loops", "Pasta"], shared: nil),
        List(name: "11/17/18", items: ["Eggs", "Green Beans", "Pirate Booty", "Bread", "Milk"], shared: nil),
        List(name: "Fruits", items: ["Fruit Loops", "Oranges"], shared: nil),
        List(name: "Red Foods", items: ["Apples", "Tomatoes", "Watermelon", "Cherries"], shared: nil),
        List(name: "Grains", items: ["Bread Crumbs", "Pasta", "Rice", "Chicken Flavored Rice"], shared: nil)
    ]

    //Setup Enums and Switches
    var listType: ListType!


    override func viewDidLoad() {
        super.viewDidLoad()

        //Things that both cases have in common:
        navigationController?.navigationBar.prefersLargeTitles = true

        let addBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(newList)) //+ Button
        addBarButtonItem.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 27)!], for: .normal)
        addBarButtonItem.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 27)!], for: .selected)

        //SWITCH STATEMENT
        switch listType as ListType {

        case ListType.apply:
            //Apply Begin
            navigationItem.title = "Apply List"

            //Setup Navigation Bar Items
            let locateBarButtonItem = UIBarButtonItem(title: "Locate Item", style: .plain, target: self, action: nil)

            //Add items to navigation bar
            navigationItem.rightBarButtonItems = [addBarButtonItem, locateBarButtonItem]
            //Apply End
        case ListType.view:
            //View Begin
            //Setup Navigation Bar
            navigationItem.title = "My Lists"

            //Add items to navigation bar
            navigationItem.rightBarButtonItem = addBarButtonItem
            navigationItem.leftBarButtonItem = editButtonItem

            //View End
        }
        //SWITCH STATEMENT END


        //Register TableView with Id: cellId
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)

    }

    @objc func newList() {
        let newListVC = NewListViewController()
        newListVC.pageControlFunc(pageView: PageView.name)
        navigationController?.pushViewController(newListVC, animated: true) //Then pushes listVC
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return lists.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

        cell.textLabel?.text = self.lists[indexPath.row].name //Then sets cell of indexPath text = lists value of index path

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let listItemsVC = ListItemsViewController()
        listItemsVC.listName = lists[indexPath.row].name
        listItemsVC.listItems = lists[indexPath.row].items
        print(listItemsVC.listItems)
        navigationController?.pushViewController(listItemsVC, animated: true)
    }

}


///////////////////////////////////////////////////////////////////////////

SECOND UITABLEVIEW - NOT UPDATING

///////////////////////////////////////////////////////////////////////////

import UIKit

class ListItemsViewController: UITableViewController {

    //Initialize Variables Here
    let cellId = "cellId1"

    var listName: String?
    var listItems: [String?] = []


    override func viewDidLoad() {
        super.viewDidLoad()

        //Things that both cases have in common:
        navigationController?.navigationBar.prefersLargeTitles = true

        //Set Title
        navigationItem.title = listName

        //Register TableView with Id: cellId
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)

        //Delegate method not called on push to view

    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listItems.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

        cell.textLabel?.text = listItems[indexPath.row] //Then sets cell of indexPath text = lists value of index path

        return cell
    }

}

通过将字符串的一部分分离并将其转换为int来将其识别为排序顺序。

答案 1 :(得分:2)

一些替代方案:

(1)按位置切片:

sorted(names, key=lambda x: int(x[5:-6]))

(2)剥离子串:

sorted(names, key=lambda x: int(x.replace('Test-', '').replace('.model', '')))

(3)分裂字符(也可以通过str.partition):

sorted(names, key=lambda x: int(x.split('-')[1].split('.')[0]))

(4)在(1) - (3)中的任何一个上使用np.argsort映射:

list(map(names.__getitem__, np.argsort([int(x[5:-6]) for x in names])))

答案 2 :(得分:1)

这是一种基于正则表达式的方法。我们可以从字符串中提取测试编号,转换为int,然后按此排序。

index="my-index",
            size=100,
            body=
            {

                "query":
                    {
                        "multi_match":
                            {
                                "fields": ["main_name", "main_id", "sub_name", "sub_id",
                                           "entity_name", "entity_id", "item_name", "item_id"],
                                "query": my_original_query,
                                "type": "best_fields",
                                "fuzziness": "1",
                                "tie_breaker": 0.3
                            }
                    },

                "highlight":
                    {

                        "type":"unified",
                        "order": "score",
                        "fields":
                            {
                                "main_name": {},
                                "main_id": {},
                                "sub_name": {},
                                "sub_id": {},
                                "entity_name": {},
                                "entity_id": {},
                                "item_name": {},
                                "item_id": {}

                            }

                    },

            }

答案 3 :(得分:1)

您可以将re.findall与sort函数的key一起使用:

import re
names = [
 'Test-1.model',
 'Test-4.model',
 'Test-6.model',
 'Test-8.model',
 'Test-10.model',
 'Test-20.model'
]
final_data = sorted(names, key=lambda x:int(re.findall('(?<=Test-)\d+', x)[0]), reverse=True)

输出:

['Test-20.model', 'Test-10.model', 'Test-8.model', 'Test-6.model', 'Test-4.model', 'Test-1.model']

答案 4 :(得分:1)

您可以使用key参数和sorted()来完成此操作,假设每个字符串的格式都相同:

def get_number_sorted(somelist):
    return sorted(somelist, key=lambda x: int(x.split('.')[0].split('-')[1]))

看起来您可能希望列表反向排序(?),在这种情况下您可以添加reverse=True

def get_number_sorted(somelist):
    return sorted(somelist, key=lambda x: int(x.split('.')[0].split('-')[1]), reverse=True)
number_sorted = get_number_sorted(names)
print(number_sorted)
['Test-20.model', 'Test-10.model', 'Test-8.model', 'Test-6.model', 'Test-4.model', 'Test-1.model']

参见相关内容:Key Functions

答案 5 :(得分:0)

我自己找到了类似的问题和解决方案。 Nonalphanumeric list order from os.listdir() in Python

import re
def sorted_alphanumeric(data):
    convert = lambda text: int(text) if text.isdigit() else text.lower()
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] 
    return sorted(data, key=alphanum_key, reverse=True)

答案 6 :(得分:0)

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""

然后执行类似

的操作
 sorted(names, key=lambda x: int(find_between(x, 'Test-', '.model')))