List question in Python
Alright, so I have a list of 296 data points and four blank spaces. I cannot edit this list of data points. i have another list of 300 data points. I want to multiply the lists together, with python skipping multiplying the data points when a blank space shows up. Here's what the lists look like:
a = [[6], [7], [], [7]]
b = [[100], [200],开发者_高级运维 [300], [400]]
What sort of exception handling would I have to put in? My current code uses
for items in mathList:
try:
sumlist = [x * y for x,y in zip(grearp, rex)]
except:
print 'No data for',items
Is the length of both lists actually 300 then, with 0's or blank strings for the missing data points? If so, this should come close:
newList = [x[0] * y[0] if x else None for x, y in zip(l1, l2)]
-- Edited --
I realized I didn't review the sample data quite as well as I could've. As the inner list is empty it'll fail a truth test on its own, so just if x. Also, added indexing for the inner lists on x, y.
Think that you can also use something like this(code below will prepare lists to be the same size and then calculates '*' if both values present - otherwise include the only value exist):
from itertools import izip_longest
a = [[6], [7], [], [7]]
b = [[100], [200], [300], [400]]
newList = [[x[0] * y[0]] if x and y else (x or y) for x,y in izip_longest(a,b, fillvalue=[])]
加载中,请稍侯......
精彩评论