在Python中以特定顺序将列表值从列表插入到另一个列表中

时间:2018-01-07 14:02:18

标签: python list

我试图将列表值从一个列表插入到另一个列表中,但是按照特定顺序,其中date [0]输入了文本[1],日期[1]输入了文本[3],依此类推。

dates=['21/11/2044', '31/12/2018', '23/9/3000', '25/12/2007']

text=['What are dates? ', ', is an example.\n', ', is another format as
well.\n', ', also exists, but is a bit ludicrous\n', ', are examples but more commonly used']

我试过这个方法:

for j in range(len(text)):
  for i in range(len(dates)):
   text.insert(int((j*2)+1), dates[i])

这是结果,这是不正确的:

['What are dates? ', '25/12/2007', '23/9/3000', '25/12/2007', '23/9/3000',
'25/12/2007', '23/9/3000', '25/12/2007', '23/9/3000', '25/12/2007',
'23/9/3000', '31/12/2018', '21/11/2044', '31/12/2018', '21/11/2044',
'31/12/2018', '21/11/2044', '31/12/2018', '21/11/2044', '31/12/2018',
'21/11/2044', ', is an example.\n', ', is another format as well.\n', ',
also exists, but is a bit ludicrous\n', ', are examples but more commonly used']

我试图找回一个如下所示的列表:

['What are dates? ','21/11/2044', 'is an example.\n','31/12/2018', ', is
another format as well.\n','23/9/3000', ', also exists, but is a bit
ludicrous\n', '25/12/2007',', are examples but more commonly used']

有没有办法按照我想要的方式将日期[i]插入文本[2 * j + 1]?我是否应该使用for循环,或者是否有其他方式而不在日期中列出所有内容?

6 个答案:

答案 0 :(得分:4)

实现这一目标的一种更简单的方法是在Python 3.x中使用itertools.zip_longest(或在Python 2.x中使用izip_longest):

>>> from itertools import zip_longest # for Python 3.x

>>> # For Python 2.x
>>> # from itertools import izip_longest

>>> dates=['21/11/2044', '31/12/2018', '23/9/3000', '25/12/2007']
>>> text=['What are dates? ', ', is an example.\n', ', is another format as well.\n', ', also exists, but is a bit ludicrous\n', ', are examples but more commonly used']

>>> [w for x in zip_longest(text, dates, fillvalue='') for w in x if w]
['What are dates? ', '21/11/2044', ', is an example.\n', '31/12/2018', ', is another format as well.\n', '23/9/3000', ', also exists, but is a bit ludicrous\n', '25/12/2007', ', are examples but more commonly used']

您的代码存在的问题是您有嵌套的for循环,这就是为什么j的每个索引都会添加dates的所有值。

答案 1 :(得分:1)

对于您希望在其他所有元素中拟合日期的具体示例,您可以使用zip

parts = zip(['a', 'b', 'c', 'd'], ['d1', 'd2', 'd3'])
text = [x for y in parts for x in y]
# ['a', 'd1', 'b', 'd2', 'c', 'd3']

您可能需要使用itertools.izip_longest和/或在列表之间处理不相等的长度,否则您会看到如上所述的结果,其中'd'不在最后。第二行是丑陋的列表理解魔术,以压缩列表列表。

答案 2 :(得分:0)

您可以使用:

result = [ ]
for x in enumerate(dates, text):
   result.append(dates[x]).append(text[x])

答案 3 :(得分:0)

双循环是这里的问题;只需使用单个for循环,如

for i in range(len(dates)):
    text.insert(int((i*2)+1), dates[i])

应该这样做。

但是,如果您计划将第二个列表作为字符串结束,则使用.format可能更简单,如

text = 'Date with one format is {} and date with another format is {}.'.format(*dates)

会给你

'Date with one format is 21/11/2044 and date with another format is 31/12/2018.'

答案 4 :(得分:0)

也许它不是最优雅的版本,但这可以作为例如:

from itertools import zip_longest


dates=['21/11/2044', '31/12/2018', '23/9/3000', '25/12/2007']

text=['What are dates? ', ', is an example.\n', ', is another format as well.\n', ', also exists, but is a bit ludicrous\n', ', are examples but more commonly used']


l3 = list()
for i, j in zip_longest(text, dates, fillvalue=None):
    if i is not None:
        l3.append(i)
    if j is not None:
        l3.append(j)

print(l3)
来自标准itertools模块的

zip_longest将2个不均匀长度的列表拉在一起,并用“fillvalue”填充缺失值。如果你只丢弃那个fillvalue,你就可以去了。

答案 5 :(得分:0)

一个非常容易理解的简单解决方案

counter = 0
while counter < len(dates):
    text.insert(counter + counter + 1, dates[counter])
    counter += 1

基本上,.insert()用于附加到特定位置的列表。