将两个列表的每个元素组合到 Python 中的一个元组而不使用 itertools 数据包
combine each element of two lists to a tuple in Python without using itertools packet
我是新手,我有两个列表,想通过随机所有可能的元素将它们组合成一个元组,不使用任何数据包,例如 itertools packet。
像这个例子:
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
和输出:
>>> new_tuple = (["a","wow"],["b","wow"],["c","wow"],["a",2],["b",2],["c",2])
你能帮帮我吗?提前谢谢你
您可以使用 itertools
进行您请求的排列,如下所示:
import itertools
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
all_combinations = []
list1_permutations = itertools.permutations(list1, len(list2))
for each_permutation in list1_permutations:
zipped = zip(each_permutation, list2)
all_combinations.append(list(zipped))
print(all_combinations)
输出如下所示:
[[('a', 'wow'), ('b', 2)], [('a', 'wow'), ('c', 2)], [('b', 'wow'), ('a', 2)], [('b', 'wow'), ('c', 2)], [('c', 'wow'), ('a', 2)], [('c', 'wow'), ('b', 2)]]```
使用itertools.product
:
from itertools import product
new_tuple = tuple([a, b] for b, a in product(list2, list1))
# (['a', 'wow'], ['b', 'wow'], ['c', 'wow'], ['a', 2], ['b', 2], ['c', 2])
Python3 使用列表生成器的单行
list1 = ['a', 'b', 'c']
list2 = ['wow', 2]
new_tuple = tuple([l1, l2] for l2 in list2 for l1 in list1)
print(new_tuple)
# (['a', 'wow'], ['b', 'wow'], ['c', 'wow'], ['a', 2], ['b', 2], ['c', 2])
import random
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
new_tuple = ()
for x in (len(list1)+len(list2)):
randInt = random.randint(0, len(list1))
randInt2 = random.randint(0, len(list2))
new_tuple += [list1[randInt], list2[randInt2]]
这真的很简单python。试试代码。
你要的基本上是笛卡尔积。尝试使用 itertools 进行列表理解,例如:
import itertools
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
new_tuple = [x for x in itertools.product(list1, list2)]
要将 2 列表转换为元组,有方法 .zip:
list(zip(list1, list2))
但它只是在你的情况下添加元素:(["a", "wow"], ["b", "2"]], 所以:
a = ("John", "Charles", "Mike")
b = ("Jenny", "Christy", "Monica")
tuples = []
def combineTuples(listA, listB):
for i in range(len(listA)):
for j in range(len(listB)):
tuples.append((a[i], b[j]))
x = combineTuples(a, b)
print(tuples)
输出:
[('John', 'Jenny'), ('John', 'Christy'), ('John', 'Monica'), ('Charles', 'Jenny'), ('Charles', 'Christy'), ('Charles', 'Monica'), ('Mike', 'Jenny'), ( 'Mike', 'Christy'), ('Mike', 'Monica')]
您的数据输出是相同的,但没有顺序。
[('a', 'wow'), ('a', 2), ('b', 'wow'), ('b' , 2), ('c', 'wow'), ('c', 2)]
您可以使用 itertools
import itertools
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
c = tuple(itertools.product(list1, list2))
print(c)
我是新手,我有两个列表,想通过随机所有可能的元素将它们组合成一个元组,不使用任何数据包,例如 itertools packet。 像这个例子:
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
和输出:
>>> new_tuple = (["a","wow"],["b","wow"],["c","wow"],["a",2],["b",2],["c",2])
你能帮帮我吗?提前谢谢你
您可以使用 itertools
进行您请求的排列,如下所示:
import itertools
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
all_combinations = []
list1_permutations = itertools.permutations(list1, len(list2))
for each_permutation in list1_permutations:
zipped = zip(each_permutation, list2)
all_combinations.append(list(zipped))
print(all_combinations)
输出如下所示:
[[('a', 'wow'), ('b', 2)], [('a', 'wow'), ('c', 2)], [('b', 'wow'), ('a', 2)], [('b', 'wow'), ('c', 2)], [('c', 'wow'), ('a', 2)], [('c', 'wow'), ('b', 2)]]```
使用itertools.product
:
from itertools import product
new_tuple = tuple([a, b] for b, a in product(list2, list1))
# (['a', 'wow'], ['b', 'wow'], ['c', 'wow'], ['a', 2], ['b', 2], ['c', 2])
Python3 使用列表生成器的单行
list1 = ['a', 'b', 'c']
list2 = ['wow', 2]
new_tuple = tuple([l1, l2] for l2 in list2 for l1 in list1)
print(new_tuple)
# (['a', 'wow'], ['b', 'wow'], ['c', 'wow'], ['a', 2], ['b', 2], ['c', 2])
import random
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
new_tuple = ()
for x in (len(list1)+len(list2)):
randInt = random.randint(0, len(list1))
randInt2 = random.randint(0, len(list2))
new_tuple += [list1[randInt], list2[randInt2]]
这真的很简单python。试试代码。
你要的基本上是笛卡尔积。尝试使用 itertools 进行列表理解,例如:
import itertools
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
new_tuple = [x for x in itertools.product(list1, list2)]
要将 2 列表转换为元组,有方法 .zip:
list(zip(list1, list2))
但它只是在你的情况下添加元素:(["a", "wow"], ["b", "2"]], 所以:
a = ("John", "Charles", "Mike")
b = ("Jenny", "Christy", "Monica")
tuples = []
def combineTuples(listA, listB):
for i in range(len(listA)):
for j in range(len(listB)):
tuples.append((a[i], b[j]))
x = combineTuples(a, b)
print(tuples)
输出: [('John', 'Jenny'), ('John', 'Christy'), ('John', 'Monica'), ('Charles', 'Jenny'), ('Charles', 'Christy'), ('Charles', 'Monica'), ('Mike', 'Jenny'), ( 'Mike', 'Christy'), ('Mike', 'Monica')]
您的数据输出是相同的,但没有顺序。
[('a', 'wow'), ('a', 2), ('b', 'wow'), ('b' , 2), ('c', 'wow'), ('c', 2)]
您可以使用 itertools
import itertools
list1 = ["a", "b", "c"]
list2 = ["wow", 2]
c = tuple(itertools.product(list1, list2))
print(c)