你想在Python中根据特定条件对随机单词进行排序。

huangapple go评论68阅读模式
英文:

How can I sort a random word in Python based on specific criteria?

问题

怎么按不同的排序逻辑(标准)对一个随机单词进行排序?

一本书上的练习要求我编写一个程序,根据特定标准对随机单词的字母进行排序。

  1. 首先,按照字母表顺序排列所有小写字母,同样地,排列大写字母。
  2. 然后,对数字进行排序,奇数和偶数分别按升序排列。
  3. 最后,创建一个新变量来存储按照上述所有标准排序后的新单词。

这本书给了我一个例子。

给定单词"OrDenar1234",新单词应该是"aenrrDO1324"。

需要注意的是,我还没学到Python书中的函数部分。所以我正在尝试用我现在已经学到的知识(变量、条件语句、循环、列表等)来完成这个任务。

附:单词是通过输入要求用户提供的。

英文:

How to sort a random word by different ordering logics (criteria)?

An exercise from a book asks me to make a program that sorts the letters of a random word given certain criteria.

  1. First, all the lowercase letters in ascending order, same but with uppercase letters.
  2. Then with the digits (numbers I suppose) sort them by odd and even in ascending order as well.
  3. Finally create a new variable to store the new word you created sorted by all the criteria above.

The book gives me and example.

> Given the word "OrDenar1234" the new world should be "aenrrDO1324"

Something to note is that I still didn't get to the functions section of the python book. So I'm trying to do it with the knowledge I have right now (variables, conditionals, loops, lists, etc.).

PD: the world is asked to the user using input.

答案1

得分: 0

Sure, here's the translated code:

# 以用户输入的方式获取单词
word = input("输入一个单词:")

# 分离出小写字母、大写字母和数字
小写字母 = [char for char in word if char.islower()]
大写字母 = [char for char in word if char.isupper()]
数字 = [char for char in word if char.isdigit()]

# 对小写字母和大写字母进行排序
小写字母.sort()
大写字母.sort()

# 分离并排序奇数和偶数数字
奇数数字 = [digit for digit in 数字 if int(digit) % 2 != 0]
偶数数字 = [digit for digit in 数字 if int(digit) % 2 == 0]
奇数数字.sort()
偶数数字.sort()

# 将它们组合在一起形成最终的字符串
最终单词 = "".join(小写字母 + 大写字母 + 奇数数字 + 偶数数字)

print(最终单词)

This code performs the following steps in Chinese:

  1. 请求用户输入一个单词。
  2. 将该单词分解为小写字母、大写字母和数字。
  3. 对每个组进行升序排序。
  4. 将排序后的组合成最终的单词。
  5. 最后,打印最终结果!
英文:
# Take the word as input
word = input("Enter a word: ")

# Separate out the lowercase, uppercase, and digits
lowercase_letters = [char for char in word if char.islower()]
uppercase_letters = [char for char in word if char.isupper()]
digits = [char for char in word if char.isdigit()]

# Sort the lowercase and uppercase letters
lowercase_letters.sort()
uppercase_letters.sort()

# Separate and sort the odd and even digits
odd_digits = [digit for digit in digits if int(digit) % 2 != 0]
even_digits = [digit for digit in digits if int(digit) % 2 == 0]
odd_digits.sort()
even_digits.sort()

# Combine all of them to form the final string
final_word = "".join(lowercase_letters + uppercase_letters + odd_digits + even_digits)

print(final_word)
  1. Asking the user to input for a word.
  2. Break that word down into lowercase letters, uppercase letters, and digits.
  3. Sort each group in ascending order.
  4. Combine the sorted groups to form the final word.
  5. Finally, Print the final result!!

答案2

得分: 0

你可以使用内置的字符串方法:str.islower()str.isupper()str.isdigit()(我没有使用 str.isdigit() 方法,因为我使用了 else 子句)。

s = 'OrDenar1234'
middle = ''
end = ''
start = ''
for i in sorted(s):
    if i.islower():
        start += i
    elif i.isupper():
        middle += i
    else:
        end += i
print(start + middle + end)

输出:

aenrrDO1234
英文:

You can utilize the built-in string methods: str.islower(), str.isupper(), and str.isdigit() (I didn't use the str.isdigit() method because I use an else clause instead.

s = 'OrDenar1234'
middle = ''
end = ''
start = ''
for i in sorted(s):
    if i.islower():
        start += i
    elif i.isupper():
        middle += i
    else:
        end += i
print(start + middle + end)

Output:

aenrrDO1234

答案3

得分: 0

以下是翻译好的代码部分:

s = "Test1231DEche"
lc = []  # lc=[] list for sort() function
uc = ""
edigits = ""
odigits = ""
for i in s:  # iterate through string
    if i.islower():  # check lower case
        lc.append(i)
    elif i.isdigit():
        if int(i) % 2 == 0:
            edigits += i  # check even digits
        else:
            odigits += i
    elif i.isupper():  # check uppercase
        uc += i
lc.sort()  # sorted since string, for sort() you need to make above to list
uc = sorted(uc)  # uc after sort is a list you can see by printing them
edigits.sort()  # edigits.sort() for list
odigits.sort()
print(uc)
fin = ' '.join(lc + uc + edigits + odigits)  # join above list to string
print(fin)

请注意,我已经对代码进行了适当的翻译,使其在中文环境下易于理解。

英文:

The above answer is a better way of writing this program. But while starting out this was more easier to understand for me. So thought I should put it here. sort

s = "Test1231DEche"
lc="" # lc=[] list for sort() function
uc=""
edigits=""
odigits=""
for i in s: #iterate through string
    if(i.islower()):#check lower case
        lc+=i
    elif(i.isdigit()):
        if(int(i)%2==0):
            edigits+=i #check even digits
        else:
            odigits+=i
    elif(i.isupper()): #check uppercase
        uc+=i
lc=sorted(lc) #sorted since string, for sort() you need to make above to list
uc=sorted(uc) #uc after sort is a list you can see by printing them
edigits=sorted(edigits) #edigits.sort() for list
odigits=sorted(odigits)
print(uc)
fin = ' '.join(lc+uc+edigits+odigits) #join above list to string
print(fin)

huangapple
  • 本文由 发表于 2023年5月21日 07:50:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定