简体   繁体   English

如何从嵌套在特定元素之间的列表中提取元素,并将它们添加到新列表中?

[英]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. Look for the first 1 in the input string在输入字符串中查找第一个1
  2. Add the index to a list将索引添加到列表
  3. Look for the next 0 in the string.在字符串中查找下一个0
  4. Append one less than found index to a list.将比找到的索引少的一个附加到列表中。 (Or maybe append the index from steps 2 and 4 as a pair?) (或者可以将第 2 步和第 4 步中的索引作为一对附加?)
  5. Continue by looking for the next 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.groupbyoperator.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.

相关问题 如何使用python检测列表中的不同元素并从中创建新列表(查找) - How to detect different elements in list and create new lists from them (finding) with python 如何有效地修改列表列表的所有元素并将其添加到现有列表列表中 - How to efficiently modify all elements of a list of lists and add them to the existing list of lists 如何从现有列表列表中创建第一个元素的新列表 - How to make a new list of first elements from existing list of lists 如何将列表中的元素逐个附加到数据框中的嵌套列表中 - How to append elements from a list into nested lists in a dataframe one by one 如何从具有嵌套列表作为元素的列表中选择 n 个最小的数字 - How to pick n smallest numbers from a list with nested lists as elements 从嵌套列表中的列表中按类型删除元素 - Remove Elements by Type from Lists in a Nested List 如何从两个列表中删除特定独立元素的某些元素? - how to remove the elements of some elements of particular indies from both the lists? 如何提取两个不同嵌套列表中相同元素的子列表信息并存储? - How to extract sub-list information of identical elements in two different nested lists and storing them? 如何从 Python 的列表中添加特定元素 - How to add specific elements from list of lists in Python 从连续元素之间的差异小于特定数字的列表创建嵌套列表 - create a nested list from a list where where the difference between consecutive elements is less than a particular number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM