user1819717
user1819717

Reputation: 133

Printing from 2 lists on one line

What I have so far:

def balance_equation(species,coeff):
  data=zip(coeff,species)
  positive=[]
  negative=[]
  for (mul,el) in data:
    if int(mul)<0:
      negative.append((el,mul))
    if int(mul)>0:
      positive.append((el,mul))

I know this does not print anything. What I have is a function that takes in two lists species=['H2O','O2','CO2'] and coeff=['1','3','-4']. I need it to print like so: 1H20+3O2=4CO2

I started by putting the negative coeff and species in one list and the positive in the other. I just can seem to be able to get the two to print right.

Upvotes: 2

Views: 153

Answers (2)

irrelephant
irrelephant

Reputation: 4111

Try this:

species = ["H2O", "CO2", "O2"]
coeff = ['1', '-4', '3']

pos = [c + s for c, s in zip(coeff, species) if int(c) > 0]
neg = [c[1:] + s for c, s in zip(coeff, species) if int(c) < 0]
print ("+".join(pos))+"="+("+".join(neg))

EDIT: I took out the spaces. 2nd EDIT: coeff is a list of strings.

You should also test if pos or neg are empty to replace them with 0s when appropriate. It appears that the coefficients are integers.

Upvotes: 7

abarnert
abarnert

Reputation: 365597

Breaking things down into steps is a good way to solve things (you can always recombine the steps later), and you've got 80% of the way there.

You already have positive and negative lists. So, you need to convert each one into a string, then just:

print poshalf, "=", neghalf

So, how do you convert positive into poshalf? Well, it's a representation of each member, separated by '+', so if you had a function stringify that could turn each member into its representation, it's just:

poshalf = '+'.join(stringify(el, mul) for (el, mul) in pos)
neghalf = '+'.join(stringify(el, mul)[1:] for (el, mul) in neg)

The [1:] there is to take out the - sign. If mul is actually an integer rather than a string, it probably makes sense to just negate the value before passing it to stringify:

neghalf = '+'.join(stringify(el, -mul) for (el, mul) in neg)

Now, what does that "stringify" function look like? Well, each one member is an (el, mul) pair. If they were both strings, you could just add them. From your previous questions, mul may end up being some kind of number at this point, but that's almost as easy:

def stringify(el, mul):
    return str(mul) + el

Put it all together, and you're done.

One way to make this all simpler: If you never use the (el, mul) for any other purpose except to call stringify on it, just call stringify in the first place and store the result:

def balance_equation(species,coeff):
  data=zip(coeff,species)
  positive=[]
  negative=[]
  for (mul,el) in data:
    if int(mul)<0:
      negative.append(str(mul)[1:] + el)
    if int(mul)>0:
      positive.append(str(mul) + el)
  return positive, negative

Remember that last line, which you've left off both versions of your previous question and this question! If you never return the values, all that effort figuring them out is wasted, and the caller just gets None as an answer.

Obviously either the str or the int is unnecessary, but I've left them both in for safety; you should look at your surrounding code and remove the unnecessary one. (If you're taking mul as an int, you probably want str(-mul) instead of str(mul)[1:], as described earlier.)

Once you've got this, if you understand list comprehensions, you might realize that this is a familiar pattern: start with [], loop over some other collection, and append each value that meets some test. In other words:

def balance_equation(species,coeff):
  data=zip(coeff,species)
  positive = [str(mul) + el for (mul, el) in data if int(mul) > 0]
  negative = [str(mul) + el for (mul, el) in data if int(mul) < 0]
  return positive, negative

You might notice that you can simplify this even further—the only thing you use the lists for is to build the strings, so maybe you just want a function that returns a string equation (in which case you can use a generator expression instead of a list comprehension—if you don't know about them yet, ignore that part).

def balance_equation(species,coeff):
  data=zip(coeff,species)
  positive = '+'.join(str(mul) + el for (mul, el) in data if int(mul) > 0)
  negative = '-'.join(str(mul) + el for (mul, el) in data if int(mul) < 0)
  return positive + '=' + negative

Of course now you're returning a string instead of a pair of lists, so you have to change your calling code to just print balance_equation(species, coeff) instead of combining the lists.

One last thing: You seem to reverse the order of the coefficients/multipliers and species/elements at each call. For example:

def balance_equation(species,coeff):
  data=zip(coeff,species)

Unless there's a good reason to do otherwise, it's better to pick one order and be consistent throughout, or you're invariably going to run into a bug where you get them backwards.

Upvotes: 4

Related Questions