1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python字符串按首字母排序 Python按字母順序排序字符串 首先是小寫

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

时间:2022-11-25 01:30:15

相关推荐

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

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='')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。