如何根据优先级升序排列待办事项列表

     2023-03-27     163

关键词:

【中文标题】如何根据优先级升序排列待办事项列表【英文标题】:How to ascendingly order a to do list based on priority 【发布时间】:2022-01-21 11:06:56 【问题描述】:

因此,作为实习机会的一部分,我们被告知要制作一个待办事项列表 CLI 应用。问题是我不知道如何按优先级升序对文件中的文本进行排序,任何帮助将不胜感激

这是我的代码:

import typer 
app = typer.Typer()

@app.command()
def help():
    print("""Usage :-
$ ./todo add 2 hello world  # Add a new item with priority 2 and text "hello world" to the list
$ ./todo ls               # Show incomplete priortiy list items sorted by priority in ascending order
$ ./todo del NUMBER       # Delete the incomplete item with the given priority number
$ ./todo done NUMBER      # Mark the incomplete item with given PRIORITY_NUMBER as complete
$ ./todo help             # Show usage
$ ./todo report           # Statistics """)


@app.command()
def add(priority: int,task: str):

    file1=open("ls.txt","r")
    x=len(file1.readlines())
    y=x+1
    file1.close()
    file1=open("ls.txt","a")
    file1.write(str(y)+". "+task + " ["+str(priority)+"]"+ "\n")
    file1.close()


@app.command()
def ls():
    file1=open("ls.txt","r")
    print(file1.read())
   
        
if __name__=="__main__":
    app()
       

这是所需的输出image:

【问题讨论】:

请以文本而非图像的形式提供输出。 【参考方案1】:

我看不到图片,但我猜你想显示按优先级排序的任务:

from pathlib import Path

import typer

app = typer.Typer()
DB_NAME = "ls.txt"


@app.command()
def help():
    HELP_TEXT = """Usage :-
$ ./todo add 2 hello world  # Add a new item with priority 2 and text "hello world" to the list
$ ./todo ls               # Show incomplete priortiy list items sorted by priority in ascending order
$ ./todo del NUMBER       # Delete the incomplete item with the given priority number
$ ./todo done NUMBER      # Mark the incomplete item with given PRIORITY_NUMBER as complete
$ ./todo help             # Show usage
$ ./todo report           # Statistics """
    print(HELP_TEXT)


@app.command()
def add(priority: int, task: str):
    filepath = Path(DB_NAME)
    text = filepath.read_text()
    y = len(text.splitlines()) + 1
    with filepath.open("a") as fp:
        fp.write(f"y. task [priority]\n")


@app.command()
def ls():
    ss = Path(DB_NAME).read_text().splitlines()
    tasks = []
    for line in ss:
        index, tp = line.split(".", 1)
        task, priority = tp.strip("]").rsplit(" [", 1)
        tasks.append((int(priority), int(index), task.strip()))
    for priority, _, task in sorted(tasks):
        print(priority, task)


if __name__ == "__main__":
    app()

【讨论】:

如何使列表视图构建器在颤振中成为可重新排序的列表视图(优先任务应用程序(待办事项应用程序))

】如何使列表视图构建器在颤振中成为可重新排序的列表视图(优先任务应用程序(待办事项应用程序))【英文标题】:howcanmakelistviewbuildertoareorderablelistviewinflutter(aprioritytaskapp(todoapp))【发布时间】:2021-09-1620:26:52【问题描述... 查看详情

如何根据优先级重新排列列表视图中的项目?

】如何根据优先级重新排列列表视图中的项目?【英文标题】:Howtorearrangeitemsinalistviewaccordingtotheirpriority?【发布时间】:2021-10-0502:18:24【问题描述】:我正在开发一个任务应用程序,我正在尝试设置一个列表视图。现在,我有... 查看详情

C++ 待办事项列表重复输入。如何避免这种情况?

】C++待办事项列表重复输入。如何避免这种情况?【英文标题】:C++To-DoListrepeatsinputs.Howtoavoidthat?【发布时间】:2018-08-1600:36:10【问题描述】:我有以下代码,我正在尝试创建一个非常简单(不是那么简单)的待办事项列表。期... 查看详情

如何从 laravel 的列表中删除待办事项列表项?

