Lists (Python): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
Regel 48: | Regel 48: | ||
print [x + 1 for x in range(10) if x % 2 == 1] | print [x + 1 for x in range(10) if x % 2 == 1] | ||
print [x + y for x in '123' for y in 'abc'] | print [x + y for x in '123' for y in 'abc'] | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</pre> | </pre> | ||
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']