简体   繁体   English

最后打印第一个变量(index = 0)

[英]Printing first variable (index=0) at the end

I'm trying to attach the first element of comma separated strings at the end. 我试图在末尾附加逗号分隔字符串的第一个元素。 If user inputs 'first, second, third' then it should return ['first', 'second', 'third', 'first'] 如果用户输入“第一,第二,第三”,那么它应该返回['first','second','third','first']

So I'm really close to completing it but my code returns ['first', 'second', 'third] first 所以我真的很接近完成它,但我的代码首先返回['first','second','third]

I was thinking of using .append() first and then print the code. 我想先使用.append()然后打印代码。 Can somebody elaborate on .append()? 有人可以详细说明.append()吗?

s = input('Please enter a series of comma-separated strings: ')
l = s.split(",")
print(l,l[0])

the append() function allows a user to add a value to the end of a list. append()函数允许用户将值添加到列表的末尾。 So if your list is ["hi", 1, "hello"] and we do a my_list.append(100) the new list will be come ["hi", 1, "hello", 100] 因此,如果您的列表是["hi", 1, "hello"] ,我们执行my_list.append(100) ,新列表将会出现["hi", 1, "hello", 100]

for your case all you need to do is read the values into the list as you are doing split it and then 对于您的情况,您需要做的就是将值读取到列表中,然后进行拆分

s = input('Please enter a series of comma-separated strings: ')
l = s.split(",")
l.append(l[0])
print(l)

Your list will be in the proper format now. 您的列表现在将采用正确的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM