简体   繁体   English

如何使用另一个列表中的所有项目划分一个列表中的所有项目,并进行所有可能的组合?

[英]How to divide all items in one list using all items in another list, and doing all possible combinations?

I'm having a bit of a problem here. 我在这里有点问题。 For example, there are 2 lists: 例如,有2个列表:

a=[12,3,4,6,2]
b=[6,2,1,3,12]

What I want to do is make a new list which consists of: 我要做的是制作一个新列表,其中包括:

c=[2,6,12,4,1,0.5, ...]

Also, is there any way to do this without importing anything? 此外,有什么方法可以执行此操作而不导入任何内容吗?

Using set comprehension (to prevent duplicated items): 使用集合理解(以防止重复项):

>>> a = [12,3,4,6,2]
>>> b = [6,2,1,3,12]
>>> c = {x/y for x in a for y in b}  # float(x)/y  in Python 2.x
>>> c
{0.5, 1.0, 2.0, 1.5, 4.0, 3.0, 6.0, 0.25, 0.6666666666666666, 1.3333333333333333, 
 0.16666666666666666, 12.0, 0.3333333333333333}

Use list to get a list object instead of a set : 使用list获取列表对象而不是set

>>> list(c)
[0.5, 1.0, 2.0, 1.5, 4.0, 3.0, 6.0, 0.25, 0.6666666666666666, 1.3333333333333333,
 0.16666666666666666, 12.0, 0.3333333333333333]

You could use list comprehensions: 您可以使用列表推导:

 [float(x)/y for x in a for y in b] [2.0, 6.0, 12.0, 4.0, 1.0, 0.5, 1.5, 3.0, 1.0, 0.25, 0.6666666666666666, 2.0, 4.0, 1.3333333333333333, 0.3333333333333333, 1.0, 3.0, 6.0, 2.0, 0.5, 0.3333333333333333, 1.0, 2.0, 0.6666666666666666, 0.16666666666666666] 

If you want the result in fractions you should import the fractions module: 如果要分数结果,则应导入分数模块:

 import fractions set([fractions.Fraction(x)/y for x in a for y in b]) set([Fraction(1, 2), Fraction(1, 1), Fraction(2, 1), Fraction(3, 1), Fraction(4, 1), Fraction(6, 1), Fraction(12, 1), Fraction(1, 3), Fraction(2, 3), Fraction(4, 3), Fraction(1, 6), Fraction(1, 4), Fraction(3, 2)]) 

You can use itertools.product to get all the pairs to substract: 您可以使用itertools.product减去所有对。

>>> import itertools
>>> pairs = list(itertools.product(*(a,b)))
>>> pairs
[(12, 6), (12, 2), (12, 1), (12, 3), (12, 12), (3, 6), (3, 2), (3, 1), (3, 3), (3, 12), (4, 6), (4, 2), (4, 1), (4, 3), (4, 12), (6, 6), (6, 2), (6, 1), (6, 3), (6, 12), (2, 6), (2, 2), (2, 1), (2, 3), (2, 12)]

Then you divide: 然后划分:

>>> [a/b for a,b in pairs]
[2, 6, 12, 4, 1, 0, 1, 3, 1, 0, 0, 2, 4, 1, 0, 1, 3, 6, 2, 0, 0, 1, 2, 0, 0]
>>> 

If you want floats: 如果要浮动:

>>> [float(a)/b for a,b in pairs]
[2.0, 6.0, 12.0, 4.0, 1.0, 0.5, 1.5, 3.0, 1.0, 0.25, 0.6666666666666666, 2.0, 4.0, 1.3333333333333333, 0.3333333333333333, 1.0, 3.0, 6.0, 2.0, 0.5, 0.3333333333333333, 1.0, 2.0, 0.6666666666666666, 0.16666666666666666]

Putting all together in a one-liner: 将所有内容放在一起:

>>> [float(a)/b for a,b in it.product(*(a,b))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用python中所有可能组合的另一个列表替换一个项目列表 - Replace one list of items with another list in all possible combinations in python 如何生成所有可能的组合,将项目从一个列表添加到 python 中的另一个列表? - How generate all possible combinations adding items from one list to another in python? Python:清单项目集的所有可能组合 - Python: all possible combinations of list items Set 如何将一个列表分成两个列表并获得所有可能的组合 - how to divide a list to two lists and get all possible combinations 如何检查列表中的所有项目是否都在另一个列表中? - How to check if all items in a list are there in another list? 列表中项目的所有组合。 所有包含所有项目 - all combinations of items in a list. all containing alle items 获取列表项及其名称的所有组合 - Get all combinations of list items with their names 如何找到从第一个列表中选择一个项目和从第二个列表中选择最多 n 个项目的所有组合 - How to find all combinations of picking one item from the first list and at most n items from the second list 您将如何递归获取列表中项目的所有组合 - How Would You Recursively Get All The Combinations Of Items Within A List 如何列出dict.items()中所有可能的3个字母组合? - How can I list all possible 3 letter combinations from dict.items()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM