Best form of iterating over a lsit of lists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Best form of iterating over a lsit of lists (/thread-5098.html) Pages:
1
2
|
RE: Best form of iterating over a lsit of lists - buran - Oct-15-2017 eventually you can use named tupe from collections import namedtuple Pet = namedtuple('Pet', ['name', 'animal', 'weight']) pets = (Pet('Garfield', 'cat', 4), Pet('Pluto', 'dog', 10)) for mypet in pets: print(mypet) print('{} is a {} and its weight is {}'.format(*mypet)) print(mypet.name, mypet.animal, mypet.weight)
RE: Best form of iterating over a lsit of lists - snippsat - Oct-15-2017 Use the first one in a loop,the all will work the same. for name, species, weight in pets: print(name, species, weight)The name of the this stuff is called tuple assignment and tuple unpacking .>>> info = ('julia', 'borger', '27') # The normal way >>> name, surname, birth_year = info >>> name 'julia' >>> surname 'borger' >>> birth_year '27' # Can do this,but not so common (name, surname, birth_year) = info # Can do this,whaaat? [name, surname, birth_year] = info RE: Best form of iterating over a lsit of lists - ichabod801 - Oct-15-2017 There's no real difference. You might be wasting a little time with the latter two, and they look a little odd, but there's no real difference. RE: Best form of iterating over a lsit of lists - tonymcgregor - Oct-15-2017 I've done what I should probably have done in the first place; read the spec. I think I basically understand now but htere's one thing still bothering me. There is a difference between () and [] on the LHS of an assignment but it's quite subtle. (a) = is the same as a = [a] = requires an iterable on the RHS (with just one value)So (a) = 2 is valid but [a] = 2 is not.I'm guessing this use of parenthesis is to be consistent with the rules for creating tuples and lists. In (1,2) the parentheses don't make this a tuple, the comma does. The parentheses are, actually (redundant) "ordering" parentheses as in (2 + 3) . On the LHS they do the same thing. E.G. a,(b,) = 1, 2 #invalid a,(b,) = 1, 2, #invalid a,(b,) = 1, (2,) #OK a,(b,) = 1, (2) #invalid a, b = 1, 2, #OK a, b, = 1, 2 #OKBut, what I still don't get is why allow [] on the RHS at all. a, requires and iterable on the RHS just like [a] . I.E.:(a) = 1, #a is a tuple. [b] = 2, #b is an integer #but so is c in c, = 1,I can't see a need for [] on the LHS so it seems odd to allow them. Especially given the confusion with lists and tuples. RE: Best form of iterating over a lsit of lists - ichabod801 - Oct-15-2017 Maybe [] are allowed on the LHS for slice assignment? some_list[2:5] = range(4)
RE: Best form of iterating over a lsit of lists - tonymcgregor - Oct-15-2017 You're right, my statement "I can't see a need for [] on the LHS" was to broad. I do see their use in a slice assignment. But still not in assignment to a target list. |