所以我目前所拥有的是一个看起来像这样的字符串,
hello here, hello there, hello Everywhere
如果有人知道那是什么,我正在进行kwic的迭代。所需的格式是列表的元组列表,而不区分大小写不敏感。所以最后我有一个看似
的未排序列表(['here,', 'hello', 'there,', 'hello', 'Everywhere', 'hello'], 0)
(['hello', 'there,', 'hello', 'Everywhere', 'hello', 'here,'], 0)
(['there,', 'hello', 'Everywhere', 'hello', 'here,', 'hello'], 0)
(['hello', 'Everywhere', 'hello', 'here,', 'hello', 'there,'], 0)
(['Everywhere', 'hello', 'here,', 'hello', 'there,', 'hello'], 0)
(['hello', 'here,', 'hello', 'there,', 'hello', 'Everywhere'], 0)`
目前我正在使用像
这样的python类型Final_Array.sort(key = lambda a: a[0][0].lower())
但是这给了我一个看起来像
的排序列表(['Everywhere', 'hello', 'here,', 'hello', 'there,', 'hello'], 0)
(['hello', 'there,', 'hello', 'Everywhere', 'hello', 'here,'], 0)
(['hello', 'Everywhere', 'hello', 'here,', 'hello', 'there,'], 0)
(['hello', 'here,', 'hello', 'there,', 'hello', 'Everywhere'], 0)
(['here,', 'hello', 'there,', 'hello', 'Everywhere', 'hello'], 0)
(['there,', 'hello', 'Everywhere', 'hello', 'here,', 'hello'], 0)`
显然hello Everywhere
应该在hello there
之前,还有hello here
。
它是基于将访问列表的第一个单词发送到较低的排序,但是我需要它来对所访问列表的所有条目进行排序和比较,这样如果存在平局,它就会一直比较数组中的下一个值和下一个值一直无视案例。
答案 0 :(得分:2)
Right now, your sort is only taking into account the first word in the list. In order to make it sort lexicographically based on all the words in the list, your sort key should return a list of lower-cased words (one lower-cased word for each word in the input list)
def sort_key(t):
word_list, integer = t
return [word.lower() for word in word_list]
Final_Array.sort(key=sort_key)
Due to the complexity of the sort, I'd prefer to avoid the lambda in this case, but not everyone necessarily agrees with that opinion :-)
答案 1 :(得分:-1)
Final_Array.sort(key=lambda x: list(map(str.lower, x[0])))