组合字符串

发布于 2024-12-19 12:28:18 字数 565 浏览 0 评论 0原文

假设我有 2 个字符串列表,我想通过组合这两个列表来创建一个新列表,以便第一个列表中的第一个字符串将与第二个列表中的第一个单词位于一个元组中,第二个与第二个单词位于一个元组中等等...

举个例子:

input: lst1 = ["hello", "first", "is"]
input: lst2 = ["my", "name", "tom"]
output: [("hello","my"), ("first", "name"), ("is", "tom")]

我写了类似的东西:

lst1 = ["hello", "first", "is"]
lst2 = ["my", "name", "tom"]
new_list = []
    for i in lst1 :
            for j in lst2 :
                    two_words = (i, j)
                    new_list.append(two_words)
    return new_list

我在这里做错了什么?

Let's say I have 2 lists of strings, and I want to make a new list by combining the two lists so that the first string in the first list, will be in a tuple with the first word in the second list, second with second and so on...

Just for example:

input: lst1 = ["hello", "first", "is"]
input: lst2 = ["my", "name", "tom"]
output: [("hello","my"), ("first", "name"), ("is", "tom")]

I wrote something like that:

lst1 = ["hello", "first", "is"]
lst2 = ["my", "name", "tom"]
new_list = []
    for i in lst1 :
            for j in lst2 :
                    two_words = (i, j)
                    new_list.append(two_words)
    return new_list

What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

场罚期间 2024-12-26 12:28:19

使用列表推导式是最好、最简单的方法。

lst1 = ["hello", "first", "is"]
lst2 = ["my", "name", "tom"]
new_list = [ ( lst1[i], lst2[i] ) for i in range ( len(lst1) ) ]

如果您希望内部元素位于列表中,
使用

new_list = [ [ lst1[i], lst2[i] ] for i in range ( len(lst1) ) ]

输出:

[('hello', 'my'), ('first', 'name'), ('is', 'tom')]

The best and simplest way to do it using list comprehensions.

lst1 = ["hello", "first", "is"]
lst2 = ["my", "name", "tom"]
new_list = [ ( lst1[i], lst2[i] ) for i in range ( len(lst1) ) ]

if you want the inner elements to be in a list,
use

new_list = [ [ lst1[i], lst2[i] ] for i in range ( len(lst1) ) ]

output:

[('hello', 'my'), ('first', 'name'), ('is', 'tom')]
简单 2024-12-26 12:28:18

zip 是您要查找的内容:

>>> lst1 = ["hello", "first", "is"]
>>> lst2 = ["my", "name", "tom"]
>>> zip(lst1,lst2)
[('hello', 'my'), ('first', 'name'), ('is', 'tom')]

更多信息:http ://docs.python.org/library/functions.html#zip

zip is what you're looking for:

>>> lst1 = ["hello", "first", "is"]
>>> lst2 = ["my", "name", "tom"]
>>> zip(lst1,lst2)
[('hello', 'my'), ('first', 'name'), ('is', 'tom')]

More about it: http://docs.python.org/library/functions.html#zip

━╋う一瞬間旳綻放 2024-12-26 12:28:18

Julio 的答案实际上是Pythonic 的做法。但至于你的问题你做错了什么,你的错误就在这里:

    for i in lst1 :
            for j in lst2 :

你不想像这样迭代列表,因为你只想要一个与两个列表大小相同的结果。假设两个列表的大小相同,那就简单了

lst1 = ["hello", "first", "is"]
lst2 = ["my", "name", "tom"]
new_list = []
    for i in range(len(lst1)):
                    new_list.append((lst1[i], list2[i]))
    return new_list

Julio's answer is in fact the pythonic way of doing it. But as to your question what you're doing wrong, your error is here:

    for i in lst1 :
            for j in lst2 :

You don't want to iterate over the lists like that, because you only want a result that is the same size as both lists. Assuming both lists are the same size, it would simply be

lst1 = ["hello", "first", "is"]
lst2 = ["my", "name", "tom"]
new_list = []
    for i in range(len(lst1)):
                    new_list.append((lst1[i], list2[i]))
    return new_list
请别遗忘我 2024-12-26 12:28:18

您的问题是循环内的循环形成“叉积”,从两个列表创建每个可能的字符串对。解决方案是使用zip,或者对可能的索引进行单个循环。

Your problem is that a loop inside a loop forms a "cross product", creating every possible pair of strings from the two lists. The solution is to use zip, or to make a single loop over the possible indices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文