Author Topic: Have a small problem on python.  (Read 4238 times)

0 Members and 1 Guest are viewing this topic.

Offline fangyongxinxi

  • QuantumATK Guru
  • ****
  • Posts: 143
  • Reputation: 0
    • View Profile
Have a small problem on python.
« on: October 8, 2010, 10:00 »
Dear Sir,
I have a small problem on python, for example,
I have a list: a0=[...],a1=[***],....a10=[&&&];
I want to print the whole list like this:
for i in arange ( 0,9,1):
  print ai

How can I get this ?

Thank you.

Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5576
  • Country: dk
  • Reputation: 96
    • View Profile
    • QuantumATK at Synopsys
Re: Have a small problem on python.
« Reply #1 on: October 8, 2010, 10:10 »
It's probably a good idea to take a step back and consider how these lists are defined, and what they contain. You show 11 lists, not one (a0 is a list, and so is a1 etc, in the way you show it). So it's hard to understand what you want to print :)
Make the example a bit more concrete, a1=[***] is not proper Python.

Offline fangyongxinxi

  • QuantumATK Guru
  • ****
  • Posts: 143
  • Reputation: 0
    • View Profile
Re: Have a small problem on python.
« Reply #2 on: October 8, 2010, 12:40 »
ao=[   \
 [-6.17002749e-004, -6.03315307e-004 , 3.58848081e-001],\
 [-8.33706695e-005 ,-8.36845952e-005 , 2.50715526e-001],\
 [-8.90464098e-005 ,-1.05493730e-004 ,-2.50966073e-001],\
 [-5.09569544e-004, -8.42857764e-004 ,-3.58956138e-001],\
]
a1= [ \
 [-6.16174681e-004 ,-6.02638671e-004 , 3.61338929e-001],\
 [-8.30438040e-005 ,-8.34390381e-005 , 2.53054406e-001],\
 [-8.66211312e-005 ,-1.04184193e-004 ,-2.49179985e-001] ,\
 [-4.83007091e-004 ,-8.40519850e-004 ,-3.57777252e-001] ,\
]
a2=[ \
 [-6.15333332e-004 ,-6.01961575e-004 , 3.63840486e-001] ,\
 [-8.28628774e-005 ,-8.33948589e-005 , 2.55395674e-001] ,\
 [-8.57491655e-005 ,-1.04860759e-004 ,-2.47401226e-001] ,\
 [-4.78033324e-004 ,-8.61610364e-004 ,-3.56603951e-001] ,\
]

take a0,a1,a2 (there are many a*, about a80), which come from vnl.file, for example,
I want to get a list like:
a0[0][2]
a1[0][2]
a2[0][2]
a3[0][2]
a4[0][2]
so, I wonder how to write a script like:
    for i in arange(0,4,1):
        print ai[0][2]
to get this list.


Thank you.


Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5576
  • Country: dk
  • Reputation: 96
    • View Profile
    • QuantumATK at Synopsys
Re: Have a small problem on python.
« Reply #3 on: October 8, 2010, 12:47 »
What you are looking for is the exec command. Like:
Code: python
a0 = 1
exec('print a%s'%0)
So, without having tested it carefully,
Code: python
for i in arange(0,4,1):
    exec('print a%s[0][2]'%i)
probably works But the easiest way would still be to not make 80 different a's but put all data in one big numpy array ;)

Offline fangyongxinxi

  • QuantumATK Guru
  • ****
  • Posts: 143
  • Reputation: 0
    • View Profile
Re: Have a small problem on python.
« Reply #4 on: October 9, 2010, 01:57 »
This is exactly what I want. Thank you.