不过,我试图在Python中将元素扩展到列表,而不是扩展索引“I”中的字符串,而是扩展索引“I”中字符串的每个字符。
例如,我有一个名为“strings”的列表,其中只有一个字符串“string1”和一个名为“final_list”的空列表。
我想将“strings”的第一个元素扩展到“final_list”,所以我要final_list.extend(strings[0])
。但是,与插入的字符串对应的“final_list”的长度不是1,而是7。
如果有帮助,这是我的代码:
con = connect()
i = 0
new_files = []
while i < len(files):
info_file = obter_info(con, files[i])
if info_file [5] == 0: #file not processed
new_files.append(files[i])
i += 1
有人知道我该怎么做吗?
extend
方法将iterable作为参数,解压该iterable并将每个元素分别添加到调用它的列表中。在你的例子中,你用一个字符串“扩展”一个列表。字符串是iterable。因此,字符串是“unpacted”,每个字符都是单独添加的:如果只想将列表中的一个元素添加到另一个列表中,则使用
append
。否则,将字符串环绕在列表中并重复扩展:试试那个:
或:
相关问题 更多 >
编程相关推荐