thaking
thaking

Reputation: 3635

Python "list order"

I have 2 troubles with my data, can anyone help me:

How can I get from this:

1.

k=[['1','7', 'U1'],
 ['1.5', '8', 'U1'],
 ['2', '5.5', 'U1']]

get this

1,7,U1
1.5,8,U1
2,5.5,U1

EDIT 2 I MAKE SOME CHANGE ON second case:, still searching solution for this one:

2. And how to get, from this

l=array([[[ 4.24231542], 'U1'],
       [[ 3.41424819], 'U1'],
       [[ 2.17214734], 'U1'],], dtype=object)

get

4.24231542,U1
3.41424819,U1
2.17214734,U1

Thanks

Upvotes: 0

Views: 601

Answers (8)

riza
riza

Reputation: 17144

Using map:

k = [['1','7', 'U1'], ['1.5', '8', 'U1'], ['2', '5.5', 'U1']]

map(','.join, k) # returns ['1,7,U1', '1.5,8,U1', '2,5.5,U1']

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208565

Here is a single solution that will work for both 1 and 2:

k = [['1', '7', 'U1'], ['1.5', '8', 'U1'], ['2', '5.5', 'U1']]
l = [[[4.2423154199999997], 'U1'], [[3.4142481899999999], 'U1'], [[2.17214734], 'U1']]

make_string = lambda x: ",".join(map(make_string, x)) if isinstance(x, list) else str(x)

print "\n".join(map(make_string, k))
print "\n".join(map(make_string, l))

Upvotes: 1

Hyperboreus
Hyperboreus

Reputation: 126

for a in k: print ",".join (a)

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 114035

I'm going to venture a guess as to what you actually mean in terms of syntax

For the first one:

k=[['1','7', 'U1'],
 ['1.5', '8', 'U1'],
 ['2', '5.5', 'U1']]

for l in k:
    print ', '.join(l)

For the second one:

for l in k:
    print l[0][0], l[1]

Hope this helps

Upvotes: 1

Boaz Yaniv
Boaz Yaniv

Reputation: 6424

One-line functional style:

print '\n'.join(','.join(x) for x in k)

Upvotes: 5

Fred Foo
Fred Foo

Reputation: 363767

1)

for x in k:
    print(','.join(x))

2)

for x,y in l:
    print('%.8f,%s' % (x[0], y))

Upvotes: 4

Santa
Santa

Reputation: 11545

>>> print k
[['1', '7', 'U1'], ['1.5', '8', 'U1'], ['2', '5.5', 'U1']]
>>> for a, b, c in k:
...     print '%s,%s,%s' % (a, b, c)
...
1,7,U1
1.5,8,U1
2,5.5,U1

Now, you try! (for the second one)

Upvotes: 1

Hyperboreus
Hyperboreus

Reputation: 126

for a,b,c in k: print "%s,%s,%s" % (a,b,c)

for a,b in l: print "%s,%s" % (a[0],b)

Upvotes: 1

Related Questions