有没有机器人框架关键字可以对具有特殊字符的字符串列表进行排序



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

1.Kevin_richard
2.Dan_ronald
3.Daniel_white
4.David_jacob
5.eddie_bacon

为了按升序对列表进行排序,我使用了集合库中的sort list(字符串按字母顺序排序,数字按数字排序(关键字。

*** 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}

在执行上述脚本时,列表按以下顺序排序:

  1. Daniel_white
  2. Dan_ronald
  3. 大卫_雅各布
  4. eddie_bacon
  5. Kevin_richard

但这不是预期的排序顺序。在上面的列表中,Dan_ronald必须位于列表的顶部排序列表忽略特殊字符,因此跳过Dan后的下划线并检查下一个字母表(r vs i(。因此,Daniel_white位居榜首。

如能为解决这一问题提供任何帮助,我们将不胜感激。

在下面的示例中,使用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(。

事实并非如此-Sort List不会以任何方式修改成员,也不会跳过任何租船人。当它运行时,它比较了'i''_',并(不知何故(决定"i"的字符代码比'_'
实际上,Sort List只是python的sort()方法的包装器。

我已经在回复中运行了你的源列表,它按照预期的方式排序——"Dan_"在"Daniel_"之前:

>>> 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"的情况(——如果你只想比较名字,那么在排序之前先去掉它们
  • 代码示例是你为得到这个结果所做的一切
  • 最后——我已经删除了值前面的数字——它们会对排序产生很大影响,但不得不问——它们不在源值中,对吧

最新更新