Python 如何从列表中获取元素并创建新列表

Python 如何从列表中获取元素并创建新列表,python,python-3.x,Python,Python 3.x,我的任务是从字符串生成一个列表。然后将所有元素从列表移动到另一个列表,并再次将其转换为字符串。 这就是我所做的: def string_list(sentence: str) -> str: result = [] sentence = sentence.split(" ") while sentence: for i in range(len(sentence)): tmp_v

我的任务是从字符串生成一个列表。然后将所有元素从列表移动到另一个列表,并再次将其转换为字符串。 这就是我所做的:

    def string_list(sentence: str) -> str:
        result = []
        sentence = sentence.split(" ")
        while sentence:
            for i in range(len(sentence)):
                tmp_var = sentence[i]
                result.append(tmp_var)
                sentence.remove(tmp_var)
        return " ".join(result)

print(string_list("Hey, how's it going?"))
但我收到了以下错误消息:索引器错误:列表索引超出范围

提前谢谢你

def string_list(str):
    result = []
    sentence = str.split(" ")
    for i in range(len(sentence)):
        result.append(sentence[i])
    return " ".join(result)

print(string_list("Hey, how's it going?"))
我不明白为什么要将单词从一个列表移动到另一个列表,但在python中,可以使用循环机制,而不是使用索引,这可能会引起麻烦

希望我提供的答案能解决您的问题

我不明白为什么要将单词从一个列表移动到另一个列表,但在python中,可以使用循环机制,而不是使用索引,这可能会引起麻烦


希望我提供的答案能解决您的问题。

使用
list.pop
清空输入数据

def string_list(sentence: str) -> str:
    result = []
    sentence = sentence.split(" ")
    if sentence:
        word = sentence.pop()
        result.append(word)
        while word:
            if not sentence:
                break
            word = sentence.pop()
            result.append(word)

    return ' '.join(result)

使用
list.pop
清空输入数据

def string_list(sentence: str) -> str:
    result = []
    sentence = sentence.split(" ")
    if sentence:
        word = sentence.pop()
        result.append(word)
        while word:
            if not sentence:
                break
            word = sentence.pop()
            result.append(word)

    return ' '.join(result)

如果需要,您可以尝试列表理解:

string_1="Hey, how's it going?"



origional_list=[i for i in string_1]
string_to_list=[i for i in origional_list if i!=',' and i!="'" and i!=' ']

print("String to list : {}".format(string_to_list))
list_to_string=("".join(origional_list))


print("list to String : {}".format(list_to_string))
输出:

String to list : ['H', 'e', 'y', 'h', 'o', 'w', 's', 'i', 't', 'g', 'o', 'i', 'n', 'g', '?']
list to String : Hey, how's it going?

如果需要,您可以尝试列表理解:

string_1="Hey, how's it going?"



origional_list=[i for i in string_1]
string_to_list=[i for i in origional_list if i!=',' and i!="'" and i!=' ']

print("String to list : {}".format(string_to_list))
list_to_string=("".join(origional_list))


print("list to String : {}".format(list_to_string))
输出:

String to list : ['H', 'e', 'y', 'h', 'o', 'w', 's', 'i', 't', 'g', 'o', 'i', 'n', 'g', '?']
list to String : Hey, how's it going?

因为在每一次迭代中,你都会删除一个句子,这样它的长度就会减少。但是for循环仍然会遍历初始句子长度。

因为每次迭代都会删除一个句子项,因此句子长度会减少。但是for循环仍然会遍历初始句子长度。

而语句
?此外,永远不要删除当前正在迭代的列表中的元素。为什么不干脆
setence=setence.split(“”);返回“”。连接(句子)
?或
返回“”。加入(setence.split(“”))
。在执行之前,您应该学习一些基本知识。。。在while循环中删除。首先,使用一个FOR循环,或者更好,一个已经存在的函数。@ChristianDean,因为在下一步中,我想对元素进行排序并创建一个新的字符串
,而句子
?此外,永远不要删除您当前迭代的列表中的元素。为什么不干脆
setence=setence.split(“”);返回“”。连接(句子)
?或
返回“”。加入(setence.split(“”))
。在执行之前,您应该学习一些基本知识。。。在while循环中删除。首先,使用FOR循环,或者更好,使用一个已经存在的函数。@ChristianDean,因为在下一步中,我想对元素进行排序并创建一个新的stringSo,所以我不能在FOR循环中使用remove函数?如果使用索引,则不建议使用。因此,我不能在FOR循环中使用remove函数?如果使用索引,则不建议使用。