python字符串按首字母排序,Python按字母順序排序字符串,首先是小寫

本文介绍了如何使用Python对包含大小写字母的字符串数组进行排序,使得小写字母优先于大写字母出现。通过自定义排序键、交换字母大小写、使用`string.ascii_letters`索引和创建字典等方法实现。这些方法适用于单个字符或单词的排序,对于字母数字字符串,还可以根据特定规则排序,如小写字母、大写字母和数字的优先级。

I want to sort a given array of strings alphabetically using python, but lowercase words should appear first.

我想使用python按字母順序對給定的字符串數組進行排序,但首先應出現小寫字。

An example:

一個例子:

#!/usr/local/bin/python2.7

arr=['A','e','a','D','f','B']

arr.sort()

for s in arr: print s

Input:

輸入:

A

e

a

D

f

B

Output (current):

輸出(當前):

A

B

D

a

e

f

Output (should be):

輸出(應該):

a

e

f

A

B

D

5 个解决方案

#1

6

Use a custom key method which checks whether the item is not .lower() and then compares the items itself. For 'A', 'D' and 'B' not x.islower() will return True and for other it is False, as True > False smaller case items will come first:

使用自定義鍵方法檢查項目是否不是.lower(),然后比較項目本身。對於'A','D'和'B'而不是x.islower()將返回True而對於其他它將為False,因為True> False較小的案例項將首先出現:

>>> arr = ['A','e','a','D','f','B']

>>> arr.sort(key=lambda x:(not x.islower(), x))

>>> arr

['a', 'e', 'f', 'A', 'B', 'D']

#2

7

To sort words, and not simply letters, just swap the case:

要對單詞進行排序,而不僅僅是字母,只需交換案例:

>>> words = ['alpha', 'Alpha', 'aLpha', 'Bravo', 'bRavo']

>>> sorted(words)

['Alpha', 'Bravo', 'aLpha', 'alpha', 'bRavo']

>>> sorted(words, key=str.swapcase)

['alpha', 'aLpha', 'bRavo', 'Alpha', 'Bravo']

#3

3

We can use string.ascii_letters to get index of each letters to sort them.

我們可以使用string.ascii_letters來獲取每個字母的索引以對它們進行排序。

arr = ['A','e','a','D','f','B']

import string

print sorted(arr, key=string.ascii_letters.index)

Results:

結果:

['a', 'e', 'f', 'A', 'B', 'D']

Or if you want to sort the original arr list use sort built-in function.

或者,如果要對原始arr列表進行排序,請使用sort內置函數。

arr.sort(key=string.ascii_letters.index)

print arr

If the arr list is having words instead of single letters or alphabets we can use str.swapcase

如果arr列表中有單詞而不是單個字母或字母,我們可以使用str.swapcase

arr = ['Abc', 'abc', 'aBc']

print sorted(arr, key=str.swapcase)

Yields:

產量:

['abc', 'aBc', 'Abc']

#4

1

Some timings show that for sorting single characters creating a dict is the actually most efficient:

一些時間表明,對於排序單個字符創建一個字典實際上是最有效的:

python2.7:

python2.7:

from string import ascii_letters

d = {b:a for a, b in enumerate(ascii_letters)}

In [34]: timeit sorted(s, key=str.swapcase)

10 loops, best of 3: 32.6 ms per loop

In [35]: timeit sorted(s,key=lambda x: (not x.islower(),x))

10 loops, best of 3: 51.4 ms per loop

In [37]: timeit (sorted(s ,key=d.get))

10 loops, best of 3: 22.4 ms per loop

Python3.4:

Python3.4:

In [4]: timeit sorted(s,key=lambda x: (not x.islower(),x))

10 loops, best of 3: 57.7 ms per loop

In [5]: timeit sorted(s, key=str.swapcase)

10 loops, best of 3: 41.2 ms per loop

In [6]: timeit (sorted(s ,key=d.get))

10 loops, best of 3: 21.1 ms per loop

#5

0

Given : Alphanumeric string

給定:字母數字字符串

Aim: To sort by rules

目標:按規則排序

Small letters first.

首先是小寫字母。

Then capital letters.

然后大寫字母。

Then digits (even first, odd last) (Least priority).

然后是數字(甚至是第一個,最后一個奇數)(最低優先級)。 def func(l):

if l.islower():

return ord(l) - 32

elif l.isupper():

return ord(l) + 32

elif l.isdigit():

if int(l) % 2 == 0:

return ord(l) + 200

else:

return ord(l) + 100

print(*sorted(st, key=func), sep='')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值