简体   繁体   English

如何从一个列表中删除某些内容并将其添加到 python 中的另一个列表中

[英]How to remove something from one list and add it to another in python

I have a nested list that has certain elements with an [x] at the beginning.我有一个嵌套列表,其中某些元素的开头带有 [x]。 I want the function to remove those elements and move them to the last list in list1 (at index 2).我希望 function 删除这些元素并将它们移动到list1中的最后一个列表(在索引 2 处)。 But it should remove the [x] from it before placing it in the last list.但它应该在将其放入最后一个列表之前从中删除 [x] 。 It should also count how many were removed from each list.它还应该计算从每个列表中删除了多少。

For example:例如:

list1 = [['[x]homework', '[x]eat','stretch'], ['[x]final', 'school'], ['sleep','midterm']

# After:

list1 = [['stretch'], ['school'], ['sleep','midterm', 'homework', 'eat', 'final']]

# Output:
# 2 removed from 1st list
# 1 removed from 2nd list

This is how you would do that:这就是你要这样做的方式:

list1 = [['[x]homework', '[x]eat','stretch'], ['[x]final', 'school'], ['sleep','midterm']]
for x in range(0,len(list1)-1):
    lst = list1[x]
    count =  0
    to_be_removed = []
    for str in lst:
        if str[0:3] == "[x]":
            to_be_removed.append(str)
            list1[-1].append(str[3:])
            count += 1
    for str in to_be_removed:
        lst.remove(str)
    print(f"{count} items were removed from list {x+1}" )
print(list1)
list1 = [['[x]homework', '[x]eat','stretch'], ['[x]final', 'school'], ['sleep','midterm']]
new_list = [list() for i in range(len(list1))]
new_list[2] = list1[2]

items = 0
for i in range(len(list1)-1):
    for j in list1[i]:
        if "[x]" in j:
            new_list[2].append(j.replace("[x]", ""))
            items += 1
        else:
            new_list[i].append(j)

print(list1, new_list, items)

I think you have a data structure problem.我认为你有一个数据结构问题。 Your code shouldn't match user input, you should get away from that as soon as possible, and separate display and storage.你的代码不应该匹配用户输入,你应该尽快摆脱它,并将显示和存储分开。 That being said, you can take a step towards those changes by still discarding the silliness asap:话虽这么说,您仍然可以通过尽快放弃愚蠢来朝着这些变化迈出一步:

Example of running:运行示例:

$ python code.py
[['[x]homework', '[x]eat', 'stretch'], ['[x]final', 'school'], ['sleep', 'midterm']]
changed count is 0
[['stretch'], ['school'], ['sleep', 'midterm', 'homework', 'eat', 'final']]

with source code like below源代码如下

def main():
    list1 = [['[x]homework', '[x]eat','stretch'], ['[x]final', 'school'], ['sleep','midterm']]
    print(list1)
    count, list2 = process(list1)
    print(f'changed count is {count}')
    print(list2)
         
def parse(silly_item):
    if silly_item.startswith('[x]'):
        finished = True
        raw_item = silly_item[3:]
    else:
        finished = False
        raw_item = silly_item
    return finished, raw_item

def process(silly_data_format):
    todos, done = silly_data_format[:-1], silly_data_format[-1]
    count = 0
    new_todos = []
    new_done = done[:]
    for todo in todos:
        new_todo = []
        for silly_item in todo:
            finished, raw_item = parse(silly_item)
            target = new_done if finished else new_todo
            target.append(raw_item)
        new_todos.append(new_todo)
    new_silly_data_format = new_todos + [new_done]
    return count, new_silly_data_format

if __name__ == '__main__':
    main()

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

相关问题 如何从列表中删除随机元素并将其添加到python中的另一个列表中 - How to remove a random element from a list and add it to another list in python 如何在Python3中将元素从一个列表添加到另一个列表? - How to add the elements from one list to another list in Python3? Python,如何将元素从一个列表随机添加/追加到另一个列表? - Python, how to randomly add/append elements from one list to another? 如何将元素从一个类添加到另一个类列表,python - How to add elements from one class to another class list, python 如何从用户输入python制成的列表中删除内容 - How to remove something from a list that is made from user input python 如何在python中将一个列表添加到另一个列表? - how to ADD a one list to another in python? 如何在 Python 中将一个列表中的整数添加到另一个列表中 - How to add the integers in one list to another in Python Python:如何连续删除列表的最小值并将其添加到另一个列表? - Python: How to continuously remove the minimum of a list and add it to another list? 如何在Python中删除字典中列表中的内容 - How to Remove Something in a List in a Dictionary in Python Python:为每一行从另一个列表中删除出现在一个列表中的字符串 - Python: Remove string that occurs in one list from another for every row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM