English 中文(简体)
我怎么能够同时用两个标准把扼杀名单划在沙尔?
原标题:How can I sort a string list in Python with two criterias at the same time?

鉴于 我在沙尔有一份扼杀清单:

list = ["   banana   ", "Cherry", "apple"]

我想把这一名单说成是不敏感的,忽略了白人空间。 因此:

list = ["apple", "   banana   ", "Cherry"]

如果我使用的话:

sorted(list, key=str.casefold)

我这样做:

list = ["   banana   ", "apple", "Cherry"]

情况不敏感,但空间特性在信函前出现。

如果我使用的话:

sorted(list, key=lambda x:x.replace(   ,   ))

我这样做:

list = ["Cherry", "apple", "   banana   "]

它忽视了空间,但并不敏感。 我试图把这两种解决办法结合起来,但我无法做到这一点。 是否有办法轻易地确定这一结果和“合并”两种结果?

最佳回答

您可使用str.strip(https://docs.python.org/3/library/stdtypes.html#str.case rel=“nofollow noreferer”>str.case for caseless le .

lst = ["   banana   ", "Cherry", "apple"]

res = sorted(lst, key=lambda x: x.strip().casefold())

print(res)

Output:

[ apple ,     banana    ,  Cherry ]
问题回答

电话线

values = ["   banana   ", "Cherry", "apple"]
print(sorted(values, key=lambda x: x.replace(   ,   ).casefold()))
# [ apple ,     banana    ,  Cherry ]

仅到一开始和结束时,我建议str.strip

print(sorted(values, key=lambda x: x.strip().casefold()))

一个老问题,但我要就今后使用PyICU的参考提供另一种答案,并且可以用任何得到支持的当地人来加以利用,使之更具灵活性。

借助于统法协会的合编,有可能以一些白色的方式对串通进行裁量。 Whitespace是可变的串通要素,有可能有选择地改变其处理方式。 有可能改变其串通的权重,以零强而不是初级力量处理。 这将使禁止化学武器组织能够采取必要的行为。

我们将:

  1. Create an PyICU Locale object
  2. Create a collator instance
  3. Use the collator sort keys to sort a list

有可能建立一个国际协调会地方,利用BCP-47(<>U延伸部分)对当地人进行核对。

ka subtag允许我们控制如何处理可变的串通要素。 对于候补轮班,我们使用<代码>-u-ks-seconded。 我们还可以使用<代码>kv的子标签限制改变特性。

所需语文标签是en-u-ks-seconded-kv-space

import icu
fruit = ["   banana   ", "Cherry", "apple"]
locale = icu.Locale.forLanguageTag( en-u-ka-shifted-kv-space )
collator = icu.Collator.createInstance(locale)
sorted_data = sorted(fruit, key=collator.getSortKey)
print(sorted_data)
# [ apple ,     banana    ,  Cherry ]




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签