problem with nested while loops in Python - python

I have 2 lists of names, names_1 and names_2, they are both of the same length (1000), no list has duplicate names, each list contains 1000 unique names, but some names appear in both lists, I want to isolate the names that appear in both lists in a third list dup_names and remove them from the original lists, I tried the following code but it didn't work:
dup_names=[]
m=len(names_1)
i=0
k=0
COMPARED=[]
while i<m:
while k<m:
COMPARED.append((i,k))
if names_1[i]==names_2[k]:
dup_names.append(names_1[i])
names_1[i:i+1]=[]
names_2[k:k+1]=[]
m=m-1
k=0
else:
k=k+1
i=i+1
for i_k_pair in COMPARED:
print(i_k_pair)
print('len(COMPARED) =',len(COMPARED))
print('len(dup_names) =',len(dup_names))
print('len(names_1) =',len(names_1))
print('len(names_1) =',len(names_2))
I created the "COMPARED" list to check what (i,k) values got compared to each other, the output was:
(0,0)
(0,1)
.
.
.
(0,999)
len(COMPARED) = 1000
len(dup_names) = 0
len(names_1) = 1000
len(names_2) = 1000
from the i_k_pairs that got printed, I see that it only compaired the 1st name in names_1 (i=0) to all names in names_2, since len(dup_names) = 0, len(names_1) = 1000 and len(names_2) = 1000, I expext that len(COMPARED) = 1000000 but it looks like the is a problem with the outed loop, can anyone please explain what's wrong to me?!?!
I know that I can do the job with the following code:
dup_names=[]
for i in range(len(names_1)):
for k in range(len(names_2)):
if names_1[i]==names_2[k]:
dup_names.append(names_1[i])
new_names_1=[]
new_names_2=[]
for n in range(len(names_1)):
s1=0
s2=0
for m in range(len(dup_names)):
if names_1[n]==dup_names[m]:
s1=1
if names_2[n]==dup_names[m]:
s2=1
if s1==0:
new_names_1.append(names_1[n])
if s2==0:
new_names_2.append(names_2[n])
names_1=new_names_1
names_2=new_names_2
del new_names_1,new_names_2
I tried this code and it worked just fine, but what's really puzzling me is:
what's wrong with the first code???
why the outer while loop didn't work?!
is there a problem with nested while loops in Python?

This is comparing the two lists for duplicates and never compares anything already compared in names_1. I think there might be a well optimized one for this. If you wanna compare the two lists len(names_1) times len(names_2) better avoid removing elements from names_1 and names_2. The reason your code didn't work is, your code was not resetting i=0 to start the names_1 from the beginning after the second while loop.
dup_names=[]
names_1=["a","b","c","d","e"]
names_2=["f","g","h","a","d"]
m=len(names_1)
n=len(names_2)
i=0
k=0
COMPARED=[]
while i<m:
matched_any = False
while k<n:
COMPARED.append((i,k))
if names_1[i]==names_2[k]:
dup_names.append(names_1[i])
names_1[i:i+1]=[]
names_2[k:k+1]=[]
matched_any = True
break
else:
k=k+1
i=0
k=0
if not matched_any:
names_1[i:i+1]=[]
m=len(names_1)
n=len(names_2)
for i_k_pair in COMPARED:
print(i_k_pair)
print('len(COMPARED) =',len(COMPARED))
print('len(dup_names) =',len(dup_names))
print('len(names_1) =',len(names_1))
print('len(names_1) =',len(names_2))

Use Pythonic approach and set for solving your problem.
x = ["C++", "C", "Python"]
y = ["Java", "C"]
z = set(x).intersection(set(y))
x = [elt for elt in x if elt not in z]
y = [elt for elt in y if elt not in z]

Related

Print a number not in a list python

I'm having an issue with a simple task it seems, but cannot figure it out. I believe the solution is within the code:
n = input().split(',')
list1 = []
list2 = []
for x in n:
list1.append(int(x))
for y in range(1, len(list1 + 1)):
if y not in list1:
list2.append(y)
print(list2)
The task is:
Given an array of integers, some elements appear twice and others appear once.
Each integer is in the range of [1, N], where N is the number of elements in the array.
Find all the integers of [1, N] inclusive that do NOT appear in this array.
Constrain:
N will always be in the range of [5, 1000]
Input:
1,2,3,3,5
Output:
4
Input:
1,1,1,1,1,1,1,1
Output:
2,3,4,5,6,7,8
My idea is to have two empty arrays. The first one I will write all the numbers using a for loop. Once I have them all there I will use another loop where I can look and find the missing numbers. I guess it related to some formatting that is killing my logic.
Any help would be appreciated!
Thanks
You only have to change this line:
for y in range(1, len(list1 + 1)):
to this one:
for y in range(1, len(list1)+1):
The problem is that you added one to the list, but you want to add 1 to the length of the list.

replacing string integer in list, python

I wanted to replace string with an integer so I first tried this
timeupseats=[0,480,480,0]
for n in range(0,4):
if timeupseats[n]==0:
timeupseats[n]='CLOSED'
for n in range(0,4):
if timeupseats[n]=='CLOSED':
timeupseats[n]==0
Because the first code didn't work, I tried this code for the second time and it worked
timeupseats=[0,480,480,0]
for n in range(0,4): #print closed when there is no ticket left
if timeupseats[n]==0:
timeupseats[n]='CLOSED'
timeupseats = [0 if i=='CLOSED' else i for i in timeupseats]
What's the difference between the first code and the second code?
Why did only the second code work?
In your first set of code you have this error on the last line :
timeupseats[n]==0
You want to set it to 0 (=) not check for equality (==)
There are many ways to to this. The in my opinion the most robust one would be to first define the values you want to replace, then apply that on each element of the list with ‘map’:
# make some data
ls = list(range(10))
# define values to replace
mapping = {0: 'CLOSED'}
# do replacement
ls = list(map(lambda x: mapping.get(x, x), ls))
print(ls)
There is also the more explicit, but more difficult to maintain way:
ls = list(range(10))
for i, x in enumerate(ls):
if x == 0:
ls[i] = 'CLOSED'
print(ls)

