Python中数组数组中字符串的拆分值

时间:2021-01-18 21:41:03

I've an arrays of arrays called arr and I want to split the second value of each array. This means, if I've 0-6 I would like to modify it to '0','6'

我有一个名为arr的数组数组,我想分割每个数组的第二个值。这意味着,如果我是0-6,我想将其修改为'0','6'

What I have:

是)我有的:

arr = [['PROVEEDOR', '0-6', '11111'], ['tiempo ida pulidor', '6-14', '33333']]

What I would like to have:

我想要的是:

arr = [['PROVEEDOR', '0', '6', '11111'], ['tiempo ida pulidor', '6', '14', '33333']]

How can I do this conversion? It's always the second value, and always have two numbers. I know I've to use .split('-') but I know dont know to make it work here to make the replacement, as I've to iterate between all the arrays included in arr.

我该怎么做这个转换?它始终是第二个值,并且总是有两个数字。我知道我要使用.split(' - '),但我知道不知道让它在这里进行替换,因为我要在arr中包含的所有数组之间进行迭代。

Thanks in advance.

提前致谢。

5 个解决方案

#1


12  

If you want to do it inplace:

如果你想在现场进行:

In [83]: arr = [['PROVEEDOR', '0-6', '11111'], ['tiempo ida pulidor', '6-14', '33333']]

In [84]: for i in arr:
    ...:     i[1:2]=i[1].split('-')

In [85]: arr
Out[85]: [['PROVEEDOR', '0', '6', '11111'], ['tiempo ida pulidor', '6', '14', '33333']]

#2


2  

try this

arr = [['PROVEEDOR', '0-6', '11111'], ['tiempo ida pulidor', '6-14', '33333']]

new_arr = []
for x in arr:
    new_val = [x[0]]
    new_val.extend(x[1].split('-'))
    new_val.append(x[2])
    new_arr.append(new_val)

print new_arr

#3


2  

Try this:

def splitfirst(a):
    a[1:2] = a[1].split('-')
    return a
newarr = [splitfirst(a) for a in arr]

What is going on? Well, you can assign to a slice, replacing that portion of the list with a new sequence. That is what the line

到底是怎么回事?好吧,您可以分配给切片,用新序列替换列表的那部分。这就是行

a[1:2] = [1, 2, 3, ...]

does. It replaces the slice 1:2 (element with index 1 up to but not including element with index 2) with the new sequence - the result of our splitting in this case.

确实。它将切片1:2(索引为1但不包括索引2的元素的元素)替换为新序列 - 在这种情况下我们拆分的结果。

Since this solution relies on assigning to a slice, which is a statement, we can't do this without a separate function. Wait. I'm going to go and see if I can find something...

由于此解决方案依赖于分配给切片(这是一个语句),因此如果没有单独的函数,我们就无法做到这一点。等待。我要去看看能不能找到的东西......

EDIT: Onliner for those who like that:

编辑:对于那些喜欢这样的人的在线人:

[a.__setslice__(1, 2, a[1].split('-')) or a for a in arr]

What is going on here? Well... actually exactly the same as before, but using the magic method __setslice__ instead of the syntactic sugar of slice assignment. I use the or a part of the expression to produce the arr element, since __setslice__ returns None.

这里发生了什么?嗯......实际上和以前完全一样,但是使用魔术方法__setslice__而不是切片赋值的语法糖。我使用表达式的一部分或一部分来生成arr元素,因为__setslice__返回None。

#4


1  

for a in arr:
   elems = a.pop(1).split('-')
   a.insert(1, elems[0])
   a.insert(2, elems[1])

#5


0  

Without changing original array (with copy):

无需更改原始数组(带副本):

result = [[ar[0]] + ar[1].split('-') + ar[2:] for ar in arr]

In-place solution:

for ar in arr:
    x,y = ar[1].split('-')
    ar[1] = x
    ar.insert(2, y)

#1


12  

If you want to do it inplace:

如果你想在现场进行:

In [83]: arr = [['PROVEEDOR', '0-6', '11111'], ['tiempo ida pulidor', '6-14', '33333']]

In [84]: for i in arr:
    ...:     i[1:2]=i[1].split('-')

In [85]: arr
Out[85]: [['PROVEEDOR', '0', '6', '11111'], ['tiempo ida pulidor', '6', '14', '33333']]

#2


2  

try this

arr = [['PROVEEDOR', '0-6', '11111'], ['tiempo ida pulidor', '6-14', '33333']]

new_arr = []
for x in arr:
    new_val = [x[0]]
    new_val.extend(x[1].split('-'))
    new_val.append(x[2])
    new_arr.append(new_val)

print new_arr

#3


2  

Try this:

def splitfirst(a):
    a[1:2] = a[1].split('-')
    return a
newarr = [splitfirst(a) for a in arr]

What is going on? Well, you can assign to a slice, replacing that portion of the list with a new sequence. That is what the line

到底是怎么回事?好吧,您可以分配给切片,用新序列替换列表的那部分。这就是行

a[1:2] = [1, 2, 3, ...]

does. It replaces the slice 1:2 (element with index 1 up to but not including element with index 2) with the new sequence - the result of our splitting in this case.

确实。它将切片1:2(索引为1但不包括索引2的元素的元素)替换为新序列 - 在这种情况下我们拆分的结果。

Since this solution relies on assigning to a slice, which is a statement, we can't do this without a separate function. Wait. I'm going to go and see if I can find something...

由于此解决方案依赖于分配给切片(这是一个语句),因此如果没有单独的函数,我们就无法做到这一点。等待。我要去看看能不能找到的东西......

EDIT: Onliner for those who like that:

编辑:对于那些喜欢这样的人的在线人:

[a.__setslice__(1, 2, a[1].split('-')) or a for a in arr]

What is going on here? Well... actually exactly the same as before, but using the magic method __setslice__ instead of the syntactic sugar of slice assignment. I use the or a part of the expression to produce the arr element, since __setslice__ returns None.

这里发生了什么?嗯......实际上和以前完全一样,但是使用魔术方法__setslice__而不是切片赋值的语法糖。我使用表达式的一部分或一部分来生成arr元素,因为__setslice__返回None。

#4


1  

for a in arr:
   elems = a.pop(1).split('-')
   a.insert(1, elems[0])
   a.insert(2, elems[1])

#5


0  

Without changing original array (with copy):

无需更改原始数组(带副本):

result = [[ar[0]] + ar[1].split('-') + ar[2:] for ar in arr]

In-place solution:

for ar in arr:
    x,y = ar[1].split('-')
    ar[1] = x
    ar.insert(2, y)