更改列列列scala

时间:2018-05-30 19:14:34

标签: scala

我有以下列表,我想更改列的顺序

import re s = '(((1+0)+1)+1)'

def getContectWithinBraces( x , *args , **kwargs):
    ptn = r'[%(left)s]([^%(left)s%(right)s]*)[%(right)s]' %kwargs
    Res = []
    res = re.findall(ptn , x)
    while res != []:
        Res = Res + res
        xx = x.replace('(%s)' %Res[-1] , '%s')
        res = re.findall(ptn, xx)
        print(res)
        if res != []:
            res[0] = res[0] %('(%s)' %Res[-1])
    return Res

getContectWithinBraces(s , left='\(\[\{' , right = '\)\]\}')

预期结果是:

def randbitlist(n=10):
    n_on = random.randint(0, n)
    n_off = n - n_on
    result = [1]*n_on + [0]*n_off
    random.shuffle(result)
    return result

最好的问候

1 个答案:

答案 0 :(得分:1)

如果您正在寻找将第3列与第4列交换,

  • ,
  • 分开
  • 使用交换列构建新列表
  • concat列表获取字符串

例如,

scala> val list = List("banana,QS,1,0,0", "apple,EE,1,2,1", "peas,US,1,4,4")
list: List[String] = List(banana,QS,1,0,0, apple,EE,1,2,1, peas,US,1,4,4)

scala> list.map(_.split(",")).map(elem => List(elem(0), elem(1), elem(3), elem(2), elem(4)).mkString(","))
res0: List[String] = List(banana,QS,0,1,0, apple,EE,2,1,1, peas,US,4,1,4)