简体   繁体   English

python中值的所有可能组合

[英]All possible combinations of values in python

Version: Python 3.4.3 版本: Python 3.4.3

Hi, I'm trying to create an script that reads the availible options from some select items in an html file and creates a database with all the possible choices assigning them an unique ID based on their values. 嗨,我正在尝试创建一个脚本,该脚本从html文件中的某些选择项中读取可用选项,并创建一个数据库,其中包含所有可能的选择,并根据它们的值为其分配唯一的ID。

This is the structure of the html: 这是html的结构:

              <select id="perforar" onchange="Actualiza(this.id, this.options[this.selectedIndex].value)">
              <option value="g1">sin perforacion</option>
              <option value="g2">1 Linea de perforación</option>
              <option value="g3">2 Lineas de perforación</option>
              <option value="g4">3 Lineas de perforación</option>
              <option value="g5">4 Lineas de perforación</option>
              <option value="g6">5 Lineas de perforación</option>
              <option value="g7">6 Lineas de perforación</option>
            </select></td>
            </tr><tr><td>Ennoblecimiento: </td><td>
            <select id="ennoblecimiento" onchange="Actualiza(this.id, this.options[this.selectedIndex].value)">
              <option value="h1">sin ennoblecimiento</option>
              <option value="h2">barniz UV</option>
              <option value="h3">laminado</option>
            </select></td>
            </tr><tr><td>Plegado: </td><td>
            <select id="plegado" onchange="Actualiza(this.id, this.options[this.selectedIndex].value)">
              <option value="i1">plegado envolvente</option>
              <option value="i2">plegado en acordéon</option>
              <option value="i3">plegado en ventana</option>

I manually copy/paste all that into a .txt and then run this code: 我手动将所有内容复制/粘贴到.txt中,然后运行以下代码:

#load file into buffer
leyendo = open("generadorbasedatos.txt", 'r')
archivotxt = leyendo.read()
leyendo.close()
#split it for lines
listadividida = []
listadividida= archivotxt.split("\n")
#create a dict for later
basededatos = {}

#for each line
for i in listadividida:
    if not "<option" in i: #if isn't an option, delete that line
        i = ""
    else: #if it's an option, get the value and the text
        #the text
        desde = '>'
        hasta = '<'
        _,_,resto = i.partition(desde)
        opcion,_,_ = resto.partition(hasta)
        #the value
        desde = 'value="'
        hasta = '">'
        _,_,resto = i.partition(desde)
        laid,_,_ = resto.partition(hasta)
        #add them to a dict
        basededatos[laid] = [opcion, laid]
        #And this is where I'm lost and I need help
print(basededatos)

Now comes the trouble, I want the script to create a list of all possible combinations and assign an ID for each combination using the values to create the ID, so the output should look like this: 现在麻烦了,我希望脚本创建所有可能组合的列表,并使用值创建ID为每个组合分配一个ID,因此输出应如下所示:

g1h1i1: [1 Linea de perforación, Sin ennoblecimiento, plegado envolvente]
g1h1i2: [1 Linea de perforación, Sin ennoblecimiento, plegado en acordeón]
g1h1i3: [1 Linea de perforación, Sin ennoblecimiento, plegado en ventana]
g1h2i1: [1 Linea de perforación, barniz, plegado envolvente]
g1h2i2: [1 Linea de perforación, barniz, plegado plegado en acordeón]
g1h3i3: [1 Linea de perforación, barniz, plegado en ventana]

And eventually all possible combinations. 最终所有可能的组合。 I tried with itertools and somehow managed to freeze my computer (probably due to out of memory or endless loops issues) so now I'm asking here. 我尝试使用itertools并设法冻结了我的计算机(可能是由于内存不足或无休止的循环问题所致),所以现在我在这里问。

What's the best way to achieve what I want to do? 实现我想要做的最好的方法是什么?

Notice: There is more than 12 selects, only copy/pasted 3 of them here as example, but the code should be able to create all combinations of more than just 3 selects. 注意:这里有超过12个选择,这里仅复制/粘贴了3个选择,但是代码应该能够创建超过3个选择的所有组合。

From your point, this achieve you goal : 从您的角度来看,这可以实现您的目标:

from itertools import product
base={'g':[],'h':[],'i':[]}
for (key,value) in basededatos.items(): base[key[0]].append(value) # to split the fields.
products=product(*base.values()) #make all combinations
finaldict={ "".join([p[1] for p in t]) : [p[0] for p in t] for t in products }
# formatting in a dictionnary.

Some values : 一些值:

In [263]: base
Out[263]: 
{'g': [['1 Linea de perforación', 'g2'],
  ['4 Lineas de perforación', 'g5'],
  ['2 Lineas de perforación', 'g3'],
  ....,
 'h': [['laminado', 'h3'], ['barniz UV', 'h2'], ['sin ennoblecimiento', 'h1']],
 'i': [['plegado en ventana', 'i3'],
  ['plegado en acordéon', 'i2'],
  ....]}


In [265]: finaldict
Out[265]: 
{'g1h3i2': ['sin perforacion', 'laminado', 'plegado en acordéon'],
'g7h2i1': ['6 Lineas de perforación', 'barniz UV', 'plegado envolvente'],
'g2h3i3': ['1 Linea de perforación', 'laminado', 'plegado en ventana'],....

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM