[英]can't understand how assigning characters in string to list through list slicing works in following piece of code
I can't understand how string is assigned to the list我无法理解如何将字符串分配给列表
def convert(string):
list1 = []
list1[:0] = string
return list1
# Driver code
str1 = "ABCD"
print(convert(str1))
As it looks its a way of converting a string to a list, you can do it like因为它看起来是一种将字符串转换为列表的方式,所以您可以像
list(string)
but this is also quite interesting as , it takes whatever is the input the append it in the start of the list, from python docs list insertion但这也很有趣,因为它接受任何输入并将其附加到列表的开头,来自 python docs 列表插入
list1[0:0] inserts the iterable to before the first element of the list, if you slice the list to go up to zero elemen it gives you an empty list list1[0:0] 在列表的第一个元素之前插入可迭代对象,如果您将列表切片以达到零元素,它将为您提供一个空列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.