】如何从laravel的列表中删除待办事项列表项?【英文标题】:HowcanIdeleteatodolistitemfromthelistinlaravel?【发布时间】:2019-10-3105:11:56【问题描述】:我正在尝试删除laravel中的一个项目。这是我的代码:这是我的路线:Route::resource(\'\'... 查看详情

如何让 splice() 方法在我的待办事项列表中工作?

】如何让splice()方法在我的待办事项列表中工作?【英文标题】:HowdoIgetthesplice()methodtoworkinmytodolist?【发布时间】:2022-01-1612:04:18【问题描述】:我正在构建一个简单的待办事项列表,并将使用拼接方法从列表中删除项目。我无... 查看详情

如何使用 JsPdf 下载完整的待办事项 (vue.js) 列表?

】如何使用JsPdf下载完整的待办事项(vue.js)列表?【英文标题】:Howtodownloadthefullto-do(vue.js)listusingJsPdf?【发布时间】:2022-01-2121:25:54【问题描述】:我是这个领域的初学者。尝试使用vue.js做一个小项目。我想为用户提供一个选项... 查看详情

ios - 如何在 iPad 中创建待办事项列表 [关闭]

】ios-如何在iPad中创建待办事项列表[关闭]【英文标题】:ios-howtocreatetodolistiniPad[closed]【发布时间】:2013-02-1410:07:15【问题描述】:我想创建一个应用程序来编写/查看我的to-do列表和与联系人的约会。有谁知道要使用哪个库以及... 查看详情

颤振强制列表视图构建器根据条件仅显示 1 个项目

】颤振强制列表视图构建器根据条件仅显示1个项目【英文标题】:flutterForcelistviewbuildertoshowonly1itembasedoncondition【发布时间】:2022-01-1814:07:16【问题描述】:我正在使用hive制作一个待办事项项目,我想在不同的行中显示标记为已... 查看详情

如何使用javascript制作待办事项列表(代码片段)

...礼品直接跳到末尾去评论区领书在本文中,您将学习如何使用JavaScript创建待办事项列表。TodoListJavaScript 查看详情

如何将列添加到 Azure 演示 iOS 应用程序的简易表(待办事项列表)

】如何将列添加到Azure演示iOS应用程序的简易表(待办事项列表)【英文标题】:HowtoAddColumnstoEasyTableforAzureDemoiOSApp(TodoList)【发布时间】:2018-04-0303:26:19【问题描述】:这是我第一次使用Azure,我只是想制作一个演示iOS应用程序... 查看详情

NSDocument 用于关系数据/待办事项列表?

】NSDocument用于关系数据/待办事项列表?【英文标题】:NSDocumentforrelationaldata/todolist?【发布时间】:2015-01-1023:16:51【问题描述】:我想创建一个OSX应用程序,用户可以在其中编辑类似于待办事项列表的项目-每个项目都有文本、... 查看详情

无法从 Listview 中删除项目(待办事项列表),长按没有任何反应

...它将从列表(以及数据库)中删除。但我做不到,不明白如何删除(我是新手)。我还可以在s 查看详情

textlocalstorage待办事项列表资源(代码片段)

查看详情

markdown片段待办事项列表(代码片段)

查看详情

Drupal 视图和待办事项列表

】Drupal视图和待办事项列表【英文标题】:Drupalviews&todolist【发布时间】:2010-10-3108:35:45【问题描述】:想知道是否有人可以告诉我在视图中是否可以进行以下操作。我已经安装了待办事项模块-http://drupal.org/project/to_do然后我... 查看详情

使用 localStorage 的待办事项列表

】使用localStorage的待办事项列表【英文标题】:To-DoListwithlocalStorage【发布时间】:2019-06-0213:23:53【问题描述】:我做了一个简单的待办事项清单。但我想让一个列表元素有一个text-decoration=line-through当我单击按钮时,列表项将设... 查看详情

scrum敏捷软件开发都有哪些要素?

...作是对团队交付的价值负责,他的职责是定义需求、需求优先级、需求验收标准,以及产品发布内容与日期。角色2:敏捷教练敏捷教练的核心工作是帮助团队熟悉和掌握Scrum框架,持续改进,又好又快地开展工作。角色3:研发... 查看详情

JavaScript/HTML 中的待办事项列表

】JavaScript/HTML中的待办事项列表【英文标题】:ToDoListinJavaScript/HTML【发布时间】:2014-12-2319:54:49【问题描述】:我已经设置了这个待办事项列表,几乎所有的工作都很完美。但是,如果我删除列表中的所有内容。它不会让我添... 查看详情