Thịnh Hưng
Thịnh Hưng

Reputation: 21

How to split string list in list?

Let's take:

my_list=[["a","b","c"],["d","e","f"],["g","h","i"],["j","k","l"]]

And the result I'm looking for:

0. a    _ b    (c)
1. d    _ e    (f)
2. g    _ h    (j)
3. j    _ k    (l)

Upvotes: 2

Views: 104

Answers (4)

Kian
Kian

Reputation: 1350

Try this one:

print '\n'.join(str(n) +'. '+e[0] + ' _'+ e[1] + '  ('+e[2]+')' for n,e in enumerate(my_list))

You should have:

0. a _b  (c)
1. d _e  (f)
2. g _h  (i)
3. j _k  (l)

Upvotes: 0

iGian
iGian

Reputation: 11203

Other option:

lst = [['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l']]

lines = [x[0]+'_'+x[1]+' ('+x[2]+')' for x in lst]
for i, line in enumerate(lines):
  print( str(i+1) + '. ' + line)

It returns:

# 1. a_b (c)
# 2. d_e (f)
# 3. g_h (i)
# 4. j_k (l)

Upvotes: 0

Vince
Vince

Reputation: 336

Assuming a,b,c... are integers

A = [[8, 7, 2], [1, 4, 12], [6, 5, 4]]

B = "\n".join(["%d_%d(%d)" % tuple(a) for a in A])

print(B)

if these are strings (not very clear in question), just use %s instead of %d

Upvotes: 1

Graipher
Graipher

Reputation: 7206

To get exactly your output printed in the console, iterate over the outer list and use enumerate and str.format:

values = [["a","b","c"],["d","e","f"],["g","h","i"],["j","k","l"]]

for i, x in enumerate(values):
    print("{}. {}    _ {}    ({})".format(i, *x))
# 0. a    _ b    (c)
# 1. d    _ e    (f)
# 2. g    _ h    (i)
# 3. j    _ k    (l)

Upvotes: 5

Related Questions