列表乘法

发布于 2024-08-20 02:05:31 字数 164 浏览 2 评论 0原文

我有一个列表 L = [a, b, c] 我想生成一个元组列表:

[(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...] 

我尝试做 L * L 但它不起作用。有人可以告诉我如何在 python 中得到这个吗?

I have a list L = [a, b, c] and I want to generate a list of tuples :

[(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...] 

I tried doing L * L but it didn't work. Can someone tell me how to get this in python.

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

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

发布评论

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

评论(7

南笙 2024-08-27 02:05:31

您可以使用列表理解来完成此操作:

[ (x,y) for x in L for y in L]

edit

您也可以按照其他人的建议使用 itertools.product,但前提是您使用的是 2.6 及以上版本。列表理解适用于 2.0 以后的所有 Python 版本。如果您确实使用 itertools.product,请记住它返回一个生成器而不是列表,因此您可能需要转换它(取决于您想用它做什么)。

You can do it with a list comprehension:

[ (x,y) for x in L for y in L]

edit

You can also use itertools.product as others have suggested, but only if you are using 2.6 onwards. The list comprehension will work will all versions of Python from 2.0. If you do use itertools.product bear in mind that it returns a generator instead of a list, so you may need to convert it (depending on what you want to do with it).

嘦怹 2024-08-27 02:05:31

itertools 模块包含许多用于此类排序的有用函数的东西。您可能正在寻找 产品

>>> import itertools
>>> L = [1,2,3]
>>> itertools.product(L,L)
<itertools.product object at 0x83788>
>>> list(_)
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]

The itertools module contains a number of helpful functions for this sort of thing. It looks like you may be looking for product:

>>> import itertools
>>> L = [1,2,3]
>>> itertools.product(L,L)
<itertools.product object at 0x83788>
>>> list(_)
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
泪意 2024-08-27 02:05:31

看一下 itertools 模块,它提供了一个 product 成员。

L =[1,2,3]

import itertools
res = list(itertools.product(L,L))
print(res)

给出:

[(1,1),(1,2),(1,3),(2,1), ....  and so on]

Take a look at the itertools module, which provides a product member.

L =[1,2,3]

import itertools
res = list(itertools.product(L,L))
print(res)

Gives:

[(1,1),(1,2),(1,3),(2,1), ....  and so on]
梦毁影碎の 2024-08-27 02:05:31

两种主要替代方案:

>>> L = ['a', 'b', 'c']
>>> import itertools
>>> list(itertools.product(L, L))
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]
>>> [(one, two) for one in L for two in L]
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]
>>> 

前一种需要 Python 2.6 或更高版本,后者适用于您可能使用的任何 Python 版本。

Two main alternatives:

>>> L = ['a', 'b', 'c']
>>> import itertools
>>> list(itertools.product(L, L))
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]
>>> [(one, two) for one in L for two in L]
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]
>>> 

the former one needs Python 2.6 or better -- the latter works in just about any Python version you might be tied to.

不一样的天空 2024-08-27 02:05:31

x = [a,b,c]
y = []
对于 x 中的项目:
对于 x 中的第 2 项:
y.append((item, item2))

也许不是 Pythonic 方式,但可以工作

x = [a,b,c]
y = []
for item in x:
for item2 in x:
y.append((item, item2))

Maybe not the Pythonic way but working

自由如风 2024-08-27 02:05:31

好吧,我尝试过:

L2 = [(x,y) for x in L for x in L] 这得到了 L 平方。

这是最好的Python方式吗?我希望 L * L 能在 python 中工作。

Ok I tried :

L2 = [(x,y) for x in L for x in L] and this got L square.

Is this the best pythonic way to do this? I would expect L * L to work in python.

近箐 2024-08-27 02:05:31

最老式的方法是:

def perm(L):
    result = []
    for i in L:
        for j in L:
            result.append((i,j))
    return result

它的运行时间为 O(n^2),因此相当慢,但您可以认为它是“老式”风格的代码。

The most old fashioned way to do it would be:

def perm(L):
    result = []
    for i in L:
        for j in L:
            result.append((i,j))
    return result

This has a runtime of O(n^2) and is therefore quite slow, but you could consider it to be "vintage" style code.

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