如何使用python用0填充列表

时间:2023-02-09 17:41:03

I want to get a fixed length list from another list like:

我想从另一个列表中得到一个固定长度的列表,比如:

a = ['a','b','c']
b = [0,0,0,0,0,0,0,0,0,0]

And I want to get a list like this: ['a','b','c',0,0,0,0,0,0,0]. In other words, if len(a) < len(b), i want to fill up list a with values from list b up to length of the list b, somewhat similar to what str.ljust does.

我想得到这样一个列表:['a' b' c' 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]换句话说,如果len(a) < len(b),我想用list b到list b长度的值填充list a,有点类似于string .ljust所做的。

This is my code:

这是我的代码:

a=['a','b','c']
b = [0 for i in range(5)]
b = [a[i] for i in b if a[i] else i]

print a

But it shows error:

但是它显示错误:

  File "c.py", line 7
    b = [a[i] for i in b if a[i] else i]
                                    ^
SyntaxError: invalid syntax

What can i do?

我能做什么?

9 个解决方案

#1


74  

Why not just:

为什么不直接:

a = a + [0]*(maxLen - len(a))

#2


22  

Use itertools repeat.

出现使用itertools重复。

>>> from itertools import repeat
>>> a + list(repeat(0, 6))
['a', 'b', 'c', 0, 0, 0, 0, 0, 0]

#3


7  

Why not just

为什么不直接

c = (a + b)[:len(b)]

#4


3  

Can't you just do:

你就不能做的:

a = ['a','b','c']
b = [0,0,0,0,0,0,0,0]

c = a + b #= ['a','b','c',0,0,0,0,0,0,0]

#5


2  

a+[0]*(len(b) - len(a))

['a', 'b', 'c', 0, 0, 0, 0, 0, 0, 0]

#6


1  

If you want to fill with mutable values, for example with dicts:

如果您想填充可变值,例如使用dicts:

map(lambda x: {}, [None] * n)

Where n is the number of elements in the array.

其中n是数组中的元素个数。

>>> map(lambda x: {}, [None] * 14)
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
>>> l = map(lambda x: {}, [None] * 14)
>>> l[0]
{}
>>> l[0]['bar'] = 'foo'
>>> l
[{'bar': 'foo'}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

Populating without the lambda would cause every element to be {'bar': 'foo'}!

不填充lambda会导致每个元素都是{'bar': 'foo'}!

#7


1  

To be more explicit, this solution replaces the first elements of b with all of the elements of a regardless of the values in a or b:

更明确地说,这个解决方案将b的第一个元素替换为a的所有元素,而不考虑a或b中的值:

a + b[len(a):]

This will also work with:

这也适用于:

>>> a = ['a', ['b'], None, False]
>>> b = [0, 1, 2, 3, 4, 5]
>>> a + b[len(a):]
['a', ['b'], None, False, 4, 5] 

If you do not want to the result to be longer than b:

如果你不希望结果比b长:

>>> a = ['a', ['b'], None, False]
>>> b = [0, 1, 2]
>>> (a + b[len(a):])[:len(b)]
['a', ['b'], None]

#8


0  

LIST_LENGTH = 10
a = ['a','b','c']

while len(a) < LIST_LENGTH:
    a.append(0)

#9


0  

Just another solution:

另一个解决方案:

a = ['a', 'b', 'c']
maxlen = 10
result = [(0 if i+1 > len(a) else a[i])  for i in range(maxlen)]

I like some of the other solutions better.

我更喜欢其他的解决方案。

#1


74  

Why not just:

为什么不直接:

a = a + [0]*(maxLen - len(a))

#2


22  

Use itertools repeat.

出现使用itertools重复。

>>> from itertools import repeat
>>> a + list(repeat(0, 6))
['a', 'b', 'c', 0, 0, 0, 0, 0, 0]

#3


7  

Why not just

为什么不直接

c = (a + b)[:len(b)]

#4


3  

Can't you just do:

你就不能做的:

a = ['a','b','c']
b = [0,0,0,0,0,0,0,0]

c = a + b #= ['a','b','c',0,0,0,0,0,0,0]

#5


2  

a+[0]*(len(b) - len(a))

['a', 'b', 'c', 0, 0, 0, 0, 0, 0, 0]

#6


1  

If you want to fill with mutable values, for example with dicts:

如果您想填充可变值,例如使用dicts:

map(lambda x: {}, [None] * n)

Where n is the number of elements in the array.

其中n是数组中的元素个数。

>>> map(lambda x: {}, [None] * 14)
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
>>> l = map(lambda x: {}, [None] * 14)
>>> l[0]
{}
>>> l[0]['bar'] = 'foo'
>>> l
[{'bar': 'foo'}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

Populating without the lambda would cause every element to be {'bar': 'foo'}!

不填充lambda会导致每个元素都是{'bar': 'foo'}!

#7


1  

To be more explicit, this solution replaces the first elements of b with all of the elements of a regardless of the values in a or b:

更明确地说,这个解决方案将b的第一个元素替换为a的所有元素,而不考虑a或b中的值:

a + b[len(a):]

This will also work with:

这也适用于:

>>> a = ['a', ['b'], None, False]
>>> b = [0, 1, 2, 3, 4, 5]
>>> a + b[len(a):]
['a', ['b'], None, False, 4, 5] 

If you do not want to the result to be longer than b:

如果你不希望结果比b长:

>>> a = ['a', ['b'], None, False]
>>> b = [0, 1, 2]
>>> (a + b[len(a):])[:len(b)]
['a', ['b'], None]

#8


0  

LIST_LENGTH = 10
a = ['a','b','c']

while len(a) < LIST_LENGTH:
    a.append(0)

#9


0  

Just another solution:

另一个解决方案:

a = ['a', 'b', 'c']
maxlen = 10
result = [(0 if i+1 > len(a) else a[i])  for i in range(maxlen)]

I like some of the other solutions better.

我更喜欢其他的解决方案。