Python,匹配两个列表的元素

发布于 2024-11-18 18:54:10 字数 271 浏览 2 评论 0原文

x= [0,2,3,5,6];
y= [64,384,1024,4096,384];

上面是我正在使用的两个数组。我试图以Pythonic方式将元素匹配在一起,

例如:

如果xType是2,我想计算一个名为yType的变量来对应于它在y中的值(位置明智) 。所以我应该得到y = 384。如果 xType = 3 我应该得到 1024。

我该如何做呢

x= [0,2,3,5,6];
y= [64,384,1024,4096,384];

The above are two arrays I'm using. Im trying to match the elements together in a pythonic way

example:

if xType is 2 i want to compute a variable called yType to correspond to its value(position wise) in y. so i should get y = 384. if xType = 3 i should get 1024.

How would i go about doing this

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

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

发布评论

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

评论(5

暗地喜欢 2024-11-25 18:54:10

如果您的具体目标是从给定的两个列表中生成一个 dict,请使用 zip:

>>> x = [0,2,3,5,6]
>>> y = [64,384,1024,4096,384]
>>> dict(zip(x, y))
{0: 64, 2: 384, 3: 1024, 5: 4096, 6: 384}

并去掉那些分号!

如果您不需要映射类型,而只想创建项目对,则单独使用 zip 即可:

>>> zip(x, y)
[(0, 64), (2, 384), (3, 1024), (5, 4096), (6, 384)]

If your specific aim is to generate a dict from the two lists you've given, use zip:

>>> x = [0,2,3,5,6]
>>> y = [64,384,1024,4096,384]
>>> dict(zip(x, y))
{0: 64, 2: 384, 3: 1024, 5: 4096, 6: 384}

And get rid of those semicolons!

If you don't need a mapping type, but just want to create pairs of items, zip alone will do:

>>> zip(x, y)
[(0, 64), (2, 384), (3, 1024), (5, 4096), (6, 384)]
删除→记忆 2024-11-25 18:54:10

这太短了,甚至 Stack Overflow 也不允许我提交如此简短的答案:

y[x.index(2)]

这将从 y 中返回与 2 或任何其他给定值的位置相对应的元素从 x 列表中。

希望它有帮助

:)事实上,字典可能是您需要的东西。尝试使用它们。

This is so short, even Stack Overflow did not allow me to submit such a short answer:

y[x.index(2)]

This will return element from y corresponding to the position of 2 or any other given value from within x list.

Hope it helped :)

Ps. Indeed dictionaries may be something you need. Try using them.

征棹 2024-11-25 18:54:10

如果 x 中的元素是唯一的,您可以将它们用作 dict 查找 y 中具有相同索引的元素。像这样:

x = [0,2,3,5,6]
y = [64,384,1024,4096,384]

y_from_x = dict(zip(x,y))

print y_from_x[2] # prints 384
print y_from_x[3] # prints 1024

如果您想要进行大量查找,但如果您只想进行一次查找,这很有用 Tadeck 的回答效率更高

If the elements in x are unique, you can use them as the keys in a dict to lookup the elements in y that have the same index. Like this:

x = [0,2,3,5,6]
y = [64,384,1024,4096,384]

y_from_x = dict(zip(x,y))

print y_from_x[2] # prints 384
print y_from_x[3] # prints 1024

This is useful if you want to do lots of lookups, but if you want to do just one lookup Tadeck's answer is more efficient

一场春暖 2024-11-25 18:54:10
a = {0: 64, 2:384, ...}

查找“python 中的地图”或类似的内容

a = {0: 64, 2:384, ...}

look up 'maps in python' or something like that

思念绕指尖 2024-11-25 18:54:10
>>> xymap = dict(zip(x, y))
>>> xymap[2]
384

但是,如果您还需要在 x 中查找 y 中的元素,则还需要 yxmap。如果您出于某种原因需要将它们作为列表(可能是因为您在程序过程中修改它们),您可以使用 i = x.index(2) ,然后使用 y[i]

>>> xymap = dict(zip(x, y))
>>> xymap[2]
384

However, if you also need to lookup elements in x for y, you'll need a yxmap as well. And if you need these to be lists for some reason (perhaps because you're modifying them during the course of your program), you could use i = x.index(2) and then y[i].

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