Thursday, October 5, 2017

Adventures in Python: List Manipulation

Adventures in Python: List Manipulation

This program creates two lists, one from 0 to 11, the other from 12 to 23.  Lists are enclosed by square brackets ( [ ] ).  Here is a short demo script.

# Program 004:  List manipluation

# Start with a range of 0 to 11
list1 = list(range(12))
# Let a second list range from 12 to 23
list2 = list(range(12,24))
print(list1)
print(list2)

# reverse the order
# use sorted in Python 3
print("Reverse List Elements")
list3 = sorted(list1, reverse=True)
list4 = sorted(list2, reverse=True)
print(list3)
print(list4)

# combine two (or more) lists
print("Combine lists with + ")
list5 = list1 + list2
print(list5)


# print the first two elements of each elements
print("First two elements of each list")
for k in range (0,2):
    a = list1[k]
    b = list2[k]
    print([a,b])


# sums, minimum, and maximum of each list
print("Sum, minimum, and maximum of each list")
print("Sum : ",sum(list1),sum(list2))
print("Minimum : ",min(list1),min(list2))
print("Maximum : ",max(list1),max(list2))


Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
Reverse List Elements
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12]
Combine lists with +
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
First two elements of each list
[0, 12]
[1, 13]
Sum, minimum, and maximum of each list
Sum :  66 210
Minimum :  0 12
Maximum :  11 23


Coming up, combinations and permutations, random numbers, and plotting. 

Eddie


This blog is property of Edward Shore, 2017

TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...