【问题标题】:How to list alphabetically in this way?如何以这种方式按字母顺序列出?
【发布时间】:2016-08-07 16:08:18
【问题描述】:

我必须在不使用列表排序方法的情况下对用户输入的名称进行排序。这是我到目前为止所拥有的,但在定义“一”、“二”和“三”时遇到了问题。我需要程序检查每个字母以确保它是真正按字母顺序排列的。谁能帮忙?

name1=str(input("Enter name #1: "))
name2=str(input("Enter name #2: "))
name3=str(input("Enter name #3: "))

one = name1[0].upper() + name1[1].upper() + name1[2].upper()
two = name2[0].upper() + name2[1].upper() + name2[2].upper()
three = name3[0].upper() + name3[1].upper() + name3[2].upper()

if one < two and two < three:
     print("These names in alphabetical order are: ", name1, name2, name3)
elif one < two and three < two:
     print("These names in alphabetical order are: ", name1, name3, name2)     
elif two < three and three < one:
     print("These names in alphabetical order are: ", name2, name3, name1)
elif two < one and one < three:
     print("These names in alphabetical order are: ", name2, name1, name3)
elif three < two and two < one:
     print("These names in alphabetical order are: ", name3, name2, name1)
else:
     print("These names in alphabetical order are: ", name3, name1, name2)

提前致谢! edit 我的问题在于定义“一”、“二”和“三”,它需要遍历输入中的所有字母。现在它贯穿前三个字母,但如果我添加下一个字母并且只给出三个字母的名称,则会出错。如果我使用 len 函数,它会告诉我它是一个整数

【问题讨论】:

  • 这闻起来像家庭作业。为什么不允许使用“列表排序”功能?你试过什么了?你遇到了什么问题。
  • 有一个isalpha 方法可以测试一个字符串是否真的是按字母顺序排列的
  • onetwothree 是干什么用的?为什么只比较前三个字母?此外,你可以做one = name1[:3].upper() 或(可能更像你需要的)只是one = name1.upper()。而不是one &lt; two and two &lt; three,您可以使用one &lt; two &lt; three还有,从one &lt; two and three &lt; two,你怎么推断name1 &lt; name3
  • @tobias_k 看起来应该是名字,中间名,姓氏,也许
  • @cricket_007 可能,但在所有三个名称上都缺少split()

标签: python alphabetical names


【解决方案1】:

我需要程序检查每个字母以确保它是真正按字母顺序排列的。

这就是字符串比较的作用。您的代码的问题是您将onetwothree 限制为输入字符串的前三个字母。相反,您应该将整个名称大写并比较它们。

name1 = input("Enter name #1: ")  # no need for str(...)
... # same for name2, name3

one = name1.upper()  # uppercase whole nam, not just first three letters
... # same for two, three

answer = "These names in alphabetical order are: "  # don't repeat this X times
if one < two < three:  # comparison chaining
    print(answer, name1, name2, name3)
elif one < three < two:
    print(answer, name1, name3, name2)
elif ...:
    # a whole bunch more
else:
    print(answer, name3, name2, name1)

【讨论】:

  • 这很完美。谢谢你。我想我太卡在第一个字母上。
【解决方案2】:

您可以使用良好的旧排序算法

letters = [name1.upper(),name2.upper(),name3.upper()] 
for i in range(len(letters)):
   for j in range(i,len(letters)):
             if letters[i] > letters[j]:
                     letters[i],letters[j] = letters[j],letters[i]

【讨论】:

  • 变量“字母”是一个包含大写名称的列表。在 python 中,如果你比较两个单词,它将比较所有内容。例如语句'ABCDE' &lt; 'ABCDF' yields True
  • 抱歉,我被 letters 这个名字弄糊涂了,我没看到你把整个单词都大写了。尽管如此,这仍需要第二遍才能根据大写版本的位置找到实际名称的正确位置。相反,您可以使用 letters = [(name1.upper(), name1), ...] 并使用相同的算法对这些元组进行排序,然后提取原始名称。
  • 是的,确实会保留原始名称。但是我假设一旦名称全部整理好,OP 就能够提取名称并且没有发布完整的解决方案。我应该编辑我的答案还是这样?我是 StackOverflow 社会的新手。
猜你喜欢
  • 1970-01-01
  • 2020-03-29
  • 2019-02-05
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多