559: 字符串排序(python)

简介: 559: 字符串排序(python)

收藏

难度:一般

标签:暂无标签

题目描述

在对字符串的排序中,往往具有不同的规则来判断字符串的大小先后。这里有两种比较常见的规则: 1. 首先按字符串长度进行排序,对长度相同的字符串,按字母顺序进行排序。如:ab, gdh, c, gaa, caa排序后的结果是:c, ab, caa, gaa, gdh 2. 直接按字母顺序排序,把长度不同的串都按相同长度处理。如:ab, gdh, c, gaa, caa排序后的结果是:ab, c, caa, gaa, gdh 你的任务是写一个程序,对于输入的字符串,按照这两种方式分别进行排序。

输入

只有一组测试数据,测试数据包括多行:第一行是字符串个数N,接下来的N行为字符串,字符串均为大小写字母,不需判错。见sample input

输出

输出为两行,按要求输出字符串排序后的结果,第一行为上面第一种方法,第二行为第二种方法。每两个字符串中以一个空格间隔开。见sample output

样例输入复制

5

ab

gdh

c

gaa

caa

样例输出复制

c ab caa gaa gdh

ab c caa gaa gdh

def f(a,b):
    if len(a)>len(b):
        return a
    else:
        return b
nums=[]
n=int(input())
for _ in range(n):
    nums.append(input())
nums.sort()
num2=sorted(nums)
# print(*nums)
for i in range(0,len(nums)):
    for j in range(0,len(nums)-i-1):
        if f(nums[j],nums[j+1])==nums[j]:
            nums[j],nums[j+1]=nums[j+1],nums[j]
print(*nums)
print(*num2)
相关文章
|
2月前
|
Java 数据处理 索引
(Pandas)Python做数据处理必选框架之一!(二):附带案例分析;刨析DataFrame结构和其属性;学会访问具体元素;判断元素是否存在;元素求和、求标准值、方差、去重、删除、排序...
DataFrame结构 每一列都属于Series类型,不同列之间数据类型可以不一样,但同一列的值类型必须一致。 DataFrame拥有一个总的 idx记录列,该列记录了每一行的索引 在DataFrame中,若列之间的元素个数不匹配,且使用Series填充时,在DataFrame里空值会显示为NaN;当列之间元素个数不匹配,并且不使用Series填充,会报错。在指定了index 属性显示情况下,会按照index的位置进行排序,默认是 [0,1,2,3,...] 从0索引开始正序排序行。
269 0
|
3月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
338 100
|
3月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
454 99
|
3月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
3月前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
3月前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
3月前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
4月前
|
索引 Python
python 字符串的所有基础知识
python 字符串的所有基础知识
351 0
|
4月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
297 92

推荐镜像

更多