我正在用Python解决一个简单的代码,以打印随机生成的票证号码。但是,当我打印机票号码列表时,它会给出额外的单引号。
请参考以下代码:
#PF-Exer-22
import random
def generate_ticket(airline,source,destination,no_of_passengers):
ticket_number_list=[]
src = source[0:3]
dest = destination[0:3]
listof_passonger = []
random_number_list = []
random_number = 0
random_number = 102
for loop in range(0, no_of_passengers):
#if(random_number in random_number_list):
#random_number = random.randint(101,500)
#else:
#random_number_list.append(random_number_list)
random_number = random_number + 1
ticket_number = airline + ":" + src + ":" + dest + ":" + str(random_number)
ticket_number_list.append(ticket_number)
return ticket_number_list
print(generate_ticket("AI","Bangalore","London",7))
在执行上述代码时,我的输出是:-
['AI:Ban:Lon:103', 'AI:Ban:Lon:104', 'AI:Ban:Lon:105', 'AI:Ban:Lon:106', 'AI:Ban:Lon:107', 'AI:Ban:Lon:108', 'AI:Ban:Lon:109']
但是我的预期输出是:-
[AI:Ban:Lon:103,AI:Ban:Lon:104,AI:Ban:Lon:105,AI:Ban:Lon:106,AI:Ban:Lon:107]
如您所见,没有预期的单个代码输出。
请参考下图:
答案 0 :(得分:3)
这是在python中处理Regex rRemScript = new Regex(@"<(script|link|style)[^>]*>[\s\S]*?</\1>");
hTMLText = rRemScript.Replace(hTMLText, "");
时的预期行为。
示例
list of strings
输出
# Declaring a list
L = [1, "a" , "string" , 1+2]
print L
L.append(6)
print L
L.pop()
print L
print L[1]
如果您不想在输出中显示[1, 'a', 'string', 3]
[1, 'a', 'string', 3, 6]
[1, 'a', 'string', 3]
a
。您可以单独打印元素,也可以使用'
函数,如下所示。
join()
答案 1 :(得分:0)
您的函数返回一个字符串列表。当您尝试打印此列表时,它表明列表中的各个元素都是字符串。 因此,要获得所需的输出,您需要做的是:
结果代码如下:
def generate_ticket(airline,source,destination,no_of_passengers):
ticket_number_list=[]
src = source[0:3]
dest = destination[0:3]
listof_passonger = []
random_number_list = []
random_number = 0
random_number = 102
for loop in range(0, no_of_passengers):
#if(random_number in random_number_list):
#random_number = random.randint(101,500)
#else:
#random_number_list.append(random_number_list)
random_number = random_number + 1
ticket_number = airline + ":" + src + ":" + dest + ":" + str(random_number)
ticket_number_list.append(ticket_number)
return ticket_number_list
print(generate_ticket("AI","Bangalore","London",7))
l = generate_ticket("AI","Bangalore","London",7)
for item in l:
print(item)