列举列表中的列表 [英] enumerating a list in a list

查看:77
本文介绍了列举列表中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个约会,上面有发生的事件.当我显示日历时,我想枚举该日期的事件列表.

我还需要能够从列表中删除事件.

def command_add(date, event, calendar):
    if date not in calendar:
        calendar[date] = list()
    calendar[date].append(event)


calendar = {}
command_add("2015-10-29", "Python class", calendar)
command_add("2015-10-12", "Eye doctor", calendar)
command_add("2015-10-12", "lunch with sid", calendar)
command_add("2015-10-29", "Change oil in blue car", calendar)
print(calendar)

def command_show(calendar):
    for (date, event) in calendar:
       print(date, enumerate(event))

command_show(calendar)

我认为这将允许我访问该日期下的辅助列表并进行枚举,但出现错误.

示例:

command_show(calendar)
    2015-10-20:
        0: stackover flow sign up
    2015-11-01:
        0: stackoverflow post
        1: banned from stack overflow

解决方案

如果不使用 enumerate() 在这里起作用.从文档中:

返回一个枚举对象. Iterable必须是序列,迭代器或其他支持迭代的对象.
enumerate()返回的迭代器的__next__()方法返回一个元组,该元组包含一个计数(从起始处开始,默认为0)和通过迭代可迭代获得的值.

因此,如果evernt['Python class', 'Eye doctor', 'lunch with sid'],它将返回类似[(0, 'Python class'), (1, 'Eye doctor'), (2, 'lunch with sid')]的内容.

现在我们有了[(0, 'Python class'), (1, 'Eye doctor'), (2, 'lunch with sid')],当我们像for i in enumerate(event)那样在其上使用for循环时,i将在第一个循环中为(0, 'Python class'),在第二个循环中为(1, 'Eye doctor'),依此类推.

然后,如果要打印类似0: Python class的内容(在字符串前面有一些空格),我们需要手动放置类似' '+的空格(例如+可以在此处连接字符串, 'foo'+'bar'foobar).

然后,因为i是一个元组,所以我正在使用 slice . i[0]可以获取该元组中的第一个元素,i[1]可以获取第二个元组,等等.

因为i[0]是一个整数,我们不能只做类似0 + 'foobar'的事情(会提高TypeError: unsupported operand type(s) for +: 'int' and 'str').因此,我们需要使用str()函数将其转换为字符串.然后...也许您会理解.


您还可以执行以下操作:

for num, event in enumerate(event):
    print('    '+str(num), event, sep=': ')

更清楚吗? for num, event in enumerate(event)将在第一个循环中给出类似num = 0, evert = 'Python class'的内容,并且...就像我说的那样.

关于sep,您可以检查该文档更多细节.

I have a date with events that happened on the date. I want to enumerate the list of events on the date when i show the calendar.

Also I need to be able to delete an event from the list.

def command_add(date, event, calendar):
    if date not in calendar:
        calendar[date] = list()
    calendar[date].append(event)


calendar = {}
command_add("2015-10-29", "Python class", calendar)
command_add("2015-10-12", "Eye doctor", calendar)
command_add("2015-10-12", "lunch with sid", calendar)
command_add("2015-10-29", "Change oil in blue car", calendar)
print(calendar)

def command_show(calendar):
    for (date, event) in calendar:
       print(date, enumerate(event))

command_show(calendar)

I thought that this would let me access the secondary list under the date and enumerate it but I get an error.

Example out:

command_show(calendar)
    2015-10-20:
        0: stackover flow sign up
    2015-11-01:
        0: stackoverflow post
        1: banned from stack overflow

解决方案

Just change your command_show() function to this, if you don't use dict.items() then you will only get the keys(not both keys and values):

def command_show(calendar):
    for (date, event) in calendar.items():
        print(date+':')
        for i in enumerate(event):
            print('    '+str(i[0])+': '+i[1])

Output:

2015-10-29:
    0: Python class
    1: Change oil in blue car
2015-10-12:
    0: Eye doctor
    1: lunch with sid


About why am I doing this:

for i in enumerate(event):
    print('    '+str(i[0])+': '+i[1])

As you can see, I'm using enumerate() function here. From the document:

Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration.
The __next__() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.

So it will return something like [(0, 'Python class'), (1, 'Eye doctor'), (2, 'lunch with sid')] if the evernt is ['Python class', 'Eye doctor', 'lunch with sid'].

Now we have [(0, 'Python class'), (1, 'Eye doctor'), (2, 'lunch with sid')], when we use for loop on it like for i in enumerate(event), i will be (0, 'Python class') at the first loop, and (1, 'Eye doctor') at the second loop, etc.

And then, if you want to print something like 0: Python class(there is some spaces in front of the sting), we need manually put the spaces like ' '+(+ can join strings here, for example, 'foo'+'bar' is foobar).

Then, because i is a tuple, I'm using slice. i[0] can get the first element in that tuple, i[1] can get the second, etc.

Because i[0] is a integer, and we can't just do something like 0 + 'foobar'(will raise TypeError: unsupported operand type(s) for +: 'int' and 'str'). So we need use str() function to covert it to string. And then...maybe you'll understand.


Also you can do something like:

for num, event in enumerate(event):
    print('    '+str(num), event, sep=': ')

More clear? for num, event in enumerate(event) will give something like num = 0, evert = 'Python class' at the first loop, and...as I said.

About sep, you could check the document for more details.

这篇关于列举列表中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