can someone explain why these two loops look like they do the same thing, when in fact the second version doesn't find the duplicate number correctly?
list1 = [1,2,13,4,6,6,8,11,10]
for i in range(len(list1)):
for j in range(i+1, len(list1)):
if list1[i] == list1[j]:
print i, j
print True
else:
print False
print "--------------------------------------"
for i in list1:
for j in list1:
if i == j + 1:
print True, i, j
else:
print False
for i in range(len(list1)):
for j in range(i+1, len(list1)):
if list1[i] == list1[j]:
print i, j
print True
else:
print False
The code above iterates over a list which is generated by the range() function which returns a list of numbers that in your case is [0,1,2,....,len(list1) - 1] in the first loop.In the second loop the list that you go over is [i + 1, i + 2, i + 3,...,len(list1) - 1]. On every iteration i and jare assigned to one item of the list just like a normal for loop (Java, C#, C++ and more).
for i in list1:
for j in list1:
if i == j + 1:
print True, i, j
else:
print False
In this code i and j are assigned to each item value in list1.
On every iteration i and j will be assigned to the next item value in the list, NOT THE POSITION.
In your case i value will be 1 then 2 then 13... Same applies to j.
They don't do the same thing at all. The second one loops through the whole of list1 in the inner loop, instead of just from the current index onwards. And, for some reason, you add 1 to the value before comparing, so it wouldn't be equal.
Related
I' like to convert list append code with mulitple for and if, elif to list comprehension.
I tried but only temp_list worked.
temp_list2 did not work properly
Original code
temp_list = []
temp_array = accd['ACCD_SEQ'].values
temp_list = [i for i in temp_array if len(accd[accd['ACCD_DTL'].str.contains(i)]) != 0]
temp_list2 = []
for i in temp_list:
if len(accd[accd['ACCD_DTL'].str.contains(i)]) == 1:
temp_list2.append(i)
elif len(accd[accd['ACCD_DTL'].str.contains(i)]) > 1:
for j in range(len(accd[accd['ACCD_DTL'].str.contains(i)])):
temp_list2.append(i)
else:
pass
My trial
temp_list2 = [i if len(accd[accd['ACCD_DTL'].str.contains(i)]) == 1 else i for j in range (len(accd[accd['ACCD_DTL'].str.contains(i)])) if len(accd[accd['ACCD_DTL'].str.contains(i)]) > 1 for i in temp_list
This code said "UnboundLocalError : local variable 'i' referenced before assignment"
Anyone can help to fix it?
Let's consider the following function sumlists, which "flattens" numerical 2-dimensional lists by taking the sum of the values in every row that are larger than 1:
def sumlists(lst):
new_lst = []
for i in range(len(lst)):
sum_ = 0
for j in range(len(lst[i])):
if lst[i][j] > 1:
sum_ += lst[i][j]
new_lst.append(sum_)
return new_lst
For instance, if lst is:
lst = [
[1, 2, 3],
[4, 5, 1, 6],
[7, 1]
]
the output would be:
[5, 15, 7]
The function has two nested for loops with indices i and j to traverse lst rows and columns, respectively. Before going through the values in each row, we need to define a variable, in this case sum_, to hold the sum up to the jth value in the ith row. The condition if lst[i][j] > 1: prevents any number less than 2 from being added to sum_. Finally, when all the values in the ith row have been gone through, the resulting sum is appended to the "flattened" list (new_list). With this, let's break all this info down to make our way from the function definition to list comprehension.
Step 1. row for loop:
arr[i] for i in range(len(arr))
Step 2. column for loop:
arr[i][j] for j in range(len(arr[i]))] for i in range(len(arr))
Step 3. condition for the sake of the sum, values larger than one are returned, otherwise 0:
arr[i][j] if arr[i][j] > 1 else 0 for j in range(len(arr[i]))] for i in range(len(arr))
Step 4. sum: adding all the values in [arr[i][j] if arr[i][j] > 1 else 0 for j in range(len(arr[i]))]
sum([arr[i][j] if arr[i][j] > 1 else 0 for j in range(len(arr[i]))]) for i in range(len(arr))
Step 5. append the sums to the new_lst:
↓ here ↓ here
[sum([arr[i][j] if arr[i][j] > 1 else 0 for j in range(len(arr[i]))]) for i in range(len(arr))]
I hope this answers your question.
I am trying to convert this pseudocode into python code
1 for j = 2 to A.length
2 i=1
3 while A[j]>A[i]
4 i = i+1
5 key = A[j]
6 for k = to j -i - 1
7 A[j-k] = A[j-k-1]
8 A[i] = key
The program says that i have to take 10 inputs and display it in the sorted array using Insertion sort algorithm Here's what i done at the moment i'm stuck at line 6 how do i convert that to python code
A= [int(i) for i in input ('Enter Number').spilt()]
for j in range(2, len(A)):
i = 1:
while A[j] > A[i]:
i = i+1
key = A[j]
for:#?
A[j-k] = A[j-k-1]
A[i] = key
First of all there is something missing in that line 6:
for k = to j -i - 1
It should read:
for k = 0 to j - i - 1
There are a few things to bear in mind when converting to Python:
In Python the first list element is at index 0, not index 1 as is the case in that pseudo code. So that means you need to subtract one at several places (where it concerns i and j).
Similarly, the range function's second argument (the "end" of the range) is a value that is not included in the range, while in the pseudo code, what follows to is included.
So here is how you could translate the code:
A= [int(i) for i in input ('Enter Number').split()]
for j in range(1, len(A)):
i = 0 # zero-based index
while A[j] > A[i]:
i = i+1
key = A[j]
for k in range(0, j-i):
A[j-k] = A[j-k-1]
A[i] = key
As stated in comments, the loop on k seems overly complex. It could just be:
for k in range(j, i, -1):
A[k] = A[k-1]
And a bit more pythonic would be to replace that for loop with:
A[i+1:j+1] = A[i:j]
Finally, the outer loop can take the index and value (key) with enumerate, which saves a few repetitive list look-ups in the form of A[j]:
for j, key in enumerate(A):
i = 0
while val > A[i]:
i = i+1
A[i+1:j+1] = A[i:j]
A[i] = key
With this for loop, you get one more iteration (with j == 0) which does nothing really, so you may decide to use the original loop.
So, for example, say I have a list like
List1 = [5, 6, 7, 1, 40]
And I want the solution to return true only if the value in the list that we’re looking at is 5 times large than every other member of the list. So if we’re looking at the first item, 5, the solution should return false (even though it is 5 times bigger than 1 it is not compared to the other members) and should return true when looking at the last member, 40. In python I wrote something like:
for i in range(len(list1)):
for j in range(i + 1, len(list1)):
if i >= (j*5):
return True
else:
return False
But I don’t think this is a great solution. I know this shouldn’t be so hard but I’m going through some health issues that’s making this hard for me. Any thoughts or help would be appreciated!
You are comparing i and j instead of comparing the list values and you are returning true before comparing your list1[i] value with all other list1[j] values.
Check the condition for all the j values, and use count to check if the value is greater than all other values.
count = 0
for i in range(len(list1)):
for j in range(i + 1, len(list1)):
if (list1[i] >= (list1[j]*5)):
count = count + 1
if(count == len(list1) - 1):
return True
return False
Try this:
def check(lst, value_index):
return lst[value_index] >= 5 * max(q for e, q in enumerate(lst) if e != value_index)
The following will give you a list of True/False value for each element of List1 where your condition is satisfied.
[all(List1[i] > List1[j] * 5 for j in range(len(List1)) if i != j)
for i in range(len(List1))]
lst = []
for i in range(1,13196):
if 13195 % i == 0:
lst.append(i)
for k in range(len(lst)):
for j in range(1,lst[k]):
if lst[k] % j != 0:
lst.remove(lst[k])
print(lst)
When I run this code, it says if lst[k] % j != 0: List index out of range.
Can anyone tell me what I did wrong ?
You are iterating over a range of the current length of lst. Every time you do lst.remove(lst[k]), the lst gets shorter, so the range you were using becomes unsuitable. For example, if you start with 8 elements, it goes from indices of 0 to 7 inclusive, but if you remove an element, the last index will then be 6, not 7.
The easiest way to fix this is to simply create a new list:
lst = []
for i in range(1,13196):
if 13195 % i == 0:
lst.append(i)
result = lst[:] # create a copy to work with
for k in range(len(lst)):
for j in range(1,lst[k]):
if lst[k] % j != 0 and lst[k] in result: # check that it's there before removal
result.remove(lst[k])
print(result)
That said, this code eliminates every number which cannot be evenly divided by every positive integer smaller than that number, which understandably produces a result of [1]. If that's not what you wanted, you'll have to take another look at your algorithm.
lst = []
lst2 = []
for i in range(1,13196):
if 13195 % i == 0:
lst.append(i)
lst2.append(i)
for k in range(len(lst)):
for j in range(1,lst[k]):
if lst[k] % j != 0:
lst2.remove(lst[k])
print lst2
Give this a try. Your problem is that you were iterating over the value of the original list. When you removed parts of the original list, the length of this value shortened, but the for loop will not account for it.
for l in range(1,len(S)-1):
for i in range(1,len(S)-l):
j=i+l
for X in N:
max_score = 0
args = None
if j==2:
print j
for s in range(i,j-1):#s is for split point
if j==2:
print j
for W in probBiNonterDic.keys():#y<==> X->YZ
if j==2:
print j
as you see that in first for block of X if j==2: print 2 but when in second block of s and W, if j==2 does not print 2...why is it, changing j value
Because both the l and i loop start a 1 and j = i + l. At this point j == 2 and prints out. When you try and loop from 1 to 1 in the s loop you actually don't loop even a single iteration and by the next time you get to the s loop j no longer equals 2.
>>> for x in xrange(1,1):
... if True:
... print "hi"
>>>
It looks like your problem is with the second for loop. You are trying to loop over a range that goes from i --> j -1, but because you increase the value of j at the beginning, this is the same as writing range(i, i). Perhaps you could increment j at the bottom of the for i in range(1, len(s) - 1) loop.