Python:列表解析可访问多个列表

时间:2018-09-30 08:40:15

标签: python list list-comprehension

list1 = []
list.append([item1[i], item2[i], item3[i] for i in range(2)])

i.e如何通过列表理解(其中item1,item2,item3是不同的列表)用[[item1[0], item2[0], item3[0]],[item1[1], item2[1], item3[1]]]填充list1? 示例:

item1 = [1,2,3,4]
item2 = [1,4,9,16]
item3 = [1,8,27,64]

# I want list1 =
[[1,1,1],
[2,4,8],
[3,9,27],
[4,16,64]]
# through list comprehension AND APPEND STATEMENT

3 个答案:

答案 0 :(得分:4)

尝试一下:

+++
# cat /etc/os-release
NAME="SLES"
VERSION="15"
VERSION_ID="15"
PRETTY_NAME="SUSE Linux Enterprise Server 15"
ID="sles"
ID_LIKE="suse"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:suse:sles:15"

#uname -a
Linux linux-iv0h 4.12.14-23-default #1 SMP Tue May 29 21:04:44 UTC 2018 (cd0437b) x86_64 x86_64 x86_64 GNU/Linux

#ps aux|grep httpd
root     11143  0.0  0.6  23612  6380 ?        Ss   04:33   0:00 ./httpd
nobody   11144  0.0  0.3  23612  3248 ?        S    04:33   0:00 ./httpd
nobody   11145  0.0  0.3  23612  3248 ?        S    04:33   0:00 ./httpd
nobody   11146  0.0  0.3  23612  3248 ?        S    04:33   0:00 ./httpd
nobody   11147  0.0  0.3  23612  3248 ?        S    04:33   0:00 ./httpd
nobody   11148  0.0  0.3  23612  3248 ?        S    04:33   0:00 ./httpd
+++

这将一直起作用,直到最小长度为list1 = list(zip(item1, item2, item3)) item1item2。如果您想使用更长的时间,请使用item3而不是itertools.zip_longest

zip的示例:

zip

item1 = [0,1] item3 = [2,3] item2 = [4,5,6] list1 = list(zip(item1, item2, item3)) [(0, 4, 2), (1, 5, 3)] 的示例:

itertools.zip_longest

答案 1 :(得分:3)

在您的代码中添加另一组class MonteCarloTreeSearch: def __init__(self, node: MonteCarloTreeSearchNode): self.root = node def best_action(self, simulations_number): p = list() for i in range(0, simulations_number): p.append(Process(target=self.simulate)) p[i].start() for j in range(0, simulations_number): p[j].join() print(hex(id(self.root.children))) return self.root.best_child() def simulate(self): v = self.tree_policy() reward = v.rollout() # who win? # exploitation only v.backpropagate(reward) print(hex(id(self.root.children)))

Some text

.. glossary::

  Term 1
    Definition

  Term 2
    Definition

A link to :term:`Term 1` or :term:`Term 2`

请注意,这假设[]list1 = [] list1.append([[item1[i], item2[i], item3[i]] for i in range(len(item1))]) item1的长度都相同

或者,为完全匹配您的预期输出,请使用以下命令:

item2

示例数据和输出

item3

使用list1 = [[item1[i], item2[i], item3[i]] for i in range(len(item1))] ,列表理解变为:

item1 = [1, 2, 3]
item2 = ["A", "B", "C"]
item3 = [0.1, 0.2, 0.3]
list1 = [[item1[i], item2[i], item3[i]] for i in range(len(item1))]
print(list1)

>>> [[1, 'A', 0.1], [2, 'B', 0.2], [3, 'C', 0.3]]

但是,如果要使用列表理解并将 still 附加到.append()上,请使用for i in range(len(item1)): list1.append([item1[i], item2[i], item3[i]]) 而不是list1

+=

.append()将给定项目添加到列表的末尾。 list1 += [[item1[i], item2[i], item3[i]] for i in range(len(item1))] 将改为分别添加每个子列表。

答案 2 :(得分:0)

如果在python 2上,只需执行zip而不使用list

zip(item1, item2, item3)

演示:

item1 = [1, 2, 3]
item2 = [4,5,6]
item3 = [7,8,9]
print(zip(item1, item2, item3))

输出:

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]