parsed.org

Tips by tag: data

Flatten Lists by Hawk-McKain on Sep 02, 2007 04:17 AM

Flattens a list of any depth:

def flatten(seq, a = []):
    """flatten(seq, a = []) -> list

    Return a flat version of the iterator `seq` appended to `a`
    """

    if hasattr(seq, "__iter__"):  # Can `seq` be iterated over?
        for item in seq:          # If so then iterate over `seq`
            flatten(item, a)      # and make the same check on each item.
    else:                         # If seq isn't an iterator then
        a.append(seq)             # append it to the new list.
    return a
dataflattenlistspython
Loading Data From File by xinu on Dec 31, 2005 04:06 PM

Consider the following schema:

CREATE TABLE loadtest (
    pkey int(11) NOT NULL auto_increment,
    name varchar(20),
    exam int,
    score int,
    time_enter timestamp(14),
    PRIMARY KEY (pkey),
);

And the data you need to load:

'name22999990',2,94
'name22999991',3,93
'name22999992',0,91

Running this query would load the data into the columns name, exam, score:

mysql> LOAD DATA INFILE '/tmp/out.txt' INTO TABLE loadtest
    -> FIELDS TERMINATED BY ',' (name,exam,score);

** Note: This tip borrowed from the Linux Gazette. You can read the complete article at http://www.linuxgazette.com/node/9059.

dataloadmysqlqueriesschemasql
Remove Duplicates by xinu on Jan 13, 2005 08:50 AM

Remove duplicate elements from a list:

newList = dict([(item, 1) for item in oldList]).keys()

Or, if you have Python 2.3 or newer, you can use a Set object to collapse your list:

import sets
newList = list(sets.Set(oldList))
dataimportlanguagesprogrammingpythonsetsyntax
RSS