Lists (Python): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
(Nieuwe pagina aangemaakt met ' Een ''list'' is hetzelfde als een ''array''. == Voorbeeld Wikibooks == [https://en.wikibooks.org/wiki/Python_Programming/Lists]: <pre> list1 = []...') |
|||
(6 tussenliggende versies door dezelfde gebruiker niet weergegeven) | |||
Regel 1: | Regel 1: | ||
+ | Een ''list'' is een geordende groep ''items'' of ''elementen''. De elementen in een list kunnen van verschillende types zijn. Lists kunnen cijfers, string en andere lists bevatten. | ||
− | + | == Voorbeelden == | |
− | + | Gebaseerd op [https://en.wikibooks.org/wiki/Python_Programming/Lists]: | |
− | |||
− | [https://en.wikibooks.org/wiki/Python_Programming/Lists]: | ||
<pre> | <pre> | ||
list1 = [] # A new empty list | list1 = [] # A new empty list | ||
list2 = [1, 2, 3, "cat"] # A new non-empty list with mixed item types | list2 = [1, 2, 3, "cat"] # A new non-empty list with mixed item types | ||
− | list1.append("cat") # Add a single member, at the end of the list | + | print(list2[0]) # Index begint bij 0 |
− | list1.extend(["dog", "mouse"]) # Add several members | + | print(list2[4]) # IndexError: list index out of range |
− | list1.insert(0, "fly") # Insert at the beginning | + | list1.append("cat") # Add a single member, at the end of the list - Ronde haakjes |
− | list1[0:0] = ["cow", "doe"] # Add members at the beginning | + | list1.extend(["dog", "mouse"]) # Add several members - Vierkante + ronde haakjes |
+ | list1.insert(0, "fly") # Insert at the beginning - Rest schuift op | ||
+ | list1[0:0] = ["cow", "doe"] # Add members at the beginning - Rest schuift op - Blijkbaar zorgt ":0" voor invoegen ipv. vervangen??? | ||
+ | list1[0] = ["cow", "doe"] # Item 1 is nu vervangen door een twee-dimensionale lijst | ||
doe = list1.pop(1) # Remove item at index | doe = list1.pop(1) # Remove item at index | ||
if "cat" in list1: # Membership test | if "cat" in list1: # Membership test | ||
list1.remove("cat") # Remove AKA delete | list1.remove("cat") # Remove AKA delete | ||
#list1.remove("elephant") - throws an error | #list1.remove("elephant") - throws an error | ||
− | for item in list1: # | + | |
− | print item | + | for item in list1: # Iterate over items |
+ | print(item) | ||
+ | |||
print "Item count:", len(list1) # Length AKA size AKA item count | print "Item count:", len(list1) # Length AKA size AKA item count | ||
list3 = [6, 7, 8, 9] | list3 = [6, 7, 8, 9] | ||
Regel 46: | Regel 50: | ||
</pre> | </pre> | ||
− | == | + | == Bronnen == |
− | + | * https://en.wikibooks.org/wiki/Python_Programming/Lists | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Huidige versie van 4 aug 2019 om 11:40
Een list is een geordende groep items of elementen. De elementen in een list kunnen van verschillende types zijn. Lists kunnen cijfers, string en andere lists bevatten.
Voorbeelden
Gebaseerd op [1]:
list1 = [] # A new empty list list2 = [1, 2, 3, "cat"] # A new non-empty list with mixed item types print(list2[0]) # Index begint bij 0 print(list2[4]) # IndexError: list index out of range list1.append("cat") # Add a single member, at the end of the list - Ronde haakjes list1.extend(["dog", "mouse"]) # Add several members - Vierkante + ronde haakjes list1.insert(0, "fly") # Insert at the beginning - Rest schuift op list1[0:0] = ["cow", "doe"] # Add members at the beginning - Rest schuift op - Blijkbaar zorgt ":0" voor invoegen ipv. vervangen??? list1[0] = ["cow", "doe"] # Item 1 is nu vervangen door een twee-dimensionale lijst doe = list1.pop(1) # Remove item at index if "cat" in list1: # Membership test list1.remove("cat") # Remove AKA delete #list1.remove("elephant") - throws an error for item in list1: # Iterate over items print(item) print "Item count:", len(list1) # Length AKA size AKA item count list3 = [6, 7, 8, 9] for i in range(0, len(list3)): # Read-write iteration AKA for each item list3[i] += 1 # Item access AKA element access by index last = list3[-1] # Last item nextToLast = list3[-2] # Next-to-last item isempty = len(list3) == 0 # Test for emptiness set1 = set(["cat", "dog"]) # Initialize set from a list list4 = list(set1) # Get a list from a set list5 = list4[:] # A shallow list copy list4equal5 = list4==list5 # True: same by value list4refEqual5 = list4 is list5 # False: not same by reference list6 = list4[:] del list6[:] # Clear AKA empty AKA erase list7 = [1, 2] + [2, 3, 4] # Concatenation print list1, list2, list3, list4, list5, list6, list7 print list4equal5, list4refEqual5 print list3[1:3], list3[1:], list3[:2] # Slices print max(list3 ), min(list3 ), sum(list3) # Aggregates print [x for x in range(10)] # List comprehension print [x for x in range(10) if x % 2 == 1] print [x for x in range(10) if x % 2 == 1 if x < 5] print [x + 1 for x in range(10) if x % 2 == 1] print [x + y for x in '123' for y in 'abc']