[英]How to pull elements from a list that are nested between particular elements, and add them to new lists?
I have a daypart column (str), which has 1s or 0s for each hour of the day, depending if we choose to run a campaign during that hour.我有一个时段列 (str),一天中的每个小时都有 1 或 0,具体取决于我们是否选择在该小时内运行广告系列。
Example:例子:
daypart = '110011100111111100011110'
I want to convert this to the following string format:我想将其转换为以下字符串格式:
'0-1, 4-6, 9-15, 19-22'
The above format is more readable, and shows during which hours the campaign ran.上述格式更具可读性,并显示了活动运行的时间。
Here's what I'm doing:这是我在做什么:
hours_list = []
ind = 0
for x in daypart:
if int(x) == 1:
hours_list.append(ind)
else:
hours_list.append('exclude')
ind += 1
The above gives me a list like this:上面给了我一个这样的列表:
[0, 1, 'exclude', 'exclude', 4, 5, 6, 'exclude', 'exclude', 9, 10, 11, 12, 13, 14, 15, 'exclude', 'exclude', 'exclude', 19, 20, 21, 22, 'exclude']
Now I want to find a way to make the above into my desired output.现在我想找到一种方法将上述内容变成我想要的输出。 What I am thinking of doing is finding which elements exist between 'exclude', and start adding them to new lists.我想做的是找出“排除”之间存在哪些元素,然后开始将它们添加到新列表中。 I can then take the smallest and largest element from each list, join them with a '-', and append all such lists together.然后我可以从每个列表中取出最小和最大的元素,用“-”将它们连接起来,然后将所有这些列表附加在一起。
Any ideas how I can do this, or a simpler way to do all of this?任何想法我怎么能做到这一点,或者更简单的方法来做到这一点?
Here's simple, readable code to get all intervals:这是获取所有间隔的简单易读代码:
daypart = '1111111111111111111111'
hours= []
start, end = -1, -1
for i in range(len(daypart)):
if daypart[i] == "1":
if end != -1:
end += 1
else:
start = i
end = i
else:
if end!=-1:
hours.append([start, end])
start, end = -1,-1
if end!=-1:
hours.append([start, end])
start, end = -1,-1
print(hours)
I suggest that you convert directly to your desired format rather than using an intermediate representation that has the exact same information as the original input.我建议您直接转换为您想要的格式,而不是使用与原始输入具有完全相同信息的中间表示。 Let's think about how we can do this in words:让我们想想如何用文字来做到这一点:
1
in the input string在输入字符串中查找第一个1
0
in the string.在字符串中查找下一个0
。1
and repeat steps 2-4.继续寻找下一个1
并重复步骤 2-4。I leave translating this into code as an exercise for the reader.我将把它翻译成代码作为读者的练习。
This can be done using itertools.groupby
, operator.itemgetter
, enumerate
in a comprehension to achieve this as well:这可以使用itertools.groupby
, operator.itemgetter
,在理解中enumerate
来实现:
from itertools import groupby
from operator import itemgetter
daypart = '110011100111111100011110'
get_ends, get_one = itemgetter(0,-1), itemgetter(1)
output = ', '.join('{0[0]}-{1[0]}'.format(*get_ends(list(g))) for k,g in groupby(enumerate(daypart), get_one) if k=='1')
print(output)
0-1, 4-6, 9-15, 19-22
get_ends
gets the first and last elements in each group and get_one
just gets element 1
so to use it as a key. get_ends
获取每个组中的第一个和最后一个元素,而get_one
只获取元素1
以便将其用作键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.