Python lists

M

Manatee

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.
 
R

Roy Smith

Manatee said:
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?
 
M

Manatee

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:]
 
J

Joel Goldstick

Manatee said:
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
 
A

Alex

Manatee said:
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)]
 
H

Hans Mulder

Manatee said:
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
 
E

Evan Driscoll

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
 
H

Hans Mulder

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top