Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 是否有任何robot framework关键字来对具有特殊字符的字符串列表进行排序?_Sorting_Robotframework - Fatal编程技术网

Sorting 是否有任何robot framework关键字来对具有特殊字符的字符串列表进行排序?

Sorting 是否有任何robot framework关键字来对具有特殊字符的字符串列表进行排序?,sorting,robotframework,Sorting,Robotframework,我有一个字符串列表,其中包含特殊字符,列表如下: 1.Kevin_richard 2.Dan_ronald 3.Daniel_white 4.David_jacob 5.eddie_bacon 为了按升序对列表进行排序,我使用了collections library中的关键字对列表进行排序(字符串按字母顺序排列,数字按数字顺序排列) *** Settings *** *** Test Cases *** TC Title Sort the given list of usernames

我有一个字符串列表,其中包含特殊字符,列表如下:

1.Kevin_richard
2.Dan_ronald
3.Daniel_white
4.David_jacob
5.eddie_bacon
为了按升序对列表进行排序,我使用了collections library中的关键字对列表进行排序(字符串按字母顺序排列,数字按数字顺序排列)

*** Settings ***

*** Test Cases ***
TC Title
  Sort the given list of usernames in ascending order

*** Keywords ***
Sort the given list of usernames in ascending order
  ${sorted_order_asc}=  Copy List  ${default_order_username}    //default order represents list of five user names
  Sort List  ${sorted_order_asc}
执行上述脚本时,列表按以下顺序排序:

  • 丹尼尔·怀特
  • 丹·罗纳德
  • 大卫·雅各布
  • 埃迪•培根
  • 李察
  • 但这不是预期的排序顺序。在上面的列表中,Dan_ronald必须在列表的顶部排序列表忽略特殊字符,因此跳过Dan后加下划线,并检查下一个字母表(r vs i)。因此,丹尼尔·怀特名列榜首


    任何有助于解决此问题的帮助都将不胜感激。

    在下面的示例中,通过使用python函数可以最容易地实现这一点。将创建一个自定义robot关键字,该关键字采用具有正确字符排序顺序的字符串。然后使用它对列表项进行相应的排序

    *** Settings ***
    Library    Collections
    
    *** Variables ***
    @{list}
        ...    Kevin_richard
        ...    Dan_ronald
        ...    Daniel_white
        ...    David_jacob
        ...    eddie_bacon
    
    *** Test Cases ***
    Standard Sorting
        ${sorted_list}    Sort List Custom    ${list}
    
        ${result}    Create List
        ...    Dan_ronald
        ...    Daniel_white
        ...    David_jacob
        ...    Kevin_richard
        ...    eddie_bacon    
    
        Lists Should Be Equal    ${sorted_list}    ${result}   
    
    
    Reverse Sorting
        ${alphabet_reverse}    Set Variable    zyxwvutsrqpomnlkjihgfedcbaZYXWVUTSRQPOMNLKJIHGFEDCBA9876543210_${SPACE}
        ${sorted_list}   Sort List Custom    ${list}    ${alphabet_reverse}
    
        ${result}    Create List
        ...    eddie_bacon
        ...    Kevin_richard
        ...    David_jacob
        ...    Daniel_white
        ...    Dan_ronald
    
    
        Lists Should Be Equal    ${sorted_list}    ${result}        
    
    *** Keywords ***
    Sort List Custom
        [Documentation]
        ...    Sort a list using a custom sort order
        ...    
        ...    This keyword sorts a list according to a custom sort order 
        ...    when given, or the default one when not provided. 
        ...    
        ...    Arguments:
        ...    - input_list    {list}      a list of strings to be sorted.
        ...    - alphabeth     {string}    a string of characters according to which order they
        ...                                must be sorted.
        ...    
        ...    Returns:        {list}      the sorted list.
        ...    
        [Arguments]    ${input_list}    ${alphabet}=${SPACE}_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
    
        ${ns}    Create Dictionary    alphabet=${alphabet}    input_list=${input_list}
    
        ${sorted}    Evaluate     sorted(input_list, key=lambda word: [alphabet.index(c) for c in word])     namespace=${ns} 
        [Return]    ${sorted}
    
    排序列表忽略特殊字符,因此在跳过Dan后加下划线,并检查下一个字母表(r vs i)

    并非如此-
    排序列表
    不会以任何方式修改成员,也不会跳过任何承租人。当它运行时,它将
    'i'
    '
    进行比较,并(不知何故)确定'i'的字符代码小于
    '

    实际上,
    Sort List
    只是python的
    Sort()
    方法的包装器

    我已经在repl中运行了您的源代码列表,它按照预期的方式排序——“Daniel”之前的“Dan”:

    >>> a = ["Kevin_richard", "Dan_ronald", "Daniel_white", "David_jacob", "eddie_bacon"]
    >>> a
    ['Kevin_richard', 'Dan_ronald', 'Daniel_white', 'David_jacob', 'eddie_bacon']
    >>> a.sort()
    >>> a
    ['Dan_ronald', 'Daniel_white', 'David_jacob', 'Kevin_richard', 'eddie_bacon']
    
    还有其他一些问题——在正常排序中,“Kevin”应该在“eddie”之前(大写拉丁字母在小写字母之前带有代码),但这不是问题正文中的输出。事实上,您的输出看起来不区分大小写,这是关键字不能确定的

    这让我想到还有更多的原因:

    • 您确定粘贴的示例列表与代码中使用的示例列表完全相同吗?这可能是因为Unicode特许权类似于拉丁语特许权(比如西里尔语,其中许多特许权在视觉上存在冲突),而这些特许权并没有按原样粘贴在问题中
    • 源值周围是否有空格字符?如果是,这将影响排序(并且可以解释“eddie”的情况)-如果您只想比较名称,则在进行排序之前将其剥离
    • 代码示例是否为获得此结果所做的一切
    • 最后-我删除了值前面的数字-它们会对排序产生很大影响,但必须问-它们不在源值中,对吗

    一个安全的方法是在
    alphabet
    参数的默认值中包含空格(
    ${Empty}
    )和数字-有人肯定会按原样复制代码,当
    index()
    为列表中不存在的常见字符抛出值错误时,会感到惊讶:)向上投票,尽管老年退休金问题很可能不会直接解决,但答案直接针对问题的标题。同意。更新了示例以反映使用空格和数字的排序。我的问题是,它不应该是
    ${EMPTY}
    ,而应该是
    ${space}
    (正在考虑键入其他内容,抱歉:)