这就是我所拥有的:
item_dictionary = {'10350': 'Third-Age Full', '560':'Death Rune'}
filler = []
for i in range(0,365):
filler.append(None)
filler1=[]
for i in range(0, 2):
filler1.append(None)
filler2=[]
for i in range(0, 1):
filler2.append(None)
names = item_dictionary.values()
names_filled = list(itertools.chain.from_iterable(zip(names, filler1 * len(names))))
#Returns ['Death Rune', None, 'Third-Age Full', None]
和
names = item_dictionary.values()
names1 = list(itertools.chain.from_iterable(zip(names, filler2 * len(names))))
names_filled = list(itertools.chain.from_iterable(zip(names1, filler1 * len(names))))
#Returns ['Death Rune', None, None, None, 'Third-Age Full', None, None, None]
我正试图获得['Death Rune', None, None, 'Third-Age Full', None, None]
,但我似乎无法,有人可以帮助我吗?感谢。
答案 0 :(得分:2)
怎么样:
In [171]: item_dictionary = {'10350': 'Third-Age Full', '560':'Death Rune'}
In [172]: names = item_dictionary.values()
In [173]: [n for name in names for n in [name, None, None]]
Out[173]: ['Death Rune', None, None, 'Third-Age Full', None, None]
或者如果您想轻松将None
的数量更改为其他值,
In [174]: [n for name in names for n in [name] + [None]*2]
Out[174]: ['Death Rune', None, None, 'Third-Age Full', None, None]
答案 1 :(得分:1)
首先,您可以更轻松地创建这些填充物:
filler = [None] * 365
filler1 = [None] * 2
filler2 = [None]
要真正回答你的问题,你可以压缩两个以上的迭代,所以你可以将第三个填充物压缩到它:
>>> list(itertools.chain.from_iterable(zip(names, [None] * len(names), [None] * len(names))))
['Third-Age Full', None, None, 'Death Rune', None, None]
然后你也可以使用itertools.zip_longest
根本不需要你扩展那些填充迭代,但只是自动扩展它们(zip_longest
将使用None
作为其每个默认值为fillvalue):
>>> list(itertools.chain.from_iterable(itertools.zip_longest(names, [], [])))
['Third-Age Full', None, None, 'Death Rune', None, None]
或者,正如unutbu建议的那样,使用列表理解。
答案 2 :(得分:0)
这不是你想做的吗?
names_filled = list(itertools.chain.from_iterable([[n,None,None] for n in names])