删除 while 循环中的尾随逗号 python
Remove trailing comma in while loop python
我写的代码应该找到一个字符串中所有出现的子字符串的索引,将它们打印在同一行上,并在它们之间添加一个逗号。我不被允许使用列表,所以我想到了以下内容:
string = input("Enter your string: ")
substring= input("Enter your substring: ")
print("Substring found at positions: ", end = "")
count = 0
while True:
count = string.find(substring, count+1)
if count == -1:
break
else:
print(str(count) + ",", end = " ")
但是,我需要去掉末尾的最后一个逗号,但如果不使用列表我就不知道怎么做。有什么见解吗?
正如用户 9769953 所解释的那样。您可以通过连接建立一个字符串,然后在没有找到该子字符串的其他出现时删除最后一个逗号:
string = input("Enter your string: ")
substring= input("Enter your substring: ")
print("Substring found at positions: ", end = "")
count = 0
result = ''
while True:
count = string.find(substring, count+1)
if count == -1:
result = result[:-1]
break
else:
result += str(count) + ","
print(result)
示例标准输出:
Enter your string: 123451234512345
Enter your substring: 5
Substring found at positions: 4,9,14
或者,坚持同样的逻辑:
string = input("Enter your string: ")
substring= input("Enter your substring: ")
print("Substring found at positions: ", end = "")
count = 0
last = ""
while True:
if last:
print(last + ", ", end="")
count = string.find(substring, count+1)
if count == -1:
break
else:
last = str(count)
print(last)
这个范式在这里有点冗长,但在构建逐行解析器时非常有用。
完整性
因为 'lists are forbidden' 这是某种练习。从技术上讲,这不使用列表:
posns = ", ".join(str(x) for x in range(len(string)) if string[x:].startswith(substring))
print(f"Substring found at positions: {posns}.")
我从 this question 中获取了那个迭代器。如前所述,它的复杂性是二次方的,但它是最易读的单行代码。实际上我会使用合适的生成器,因为它更容易阅读。不知何故,我认为他们不希望你像这样解决它...
您可以使用 string
并将位置值连接到它,然后使用字符串切片去掉最后一个逗号和 space:
string = input("Enter your string: ")
substring= input("Enter your substring: ")
print("Substring found at positions: ", end = "")
count = -1 #We have to start with -1 and not 0 to check the 1st index of string
pos = "" #To store the position values
while True:
count = string.find(substring, count+1)
if count == -1:
pos = pos[:len(pos)-2] #This strips off the last comma and space while exiting the loop
break
else:
pos += f"{count}, " #This is where we concatenate the position values
print(pos)
只需更改打印以将 ", NUM"
附加到该行而不是 "NUM, "
。
去掉第一个逗号比去掉最后一个逗号更容易,因为如果打印是第一次调用就可以提前知道:
count = 0
comma = False
while True:
count = string.find(substring, count+1)
if count == -1:
break
else:
if comma:
print(", ", end = "")
else:
comma = True
print(str(count), end = "")
顺便说一句,在 break
(或 return
)之后不需要 else
。另外print
不需要str(x)
,直接print(x)
即可。 count
的初始值应该是 -1
从位置 0 开始第一次搜索。不要忘记在循环后打印换行符。
我写的代码应该找到一个字符串中所有出现的子字符串的索引,将它们打印在同一行上,并在它们之间添加一个逗号。我不被允许使用列表,所以我想到了以下内容:
string = input("Enter your string: ")
substring= input("Enter your substring: ")
print("Substring found at positions: ", end = "")
count = 0
while True:
count = string.find(substring, count+1)
if count == -1:
break
else:
print(str(count) + ",", end = " ")
但是,我需要去掉末尾的最后一个逗号,但如果不使用列表我就不知道怎么做。有什么见解吗?
正如用户 9769953 所解释的那样。您可以通过连接建立一个字符串,然后在没有找到该子字符串的其他出现时删除最后一个逗号:
string = input("Enter your string: ")
substring= input("Enter your substring: ")
print("Substring found at positions: ", end = "")
count = 0
result = ''
while True:
count = string.find(substring, count+1)
if count == -1:
result = result[:-1]
break
else:
result += str(count) + ","
print(result)
示例标准输出:
Enter your string: 123451234512345
Enter your substring: 5
Substring found at positions: 4,9,14
或者,坚持同样的逻辑:
string = input("Enter your string: ")
substring= input("Enter your substring: ")
print("Substring found at positions: ", end = "")
count = 0
last = ""
while True:
if last:
print(last + ", ", end="")
count = string.find(substring, count+1)
if count == -1:
break
else:
last = str(count)
print(last)
这个范式在这里有点冗长,但在构建逐行解析器时非常有用。
完整性
因为 'lists are forbidden' 这是某种练习。从技术上讲,这不使用列表:
posns = ", ".join(str(x) for x in range(len(string)) if string[x:].startswith(substring))
print(f"Substring found at positions: {posns}.")
我从 this question 中获取了那个迭代器。如前所述,它的复杂性是二次方的,但它是最易读的单行代码。实际上我会使用合适的生成器,因为它更容易阅读。不知何故,我认为他们不希望你像这样解决它...
您可以使用 string
并将位置值连接到它,然后使用字符串切片去掉最后一个逗号和 space:
string = input("Enter your string: ")
substring= input("Enter your substring: ")
print("Substring found at positions: ", end = "")
count = -1 #We have to start with -1 and not 0 to check the 1st index of string
pos = "" #To store the position values
while True:
count = string.find(substring, count+1)
if count == -1:
pos = pos[:len(pos)-2] #This strips off the last comma and space while exiting the loop
break
else:
pos += f"{count}, " #This is where we concatenate the position values
print(pos)
只需更改打印以将 ", NUM"
附加到该行而不是 "NUM, "
。
去掉第一个逗号比去掉最后一个逗号更容易,因为如果打印是第一次调用就可以提前知道:
count = 0
comma = False
while True:
count = string.find(substring, count+1)
if count == -1:
break
else:
if comma:
print(", ", end = "")
else:
comma = True
print(str(count), end = "")
顺便说一句,在 break
(或 return
)之后不需要 else
。另外print
不需要str(x)
,直接print(x)
即可。 count
的初始值应该是 -1
从位置 0 开始第一次搜索。不要忘记在循环后打印换行符。