How to update a variable inside the while loop and see the results?

I want to use a for loop to define a range and I would like to use a while loop to check a condition for every value inside this for loop and give me the results for different values of c, but unfortunately, my algorithm doesn't work and I don't know what my mistake is.
j=0
jl=[]
c=np.linspace(0,20,num=20)
for a in range(0,len(c)):
while j<5:
j=c[a]+2
jl.append(j)
The result I am looking for is it puts different values of c inside the while loop and calculate j and check if it is bigger than 5 or not. if yes, it appends it to jl. Totally, I want to define a range with for loop with index and also check each value of this range inside while loop and get the results for j.
so expected results are j with values smaller than 5 (c[a]+2<5) and store the values of j in jl
There is one problem in your code:
j=0
"While loop" never runs because his condition is j>5. Correct that and tell us if it works.
Please double check if the following suggested solution covers all of your requirements. In any case I think List Comprehensions (e.g. compare section 5.1.3.) can help you.
c=np.linspace(0,20,num=20)
ls = [x+2 for x in c if((x+2)<5)]
print(ls)
Will result in the following output:
[2.0, 3.052631578947368, 4.105263157894736]
If you want to do more complex data manipulation, you can also do this with the help of functions, e.g. like:
def someManipulation(x):
return x+2
ls = [someManipulation(x) for x in c if(someManipulation(x)<5)]
Your algorithm doesn't work because
while j<5:
j=c[a]+2
is an infinite loop for j = 0, a = 0.
What you probably wanted to write was:
for x in c:
j = x + 2
if j < 5:
jl.append(j)
Still, the list comprehension version in the other answer does exactly the same, but better.

Python - Splitting lists of integers during iteration

I have an array of data where I'm taking a slice through a 3D array of floats and appending the numbers of the elements that satisfy a set of upper and lower bounds (also floats).
The first part of my code contains a nested for loop in the style of the following:
x_lst = []
for i in range(len(x1)):
for x in range(len(floatarray[0,:,0])):
if x1[i] <= floatarray[0,x,0] <= x2[i]:
x_lst.append(x)
#issues after this point
The inner loop compares the data in the array (floatarray[0,x,0]) with the boundaries x1 and x2, returning a list of integers, whilst the outer loop iterates through the values of the boundaries.
The issue I have is that the output (x_lst) is a single list, whereas I've been trying to produce a list of lists, with each sublist corresponding to the iterable i.
e.g.
#Desired output
x_lst = [[5,6,7,13,14],[21,22,23,36,37],[44,45,...
#Actual output
x_lst = [5,6,7,13,14,21,22,23,36,37,44,45,...
I feel like there's a very simple way of doing this, but I've not been able come up with anything that works (such as trying to use x_lst.split() after the append).
Also, any feedback on the question would be great as I'm still fairly new to SO.
It seems the following should work: why not create an intermediate sublist for each i, and add appropriate values to the sublist, then finally add the sublist to the main list?
x_lst = []
for i in range(len(x1)):
inner_list = [] # The sublist to be appended.
for x in range(len(floatarray[0,:,0])):
if x1[i] <= floatarray[0,x,0] <= x2[i]:
inner_list.append(x) # Add the numbers to the sublist.
# Issues after this point...
x_lst.append(inner_list) # Add the sublist to the main list.
Everything appears to be correct in the code except that you append into a 1-d array. For solving your problem you can simply change your code so that a temporary array named temp will append data in inner loop which would then be appended to your outer array x_lst as shown below:
x_lst = []
for i in range(len(x1))
temp=[]
for x in range(len(floatarray[0,:,0])):
if x1[i] <= floatarray[0,x,0] <= x2[i]:
temp.append(x)
x_lst.append(temp);
Okay, so Sam's answer is correct insofar as appending the values through the iteration, but trying it out with my code produced lists of lists of len(i) where each sublist contained all values of x that satisfied all elements of x1 and x2, instead of each list containing values specific to the [i]th element of x1 and x2, thus all the sublists were identical.
I got around this by replacing the inner for loop with a list comprehension, as follows, that gave the right result (in the desired form x_lst = [[5,6,7,13,14],[21,22,23,36,37],[44,45,...):
x_lst = []
for i in range(len(x1)):
y = [x for x in range(len(floatarray[0,:,0])) if x1[i] <= floatarray[0,x,0] <= x2[i]]
x_lst.append(y)

Adding a list to itself after addition to each value

I have a list of integers that follows a particular pattern, it's complex but for example say:
x = [0,2,4,6,8]
I'd like to extend the list with 9 more copies of itself, but add a constant value that linearly scales each time. E.g. if
constant = 10
loop = 9
Then the 2nd extension would result in:
x_new = [0,2,4,6,8,10,12,14,16,18]
So I think I want a loop that iterates through x and extends the array by x[i]+constant, loop number of times?
for i in range(loop):
for j in range(len(x)):
x_new = x.extend((x[j]+constant)*i)
Or perhaps this can be easily done through list comprehension? My actual list is ~3000 long and I'll be doing it a few times with different values of loop and constant.
Yes, list comprehension should work:
x_new = [ e + constant * i for i in range(loop+1) for e in x ]
I just did some work on above question, this code can be useful for above question.
x=[0,2,4,6,8]
y=x[4]
i=0
j=0
while(i<9):
z=range(y+2,y+12,2)
x.extend(z)
print x
y=y+10
i=i+1

Categories