如何在列表中拆分字符串列表?

时间:2018-12-05 15:29:26

标签: python list

让我们来

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

以及我要寻找的结果:

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

4 个答案:

答案 0 :(得分:5)

要完全在控制台中打印输出,请遍历外部列表并使用enumeratestr.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)

答案 1 :(得分:1)

假设a,b,c ...是整数

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

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

print(B)

如果这些是字符串(问题不太清楚),则只需使用%s代替%d

答案 2 :(得分:0)

其他选项:

ru_word_func
space_func
en_word_func
pnct_func

它返回:

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)

答案 3 :(得分:0)

尝试这个:

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

您应该具有:

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