Python Practice Exercises (beginner) - Data Structures #1 - Find Common element

preview_player
Показать описание
Just created a facebook page:


Here is my twitter account for programming:


Here is my github account:

Рекомендации по теме
Комментарии
Автор

finally a channel that starts off easy instead of full going into complex stuff

SlpyStrdust
Автор

a = [1, 3, 5, 7, 9, 10, 11, 12, 13, 14]
b = [2, 4, 6, 8, 9, 11, 12, 13]

commonNumbers = []
for numA in a:
for numB in b:
if numA == numB:
commonNumbers.append(numA)


print(commonNumbers)

nicolasdemaria
Автор

I was literally looking for a video like this 10 minutes ago and was only able to find data structures for java :)) you are one of my best subscriptions

DarthRhaadoo
Автор

def intersec(list1, list2):
for i in list1:
for j in list2:
if i == j:
return i

print(intersec(a, b))

norman
Автор

You are one of my best programmer ever. Very good and elaborate video. Or I must say only video on data structure on yt. And It is also very easy to understand please keep making such videos.

hafizharoonrasheedch
Автор

I got a question. How would you ignore a ValueError and continue on to the next value in the example below?

matrix = [
[1, 0, 0, 1, 0],
[1, 0, 1, 0, 0],
[0, 0, 1, 0, 1],
[1, 0, 1, 0, 1],
[1, 0, 1, 1, 0]
]
river_list = [ ]

#finds all the 1's in the matrix which represents a river location and adds the locations to the "river_list" variable

def add_river(entry):
global river_list
for x in range(len(matrix)):
for y in range(len(matrix[x])):
if matrix[x][y] == 1:
river_list.append((x, y))

#Pops one value from the river list so that the neighboring locations can be checked for 1's (rivers)
current = river_list.pop()

#defines the values that are vertical and horizontal from the 'current' river location
check_river_locations = [matrix[current[0] + 1][current[1]], matrix[current[0] - 1][current[1]],
matrix[current[0]][current[1] + 1], matrix[current[0][current[1] - 1]]]

Here's where I run into a problem. If current is at the last element of the array it will throw a index error because iterating over "check_river_locations" checks up, down, left, and right from the current location. How do you skip past the ones that are out of range? I know that I should use "try" and "except" but how?

Ramel
Автор

cool sir ...i found it too easy to understand

ZubairKhan-snio
Автор

a = [1, 3, 5, 7, 9]
b = [2, 4, 6, 8, 9]
for i in a:
for j in b:
if i == j:
print(i, "is the intersecting element")

MoadGhaziGhazi
Автор

I did the same exercise with for loops is that right?

haithemkaili
Автор

O wao i found your programming skills awesome..this help me in Python course.thank you sir

malikhamza
Автор

# just wondering as a beginner if this is bad practice since my result is [5, 9] and not [9, 5]
intersection = [i for i in a if i in b]
print(intersection)

untiedbear
Автор

Great explanation!! Thanks again for the great tutorials!!

akiratoriyama
Автор

very good video. you are very good teacher and i found this video useful as you make us understand the code from line to line . very easy for beginners like me. keep up the good work

umairabdulkhaliq
Автор

You are a wonderful explainer your videos are self explained and very good illustrations and animations are perfect easy to grasp. I just have one question are you available on Instagram or the social media platform that you mentioned because I have few questions that if you explain me on direct mesg

ahtishamali