Python lists

Discussion in 'Python' started by Manatee, Dec 28, 2012.

  1. Manatee

    Manatee Guest

    I read in this:
    ['C100, C117', 'X7R 0.033uF 10% 25V 0603', '0603-C_L, 0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', '']

    Then I need to convert it to this:
    [['C100', 'C117'], 'X7R 0.033uF 10% 25V 0603', '0603-C_L', '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', '']]

    The objectinve is too make the first instance a list of lists and have component values enclosed in a list. Then I will print out the components, each on its own line, followed by the description; for instance.
    C100, 'X7R 0.033uF 10% 25V 0603', '0603-C_L', '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', ''
    c117, 'X7R 0.033uF 10% 25V 0603', '0603-C_L', '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', ''

    The file is read in from a comma delimited file. I can't seem to work this out.
    Manatee, Dec 28, 2012
    #1
    1. Advertising

  2. Manatee

    Roy Smith Guest

    In article <>,
    Manatee <> wrote:

    > I read in this:
    > ['C100, C117', 'X7R 0.033uF 10% 25V 0603', '0603-C_L, 0603-C_N', '10', '2',
    > '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key',
    > '490-1521-1-ND', '']
    >
    > Then I need to convert it to this:
    > [['C100', 'C117'], 'X7R 0.033uF 10% 25V 0603', '0603-C_L', '0603-C_N', '10',
    > '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key',
    > '490-1521-1-ND', '']]


    You want to convert it into a syntax error???

    You've got an extra ']' in there somewhere. I'm guessing it's the one
    at the end, but that's just a guess. Could you clarify what you meant?
    Roy Smith, Dec 28, 2012
    #2
    1. Advertising

  3. Manatee

    Manatee Guest

    On Friday, December 28, 2012 9:14:57 AM UTC-5, Manatee wrote:
    > I read in this:
    >
    > ['C100, C117', 'X7R 0.033uF 10% 25V 0603', '0603-C_L, 0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', '']
    >
    >
    >
    > Then I need to convert it to this:
    >
    > [['C100', 'C117'], 'X7R 0.033uF 10% 25V 0603', '0603-C_L', '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', '']]
    >
    >
    >
    > The objectinve is too make the first instance a list of lists and have component values enclosed in a list. Then I will print out the components, each on its own line, followed by the description; for instance.
    >
    > C100, 'X7R 0.033uF 10% 25V 0603', '0603-C_L', '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', ''
    >
    > c117, 'X7R 0.033uF 10% 25V 0603', '0603-C_L', '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', ''
    >
    >
    >
    > The file is read in from a comma delimited file. I can't seem to work this out.


    Forgot to list my code, here is my code:

    import csv
    list = []
    t = [0]
    with open('38366 Rev 6_12_12_2012_0602pm.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
    repeat_info = row[1:]
    for ref_des in row[0]:
    t[0] = ref_des
    print t + row[1:]
    Manatee, Dec 28, 2012
    #3
  4. On Fri, Dec 28, 2012 at 9:19 AM, Roy Smith <> wrote:

    > In article <>,
    > Manatee <> wrote:
    >
    > > I read in this:
    > > ['C100, C117', 'X7R 0.033uF 10% 25V 0603', '0603-C_L, 0603-C_N', '10',

    > '2',
    > > '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key',
    > > '490-1521-1-ND', '']
    > >
    > > Then I need to convert it to this:
    > > [['C100', 'C117'], 'X7R 0.033uF 10% 25V 0603', '0603-C_L', '0603-C_N',

    > '10',
    > > '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D', 'Digi-Key',
    > > '490-1521-1-ND', '']]

    >
    > You want to convert it into a syntax error???
    >
    > You've got an extra ']' in there somewhere. I'm guessing it's the one
    > at the end, but that's just a guess. Could you clarify what you meant?
    >


    If Roy was right, and that last ] is a type then I would do this:

    L = ['C100, C117', 'X7R 0.033uF 10% 25V 0603', '0603-C_L, 0603-C_N',
    '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D',
    'Digi-Key', '490-1521-1-ND', '']

    new_l = L[1:] # this removes the 'C100,C117' item from the original list

    first_item = L[:1] # this is 'C100, C117'

    >From here you can split first_item into two strings, and create your final

    list with the pieces

    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Joel Goldstick
    Joel Goldstick, Dec 28, 2012
    #4
  5. Manatee

    Alex Guest

    Manatee wrote:

    > On Friday, December 28, 2012 9:14:57 AM UTC-5, Manatee wrote:
    > > I read in this:
    > >
    > > ['C100, C117', 'X7R 0.033uF 10% 25V 0603', '0603-C_L, 0603-C_N',
    > > '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D',
    > > 'Digi-Key', '490-1521-1-ND', '']
    > >
    > >
    > >
    > > Then I need to convert it to this:
    > >
    > > [['C100', 'C117'], 'X7R 0.033uF 10% 25V 0603', '0603-C_L',
    > > '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA',
    > > 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', '']]
    > >


    l = ['C100, C117', 'X7R 0.033uF 10% 25V 0603', '0603-C_L, 0603-C_N',
    '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D',
    'Digi-Key', '490-1521-1-ND', '']

    [item.split(',') if i == 0 else item for i, item in enumerate(l)]
    Alex, Dec 28, 2012
    #5
  6. Manatee

    Hans Mulder Guest

    On 28/12/12 18:46:45, Alex wrote:
    > Manatee wrote:
    >
    >> On Friday, December 28, 2012 9:14:57 AM UTC-5, Manatee wrote:
    >>> I read in this:
    >>>
    >>> ['C100, C117', 'X7R 0.033uF 10% 25V 0603', '0603-C_L, 0603-C_N',
    >>> '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D',
    >>> 'Digi-Key', '490-1521-1-ND', '']
    >>>
    >>>
    >>>
    >>> Then I need to convert it to this:
    >>>
    >>> [['C100', 'C117'], 'X7R 0.033uF 10% 25V 0603', '0603-C_L',
    >>> '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA',
    >>> 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', '']]
    >>>

    >
    > l = ['C100, C117', 'X7R 0.033uF 10% 25V 0603', '0603-C_L, 0603-C_N',
    > '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D',
    > 'Digi-Key', '490-1521-1-ND', '']
    >
    > [item.split(',') if i == 0 else item for i, item in enumerate(l)]


    If it's okay to modify the original list, you can simply do:

    l[0] = split(l[0], ", ")

    If modifying the original is not okay, the simple solution would
    be to copy it first:

    l2 = l
    l2[0] = split(l2[0], ", ")


    Hope this helps,

    -- HansM
    Hans Mulder, Dec 30, 2012
    #6
  7. Re: Re: Python lists

    On 12/30/2012 4:19 PM, Hans Mulder wrote:
    > If it's okay to modify the original list, you can simply do:
    >
    > l[0] = split(l[0], ", ")
    >
    > If modifying the original is not okay, the simple solution would
    > be to copy it first:
    >
    > l2 = l
    > l2[0] = split(l2[0], ", ")


    Um, that doesn't copy the list:

    >>> l = ["C100, C117", "X7R ..."]
    >>> l2 = l
    >>> import string
    >>> l2[0] = string.split(l2[0], ", ")
    >>> l

    [['C100', 'C117'], 'X7R ...']
    >>> l2

    [['C100', 'C117'], 'X7R ...']

    To make a copy of a list you can either use slice syntax (l2 = l[:]) or
    call the list constructor (l2 = list(l)).


    Evan


    >
    >
    > Hope this helps,
    >
    > -- HansM
    >
    >
    Evan Driscoll, Dec 30, 2012
    #7
  8. Manatee

    Hans Mulder Guest

    On 30/12/12 23:25:39, Evan Driscoll wrote:
    > On 12/30/2012 4:19 PM, Hans Mulder wrote:
    >> If it's okay to modify the original list, you can simply do:
    >>
    >> l[0] = split(l[0], ", ")
    >>
    >> If modifying the original is not okay, the simple solution would
    >> be to copy it first:
    >>
    >> l2 = l
    >> l2[0] = split(l2[0], ", ")

    >
    > Um, that doesn't copy the list:


    Oops, you're right.


    >>>> l = ["C100, C117", "X7R ..."]
    >>>> l2 = l
    >>>> import string
    >>>> l2[0] = string.split(l2[0], ", ")
    >>>> l

    > [['C100', 'C117'], 'X7R ...']
    >>>> l2

    > [['C100', 'C117'], 'X7R ...']
    >
    > To make a copy of a list you can either use slice syntax (l2 = l[:]) or
    > call the list constructor (l2 = list(l)).


    Thanks for correcting me,

    -- HansM
    Hans Mulder, Dec 30, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    317
    JustSomeGuy
    Jun 17, 2004
  2. Jon Slaughter

    lists of lists

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    4
    Views:
    416
    Buster
    Dec 13, 2004
  3. Charlotte Henkle

    Counter for items in lists in lists?

    Charlotte Henkle, Sep 25, 2004, in forum: Python
    Replies:
    8
    Views:
    397
    Charlotte Henkle
    Sep 26, 2004
  4. robin
    Replies:
    10
    Views:
    541
    Dave Hansen
    Apr 12, 2006
  5. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    398
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
Loading...

Share This Page