Python 按字母顺序返回字符串
我遇到了一个问题,一个初级Python类只使用我们到目前为止所学的内容按字母顺序显示3个字符串。我们还没有讨论列表或排序() 这就是我到目前为止所做的:Python 按字母顺序返回字符串,python,Python,我遇到了一个问题,一个初级Python类只使用我们到目前为止所学的内容按字母顺序显示3个字符串。我们还没有讨论列表或排序() 这就是我到目前为止所做的: print("Instructions: Please enter three strings.") string1 = input("Enter the first string: ") string2 = input("Enter the second string: ") str
print("Instructions: Please enter three strings.")
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
string3 = input("Enter the third string: ")
string1_lower = string1.lower()
string2_lower = string2.lower()
string3_lower = string3.lower()
string1_upper = string1.upper()
string2_upper = string2.upper()
string3_upper = string3.upper()
if string1_lower < string2_lower < string3_lower:
print(string1)
print(string2)
print(string3)
elif string1_lower < string3_lower < string2_lower:
print(string1)
print(string3)
print(string2)
elif string2_lower < string1_lower < string3_lower:
print(string2)
print(string1)
print(string3)
elif string2_lower < string3_lower < string1_lower:
print(string2)
print(string3)
print(string1)
elif string3_lower < string1_lower < string2_lower:
print(string3)
print(string1)
print(string2)
else:
print(string3)
print(string2)
print(string1)
print(“说明:请输入三个字符串”)
string1=输入(“输入第一个字符串:”)
string2=输入(“输入第二个字符串:”)
string3=输入(“输入第三个字符串:”)
string1\u lower=string1.lower()
string2_lower=string2.lower()
string3_lower=string3.lower()
string1_upper=string1.upper()
string2_upper=string2.upper()
string3_upper=string3.upper()
如果string1\u lower
这适用于小写字符串,但如何使其适用于以大写字母开头的字符串
e、 g
car-boxcar-boxcar-->car-boxcar-boxcar如果要根据输入的字符串进行排序,很简单:不要使用小写的变体进行比较,而是使用原始字符串 但是,您还有另一个问题,即如果两个(小写)字符串相同,而第三个不同,则它将始终在
else
块(a)中结束
要解决这个问题,如果要根据输入的字符串进行排序,应该使用
,这很简单:不要使用小写变量进行比较,而是使用原始字符串
但是,您还有另一个问题,即如果两个(小写)字符串相同,而第三个不同,则它将始终在else
块(a)中结束
要解决这个问题,你应该使用我知道你是个初学者,但是你做了很多工作,你应该让计算机来做。您应该考虑如何进行操作,以便,例如,不管您要显示多少字符串和排序。这里有一些东西只是为了告诉你,如果你继续学习,你将走向何方
# Print instructions for the user
print("Instructions: Please enter A string (just hit return when done")
# An empty list we'll put our entered strings in
strings = []
while True:
# Have the user enter a string
s = input('>')
# If the string is empty, then we're done accepting strings
if s == '':
break
# Add the entered string to the list
strings.append(s)
# sort the list of strings such that case matters
strings = sorted(strings)
# print the list
print("sorted where case matters...")
for s in strings:
print(s)
# sort the list of strings ignoring case
strings = sorted(strings, key=str.lower)
# print the list
print("sorted ignoring case...")
for s in strings:
print(s)
我知道你是个初学者,但是你做了很多工作,你应该让电脑来做。您应该考虑如何进行操作,以便,例如,不管您要显示多少字符串和排序。这里有一些东西只是为了告诉你,如果你继续学习,你将走向何方
# Print instructions for the user
print("Instructions: Please enter A string (just hit return when done")
# An empty list we'll put our entered strings in
strings = []
while True:
# Have the user enter a string
s = input('>')
# If the string is empty, then we're done accepting strings
if s == '':
break
# Add the entered string to the list
strings.append(s)
# sort the list of strings such that case matters
strings = sorted(strings)
# print the list
print("sorted where case matters...")
for s in strings:
print(s)
# sort the list of strings ignoring case
strings = sorted(strings, key=str.lower)
# print the list
print("sorted ignoring case...")
for s in strings:
print(s)
用你自己的话来说,为什么要在现有代码中将字符串转换为小写和大写?如果要比较小写和大写字符串以对其进行排序。。。那么,为什么不这样做呢?对于给定的字符串,@KarlKnechtel他说它们应该按字母顺序显示,所以Z
不应该出现在a
之前。你应该花时间了解列表,以及如何对其排序好的,所以问题是字符串的显示顺序是正确的,但是它们已经被转换成小写,大小写应该保留在输出中?用你自己的话来说,为什么在现有代码中将字符串转换成小写和大写?如果要比较小写和大写字符串以对其进行排序。。。那么,为什么不这样做呢?对于给定的字符串,@KarlKnechtel他说它们应该按字母顺序显示,所以Z
不应该出现在a
之前。你应该花时间了解列表,以及如何对其排序好的,所以问题是字符串的显示顺序是正确的,但是它们已经被转换成小写,并且大小写应该保留在输出中?“仅使用我们迄今为止所学的内容”似乎是规范中非常明确的一部分:-)有很多方法可以插入原始代码,但我不确定这在这个特定的情况下是否会有所帮助。是的,因此“只是告诉你你要去哪里”. 我想这会让这个问题对其他没有严格要求的人更有用。史蒂夫,这是一个公平的争论。“只使用我们迄今为止学到的东西”似乎是规范中非常明确的一部分:-)有很多方法可以导入原始代码,但我不确定这在这种特定情况下是否会有所帮助。是的,因此“只是为了告诉你你的方向”。我想这会让这个问题对其他没有严格要求的人更有用。史蒂夫,这很公平。
strings = []
while (inputStr := input("Enter string (empty string to stop)> ")) != "":
strings.append(inputStr)
print("Sorted, case-insensitive:")
for str in sorted(strings, key = str.lower):
print(f" {str}")
Enter string (empty string means stop)> boxcar
Enter string (empty string means stop)> CaR
Enter string (empty string means stop)> bOxCaR
Enter string (empty string means stop)> car
Enter string (empty string means stop)> CAR
Enter string (empty string means stop)> BOXCAR
Enter string (empty string means stop)>
Sorted, case-insensitive:
boxcar
bOxCaR
BOXCAR
CaR
car
CAR
# Print instructions for the user
print("Instructions: Please enter A string (just hit return when done")
# An empty list we'll put our entered strings in
strings = []
while True:
# Have the user enter a string
s = input('>')
# If the string is empty, then we're done accepting strings
if s == '':
break
# Add the entered string to the list
strings.append(s)
# sort the list of strings such that case matters
strings = sorted(strings)
# print the list
print("sorted where case matters...")
for s in strings:
print(s)
# sort the list of strings ignoring case
strings = sorted(strings, key=str.lower)
# print the list
print("sorted ignoring case...")
for s in strings:
print(s)