Search Results

Search found 58 results on 3 pages for 'user248237'.

Page 3/3 | < Previous Page | 1 2 3 

  • converting python list of strings to their type

    - by user248237
    given a list of python strings, how can I automatically convert them to their correct type? Meaning, if I have: ["hello", "3", "3.64", "-1"] I'd like this to be converted to the list ["hello", 3, 3.64, -1] where the first element is a stirng, the second an int, the third a float and the fourth an int. how can I do this? thanks.

    Read the article

  • averaging matrix efficiently

    - by user248237
    in Python, given an n x p matrix, e.g. 4 x 4, how can I return a matrix that's 4 x 2 that simply averages the first two columns and the last two columns for all 4 rows of the matrix? e.g. given: a = array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) return a matrix that has the average of a[:, 0] and a[:, 1] and the average of a[:, 2] and a[:, 3]. I want this to work for an arbitrary matrix of n x p assuming that the number of columns I am averaging of n is obviously evenly divisible by n. let me clarify: for each row, I want to take the average of the first two columns, then the average of the last two columns. So it would be: 1 + 2 / 2, 3 + 4 / 2 <- row 1 of new matrix 5 + 6 / 2, 7 + 8 / 2 <- row 2 of new matrix, etc. which should yield a 4 by 2 matrix rather than 4 x 4. thanks.

    Read the article

  • making errorbars not clipped in matplotlib with Python

    - by user248237
    I am using matplotlib in Python to plot a line with errorbars as follows: plt.errorbar(xvalues, up_densities, yerr=ctl_sds, fmt='-^', lw=1.2, markersize=markersize, markeredgecolor=up_color, color=up_color, label="My label", clip_on=False) plt.xticks(xvalues) I set the ticks on the x-axis using "xticks". However, the error bars of the last point in xvalues (i.e. xvalues[-1]) are clipped on the right -- meaning only half an error bar appears. This is true even with the clip_on=False option. How can I fix this, so that the error bars appear in full, even though their right side is technically outside xvalues[-1]? thanks.

    Read the article

  • getting smallest of coordinates that differ by N or more in Python

    - by user248237
    suppose I have a list of coordinates: data = [[(10, 20), (100, 120), (0, 5), (50, 60)], [(13, 20), (300, 400), (100, 120), (51, 62)]] and I want to take all tuples that either appear in each list in data, or any tuple that differs from all tuples in lists other than its own by 3 or less. How can I do this efficiently in Python? For the above example, the results should be: [[(100, 120), # since it occurs in both lists (10, 20), (13, 20), # since they differ by only 3 (50, 60), (51, 60)]] (0, 5) and (300, 400) would not be included, since they don't appear in both lists and are not different from elements in lists other than their own by 3 or less. how can this be computed? thanks.

    Read the article

  • Selecting dictionary items by key efficiently in Python

    - by user248237
    suppose I have a dictionary whose keys are strings. How can I efficiently make a new dictionary from that which contains only the keys present in some list? for example: # a dictionary mapping strings to stuff mydict = {'quux': ..., 'bar': ..., 'foo': ...} # list of keys to be selected from mydict keys_to_select = ['foo', 'bar', ...] The way I came up with is: filtered_mydict = [mydict[k] for k in mydict.keys() \ if k in keys_to_select] but I think this is highly inefficient because: (1) it requires enumerating the keys with keys(), (2) it requires looking up k in keys_to_select each time. at least one of these can be avoided, I would think. any ideas? I can use scipy/numpy too if needed.

    Read the article

  • finding elements in python association lists efficiently

    - by user248237
    I have a set of lists that look like this: conditions = [ ["condition1", ["sample1", "sample2", "sample3"]], ["condition2", ["sample4", "sample5", "sample6"], ...] how can I do the following things efficiently and elegantly in Python? Find all the elements in a certain condition? e.g. get all the samples in condition2. Right now I can do: for cond in conditions: cond_name, samples = cond if cond_name == requested_cond: return samples but that's clunky. Find the ordered union of a list of conditions? E.g. ordered_union(["condition1", "condition2"], conditions) should return: ["sample1", "sample2", "sample3", "sample4", "sample5", "sample6"] How can I do this efficiently in Python? There are probably clever one liners?

    Read the article

  • Building up an array in numpy/scipy by iteration in Python?

    - by user248237
    Often, I am building an array by iterating through some data, e.g.: my_array = [] for n in range(1000): # do operation, get value my_array.append(value) # cast to array my_array = array(my_array) I find that I have to first build a list and then cast it (using "array") to an array. Is there a way around these? all these casting calls clutter the code... how can I iteratively build up "my_array", with it being an array from the start? thanks.

    Read the article

  • speeding up parsing of files

    - by user248237
    the following function parses a CSV file into a list of dictionaries, where each element in the list is a dictionary where the values are indexed by the header of the file (assumed to be the first line.) this function is very very slow, taking ~6 seconds for a file that's relatively small (less than 30,000 lines.) how can I speed it up? def csv2dictlist_raw(filename, delimiter='\t'): f = open(filename) header_line = f.readline().strip() header_fields = header_line.split(delimiter) dictlist = [] # convert data to list of dictionaries for line in f: values = map(tryEval, line.strip().split(delimiter)) dictline = dict(zip(header_fields, values)) dictlist.append(dictline) return (dictlist, header_fields) thanks.

    Read the article

< Previous Page | 1 2 3