Related
I have a list:
[[6, 8, 5, 1, 3, 2, 9, 4, 7],
[7, 3, 4, 5, 9, 8, 2, 1, 6],
[2, 1, 9, 7, 6, 4, 8, 5, 3],
[9, 2, 6, 8, 7, 1, 5, 3, 4],
[8, 5, 1, 3, 4, 9, 6, 7, 2],
[4, 7, 3, 2, 5, 6, 1, 8, 9],
[5, 6, 8, 4, 2, 7, 3, 9, 1],
[3, 4, 2, 9, 1, 5, 7, 6, 8],
[1, 9, 7, 6, 8, 3, 4, 2, 5]]
I want to update a position in this list
for example:
update_list(position: tuple[int, int], value: Optional[int])
where value is the element that is going to replace the original element in the list
Thus update_list((1, 1), 5) should replace 3 with 5
What is the best way to code for this?
You can directly index into the inner list!
>>> content = [[6, 8, 5, 1, 3, 2, 9, 4, 7],
... [7, 3, 4, 5, 9, 8, 2, 1, 6],
... [2, 1, 9, 7, 6, 4, 8, 5, 3],
... [9, 2, 6, 8, 7, 1, 5, 3, 4],
... [8, 5, 1, 3, 4, 9, 6, 7, 2],
... [4, 7, 3, 2, 5, 6, 1, 8, 9],
... [5, 6, 8, 4, 2, 7, 3, 9, 1],
... [3, 4, 2, 9, 1, 5, 7, 6, 8],
... [1, 9, 7, 6, 8, 3, 4, 2, 5]]
>>> content[1][1]
3
>>> content[1][1] = 5
>>> content[1][1]
5
>>> content
[[6, 8, 5, 1, 3, 2, 9, 4, 7], [7, 5, 4, 5, 9, 8, 2, 1, 6], [2, 1, 9, 7, 6, 4, 8, 5, 3], [9, 2, 6, 8, 7, 1, 5, 3, 4], [8, 5, 1, 3, 4, 9, 6, 7, 2], [4, 7, 3, 2, 5, 6, 1, 8, 9], [5, 6, 8, 4, 2, 7, 3, 9, 1], [3, 4, 2, 9, 1, 5, 7, 6, 8], [1, 9, 7, 6, 8, 3, 4, 2, 5]]
This one works for me:
content = [[6, 8, 5],[1, 2, 3]]
def update_list(a, b):
content[a[0]][a[1]] = b
update_list((0,0),25) #It replace the first element by 25
The Output of content:
[[25, 8, 5], [1, 2, 3]]
So, I was learning about Bubble Sort and saw this code:
def bubblesort(list_a):
indexing_length = len(list_a) - 1
sorted = False
while not sorted:
sorted = True
for i in range(0, indexing_length):
if list_a[i] > list_a[i+1]:
sorted = False
list_a[i], list_a[i+1] = list_a[i+1], list_a[i] # flip position
return list_a
I tried running this code on my notebook so I could understand this better. I tried 5, 2, 3, 7, 10, 1, 8 and the process was like this:
--> 2, 5, 3, 7, 10, 1, 8
--> 2, 3, 5, 7, 10, 1, 8
--> 2, 3, 5, 7, 1, 10, 8
--> 2, 3, 5, 7, 1, 8, 10
I ended up with unsorted array because I thought for loop does only one iteration. Am I understanding something wrong? Could anyone explain it to me little bit easier please?
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. This sorting algorithm also known as Brute Force Approach, the reason is that during sorting each element of list will compare with the every other element of the same list and if the compared number are not in order so it will swap the position. Lets have a look on your example list [5, 2, 3, 7, 10, 1, 8].
1st pass: compare 0th index(5) and 1st index(2) and compare it
if 0th index(5) > 1st index(2) then it will swap the position else no swap counter will increase. [5, 2, 3, 7, 10, 1, 8] condition True(5 > 2), SWAP [2, 5, 3, 7, 10, 1, 8], then again compare [2, 5, 3, 7, 10, 1, 8] condition True(5 > 3), SWAP [2, 3, 5, 7, 10, 1, 8], and so on.
[2, 3, 5, 7, 10, 1, 8] True(10>1).
[2, 3, 5, 7, 1, 10, 8] True(10>8).
[2, 3, 5, 7, 1, 8, 10]
2nd pass:
[2, 3, 5, 7, 1, 8, 10]
[2, 3, 5, 7, 1, 8, 10]
[2, 3, 5, 7, 1, 8, 10] True(7>1).
[2, 3, 5, 1, 7, 8, 10]
[2, 3, 5, 1, 7, 8, 10]
3rd pass:
[2, 3, 5, 1, 7, 8, 10]
[2, 3, 5, 1, 7, 8, 10] True(5>1).
[2, 3, 1, 5, 7, 8, 10]
[2, 3, 1, 5, 7, 8, 10]
4th pass:
[2, 3, 1, 5, 7, 8, 10] True(3>1).
[2, 1, 3, 5, 7, 8, 10]
[2, 1, 3, 5, 7, 8, 10]
5th pass:
[2, 1, 3, 5, 7, 8, 10] True(2>1).
[1, 2, 3, 5, 7, 8, 10]
[1, 2, 3, 5, 7, 8, 10]
6th pass:
[1, 2, 3, 5, 7, 8, 10]
7th pass:
[1, 2, 3, 5, 7, 8, 10]
def bubbleSort(arr):
n = len(arr)
for i in range(n):
print(arr[i])
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
print(arr[j], arr[j+1])
arr[j], arr[j + 1] = arr[j + 1], arr[j]
print(arr)
arr = [5, 2, 3, 7, 10, 1, 8]
bubbleSort(arr)
print("Sorted array")
print(arr)
The list will be [2, 3, 5, 7, 1, 8, 10] after the for loop completes the first time, but the value of sorted will be False. Since you're still inside the while not sorted loop, everything inside that loop will run again, including another for loop starting at index 0.
This will keep going until the list is sorted when the for loop completes, since that will make the while loop's condition False.
I try to randomly shuffle the list in a for loop and then append it to another list. Expect to produce 5 lists in different orders, but the results are all in the same order. The code and output are as follows:
list_data = []
for i in range(10):
list_data.append(i)
print(list_data)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
random_list = []
for j in range(5):
np.random.shuffle(list_data)
random_list.append(list_data)
print(list_data)
print(random_list)
# [6, 4, 5, 7, 8, 2, 0, 1, 3, 9]
# [2, 9, 4, 3, 5, 0, 7, 1, 6, 8]
# [3, 0, 9, 1, 5, 7, 8, 6, 4, 2]
# [6, 1, 7, 2, 0, 4, 9, 8, 5, 3]
# [3, 2, 5, 9, 8, 4, 6, 7, 1, 0]
# [[3, 2, 5, 9, 8, 4, 6, 7, 1, 0], [3, 2, 5, 9, 8, 4, 6, 7, 1, 0], [3, 2, 5, 9, 8, 4, 6, 7, 1, 0], [3, 2, 5, 9, 8, 4, 6, 7, 1, 0], [3, 2, 5, 9, 8, 4, 6, 7, 1, 0]]
Although you already have an answer I'd like to help you shorten it into 3 lines of code, and you don't need numpy either. This can be achieved by list comprehension. This code below does what you want.
Note: I used random.sample instead of numpy.random.shuffle, because that shuffles the list inplace and returns None.
import random
list_data = [i for i in range(10)]
random_list = [random.sample(list_data, len(list_data)) for j in range(5)]
If I have a list where all values are unique, the code runs fine. However, if there is a duplicate value within the list, when finding the minimum value for the next iteration, it pulls from the entire list rather than just the remainder of the list.
for n in range(0,len(lst)):
a = min(lst[n:]) #minimum value within remainder of set
i = lst.index(a) #index value for minimum value within remainder of set
temp = lst[n]
lst[n] = a
lst[i] = temp
Results look like this:
lst = [6, 8, 9, 1, 3, 4, 7, 5, 4]
[1, 8, 9, 6, 3, 4, 7, 5, 4]
[1, 3, 9, 6, 8, 4, 7, 5, 4]
[1, 3, 4, 6, 8, 9, 7, 5, 4]
[1, 3, 6, 4, 8, 9, 7, 5, 4]
[1, 3, 6, 8, 4, 9, 7, 5, 4]
[1, 3, 6, 8, 9, 4, 7, 5, 4]
[1, 3, 6, 8, 9, 7, 4, 5, 4]
[1, 3, 6, 8, 9, 7, 5, 4, 4]
[1, 3, 6, 8, 9, 7, 5, 4, 4]
I'm looking for it to return this:
[1, 3, 4, 4, 5, 6, 7, 8, 9]
When n is 4, the next minimum is 4 again, but lst.index() finds the first 4 at position 3 instead.
Start searching for the miminum from n; the .index() method takes a second argument start, from where to start searching:
i = lst.index(a, n)
Note that Python can assign to two targets in place, no need to use a temporary intermediate. range() with just one argument starts from 0:
for n in range(len(lst)):
a = min(lst[n:])
i = lst.index(a, n)
lst[n], lst[i] = a, lst[n]
Demo:
>>> lst = [6, 8, 9, 1, 3, 4, 7, 5, 4]
>>> for n in range(0,len(lst)):
... a = min(lst[n:])
... i = lst.index(a, n)
... lst[n], lst[i] = a, lst[n]
...
>>> lst
[1, 3, 4, 4, 5, 6, 7, 8, 9]
Let's say I have the following pattern:
PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
and I want to use this to create the following:
PATTERN | HORIZONTAL_MIRROR (PATTERN)
VERTICAL_MIRROR(PATTERN) | HORIZONTAL_MIRROR(VERTICAL_MIRROR(PATTERN))
In other words:
[[1, 2, 3, 3, 2, 1],[4, 5, 6, 6, 5, 4],[7, 8, 9, 9, 8, 7], [7, 8, 9, 9, 8, 7], [4, 5, 6, 6, 5, 4], [1, 2, 3, 3, 2, 1]]
is there an efficient method that can be used in Python apart from copying each element from the PATTERN?
Just an idea:
PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
def patternify(l):
for sl in l:
yield sl+sl[::-1]
for sl in l[::-1]:
yield sl+sl[::-1]
list(patternify(PATTERN))
#output: [[1, 2, 3, 3, 2, 1], [4, 5, 6, 6, 5, 4], [7, 8, 9, 9, 8, 7], [7, 8, 9, 9, 8, 7], [4, 5, 6, 6, 5, 4], [1, 2, 3, 3, 2, 1]]
If I understood the requirement correctly:
def mirror(pat):
return pat + pat[::-1]
SUPERPATTERN = mirror(map(mirror, PATTERN))