String 为什么是两个字符串而不是一个?
以下是我的源代码:String 为什么是两个字符串而不是一个?,string,dictionary,set,String,Dictionary,Set,以下是我的源代码: destinations={"",} destinations.discard("") flightterminals={"Terminal 1":[],"Terminal 2":[], "Terminal 3":[]} proceed="yes" count=0 flightnumber=int(input("How many flights d
destinations={"",}
destinations.discard("")
flightterminals={"Terminal 1":[],"Terminal 2":[], "Terminal 3":[]}
proceed="yes"
count=0
flightnumber=int(input("How many flights do you want to add: "))
for i in range(flightnumber):
count+=1
print("Where is plane {} headed?".format(count))
temp=input("")+"1"
destinations.add(temp[:-1])
while ((temp in flightterminals["Terminal 1"]) or (temp in flightterminals["Terminal 2"]) or (temp in flightterminals["Terminal 3"])):
temp=temp[:-1]+str(int(temp[-1])+1)
print(type(temp))
print(temp)
flightterminals["Terminal {}".format((count-1)%3+1)]+=temp
print(flightterminals["Terminal 1"])
这是终点站:
Flight Terminal Manager 5600
How many flights do you want to add: 1
Where is plane 1 headed?
x
<class 'str'>
x1
['x', '1']
飞行终端管理器5600
您要添加多少航班:1
1号飞机飞往哪里?
x
x1
['x','1']
看起来倒数第三行,
flightterminals[“Terminal{}”.format((count-1)%3+1)]+=temp
,在我的字典中向一个集合添加了两个字符串,尽管temp
显示为前一行中只有一个字符串。我做错了什么?词汇表中的元素是列表。在您所说的行中,您使用的是+=
运算符。因此,temp
被视为一个列表,其元素被推到字典中相应列表的末尾。您必须使用append()
:flightterminals[“Terminal{}.format((count-1)%3+1)]。append(temp)
为了澄清这一点,我说的是为什么它打印两个字符串而不是一个字符串。如果我输入x
,它应该打印x1
,而不是1,x
。这就是我解释的<代码>值包含[x,1]
。因此,print(“,”.join(排序(值))
按预期输出1,x
,而不是x1
。我编辑了问题以澄清问题。请再看一遍,谢谢!哦,明白了。词汇表中的元素是列表。在您所说的行中,您使用的是+=
运算符。因此,temp
被视为一个列表,其元素被推到字典中相应列表的末尾。您必须使用append()
:flightterminals[“Terminal{}.format((count-1)%3+1)]。append(temp)
谢谢。顺便说一句,如果你改变你的答案,我会让它成为公认的答案。