jrro452
发表于 2018-5-8 13:33:23
String='easqWAwaeq'
String=sorted(String)
Newlist = String[:]
for each in String:
#each如果是大写,就插入到它小写的前面
if each.isupper():
Newlist.insert(Newlist.index(each.lower()),each)
Newlist.remove(each)
print(Newlist )
jrro452
发表于 2018-5-8 16:04:23
jrro452 发表于 2018-5-8 13:33
没考虑只有大写的情况。更正下。
String="easqWAwaeqshfohoahoFHAOJFAOJFAJhaofhaofoajFAOUFOAUQ"
upper = []
for each_ca in String:
if each_ca.lower() not in String:
upper.append(each_ca)
String = String.replace(each_ca,each_ca.lower())
String=sorted(String)
Newlist = String[:]
for each in String:
#each如果是大写,就插入到它小写的前面
if each.isupper():
Newlist.insert(Newlist.index(each.lower()),each)
Newlist.remove(each)
Result = ''.join(Newlist)
for each_nu in Result :
if each_nu.upper() in upper :
Result= Result .replace(each_nu,each_nu.upper())
print(Result)
结果:
AAAAAAaaaaaaeeFFFFFfffHhhhhhJJJjOOOOooooooQqqssUUWw
小庆子
发表于 2018-5-16 21:53:17
def array(content):
List = []
for i in content:
if i.isupper():
List.append()
else:
List.append()
List.sort()
for x in List:
print(x,end='')
萧丹夜
发表于 2018-5-22 16:38:43
## 提取字符串
x = input('请输入字符串:')
a = []
c = []
d = []
for each in x:
a.append(ord(each))
## 排序 A:65 a:97
b = sorted(a)
for each in b:
if 64 < each < 97:
c.append(each)## 大写字母
else:
d.append(each)## 小写字母
for i in c:
for j in d:
if (i+32) <= j :
k = d.index(j)
d.insert(k,i)
break
for each in d:
print(chr(each),end = '')
the0503
发表于 2018-5-24 10:38:47
借花献佛:
参考了前面大牛的答案,主要想那就学习一下,字典用法,还有就是sorted排序方法
sortbook = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
temp = input('输入一个字符串:')
print(temp)
print(''.join(sorted(temp,key = sortbook.index)))
可乐必须加冰
发表于 2018-8-2 21:05:27
def paixu():
str1 = 'easqWAwaeq'
list1 = list(str1)
for j in range(len(list1) - 1):
for i in range(len(list1) - 1):
a = ord(list1)
b = ord(list1)
if a >= 65 and a <= 90:
a += 31.5
if b >= 65 and b <= 90:
b += 31.5
if a > b:
list1, list1 = list1, list1
print(list1)
Akihisa
发表于 2018-8-17 10:00:17
str1 = 'easqWAwaeq'
list1 = []
def str_sort(a):
for each in a:
each = ord(each)
if each < 97:
each += 31.5
list1.append(each)
list1.sort()
for each in list1:
if isinstance(each,int) == True:
print('%c' % each,end='')
else:
print('%c' % int(each - 31.5),end = '')
str_sort(str1)
子沙
发表于 2018-8-18 22:05:35
def order(m):
letter=['A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i','J','j','K','k','L','l','M','m','N','n','O','o','P','p','Q','q','R','r','S','s','T','t','U','u','V','v','W','w','X','x','Y','y','Z','z']
list_m=list(m)[:]
for i in range(len(list_m)):
list_m=letter.index(list_m)
list_m.sort()
n=''
for i in list_m:
n+=letter
print(n)
order('jihyftftESES')
quan-renda
发表于 2018-8-31 18:43:56
string=input("请输入字符串:")
lst=list(string)
lst.sort()
for i in range(len(lst)):
if lst.islower():
break
for j in range(0,i):
for k in range(i,len(lst)):
if lst.lower()==lst:
lst.insert(k-1,lst.pop(j))
i-=1
for n in range(len(lst)-1,0,-1):
print(n)
if lst.isupper() and lst.lower()==lst:
lst.insert(n-1,lst.pop(n))
print(''.join(lst))
---------------------------------------------------
前面的几位大佬们太厉害了,我的方法比较笨
史莱克
发表于 2018-10-31 17:01:48
哨子1122 发表于 2017-6-8 21:19
写了一个比较死板的,强制实现
有bug,如果大写字符不在字符串中则无法排序
北城以北灬
发表于 2019-3-7 13:47:04
str1 = 'easqWAwaeq'
str2 = sorted(str1, key=lambda x: ord(x.lower()) * 2 + x.islower())
str2 = "".join(str2)
print(str2)
yu123py
发表于 2019-4-30 20:53:42
我要鱼币!
def prac(s):
s1 = 'abcdefghijklmnopqrstuvwxyz'
s2 = s1.upper()
ss = ''
for i in range(26):
ss += s2
ss += s1
l = list(s)
l.sort(key = ss.index)
return ''.join(l)
if __name__ == '__main__':
print(prac('easqWAwaeq'))
panheng
发表于 2019-8-20 09:37:56
本帖最后由 panheng 于 2019-8-20 09:39 编辑
str1 = input("请输入仅包含大小写字母的字符串:") #获取字符串
newstr = '' #输出结果变量预定义
strlist = list(str1)
ordlist = sorted() #转化为ASCII码排序,大写字母+31.5占位
chrlist = #ASCII码还原为字符,非整数则减去31.5
for i in chrlist: #字符列表重组为新字符串
newstr = newstr+i
print("您输入的字符串为:%s" % str1)
print("调整后的字符串为:%s" % newstr)
Jung
发表于 2019-11-29 16:59:49
def fun25():
L1 = []
S= ''
String1 = str(input("please enter a string:"))
lists = list(String1)
lists.sort()
for string in lists:
if string.isupper():
L1.append(string)
elif string.upper() in L1:
L1.insert(L1.index(string.upper())+1,string)
else:
L1.append(string)
for S1 in L1:
S = S + S1
return S
华一仙
发表于 2020-2-24 09:08:28
def f25(strings):
d = {}
for c, n in zip(range(65, 91), range(0, 52, 2)):
d = n
d = n+1
return ''.join(sorted(strings, key=lambda x: d))
ruokang
发表于 2020-2-24 13:21:24
def fun25(str1):
list1 = list(str1)
list2 = []
for each1 in list1:
if each1.islower():
if list2 == [] or list2[-1].lower() <= each1:
list2.append(each1)
else:
for each2 in list2:
if each2.lower() > each1:
list2.insert(list2.index(each2), each1)
break
if each1.isupper():
if list2 == [] or list2[-1].upper() < each1:
list2.append(each1)
else:
for each2 in list2:
if each2.upper() >= each1:
list2.insert(list2.index(each2), each1)
break
str2 = ''
for each in list2:
str2 = str2[:] + each
return str2
str1 = 'easqWAwaeq'
print(fun25(str1))
xuanyuan0116
发表于 2020-2-25 16:11:49
print('----------每日练习题24----------')
#一个字符串仅包含大小写字母,现在要将其排序成一个新的字符串,次序为从a到z,且相同的大写和小写字母排在一起,大写在小写前。
#如原字符串为'easqWAwaeq',排序后为'AaaeeqqsWw'
string = str(input('请输入一段字符串(大小写字母):'))
list1 = list(string)
list1.sort()
#print(list1)
list1.sort(key=str.lower)
#print(list1)
list2 = ''.join(list1)
print(list2)
嘿嘿嘿的黑子哥
发表于 2020-2-28 11:40:47
s = input("请输入一段字母:")
list1 = sorted(sorted(s),key = str.upper)
new_s = "".join(list1)
print(new_s)
def(s):
list1 = sorted(sorted(s),key = str.upper)
new_s = "".join(list1)
print(new_s)
海上飞鱼
发表于 2020-3-1 15:07:57
print(''.join(sorted(sorted(s), key = str.lower)))
杜若左
发表于 2020-3-18 11:31:10
我的水平太低了用了一个非常愚蠢的方法来解决问题
def io(a):
if ord(a)>90:
return (ord(a)-32)
else:
return ord(a)
j = (input("请输入一个字符串:"))
r = list(j)
for i in range(len(r)-1):
fort in range(i+1,len(r)):
if (io(r)>io(r)):
k = r
r = r
r = k
print("".join(r))