繁体   English   中英

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

[英]Inserting list values from a list to another in a specific order in Python

我试图将列表值从一个列表插入另一个列表,但按特定顺序,其中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循环,还是有另一种方式而不是在日期中列出所有内容?

一种更简单的方法是使用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值都会被添加。

对于想要在每个其他元素中拟合日期的具体示例,您可以使用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'不在末尾。 第二行是丑陋的列表理解魔术,以平整列表列表。

您可以使用:

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

双循环是这里的问题; 只使用一个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.'

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

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,你就可以去了。

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

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

实质上, .insert()用于追加到特定位置的列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM