#!/usr/bin/python
#
# Description: I try to simplify the implementation of the thing below.
# Sets, such as (a,b,c), with irrelavant order are given. The goal is to
# simplify the messy "assignment", not sure of the term, below.
#
#
# QUESTION: How can you simplify it?
#
# >>> a=['1','2','3']
# >>> b=['bc','b']
# >>> c=['#']
# >>> print([x+y+z for x in a for y in b for z in c])
# ['1bc#', '1b#', '2bc#', '2b#', '3bc#', '3b#']
#
# The same works with sets as well
# >>> a
# set(['a', 'c', 'b'])
# >>> b
# set(['1', '2'])
# >>> c
# set(['#'])
#
# >>> print([x+y+z for x in a for y in b for z in c])
# ['a1#', 'a2#', 'c1#', 'c2#', 'b1#', 'b2#']
#BROKEN TRIALS
d = [a,b,c]
# TRIAL 2: trying to simplify the "assignments", not sure of the term
# but see the change to the abve
# print([x+y+z for x, y, z in zip([x,y,z], d)])
# TRIAL 3: simplifying TRIAL 2
# print([x+y+z for x, y, z in zip([x,y,z], [a,b,c])])
[Update] Una cosa che manca, che dire se davvero hanno for x in a for y in b for z in c ...
, cioè quantità arbirtary di strutture, scrivendo product(a,b,c,...)
è ingombrante. Supponiamo di avere un elenco di elenchi come lo d
nell'esempio precedente. Puoi farlo più semplice? Python ci lascia fare unpacking
con *a
per gli elenchi e la valutazione del dizionario con **b
ma è solo la notazione. Nested for-loops di lunghezza arbitraria e semplificazione di tali mostri va oltre SO, per ulteriori ricerche here. Voglio sottolineare che il problema nel titolo è a tempo indeterminato, quindi non essere fuorviato se accetto una domanda!Come posso semplificare "per x in a per y in b per z in c ..." con non ordinato?
@HH, ho aggiunto al mio rispondi ad es. 'product (* d)' è equivalente a 'product (a, b, c)' –