So i have for example t=[1.0, 1.0, 1.6, 1.125, 1.5]
I want to print the indices for the elements that have the minimum value min(t) but i want them to start from 1
So for this example i want to print 1 2
It's working when i do this:
for j in range(len(t)):
if t[j]==min(t):
print j+1,`
Output :
1 2
But it's not working with this:
for j in t:
if j==min(t):
a=t.index(j)
print a+1,`
Output :
1 1
Why is that?
Your error is here
a = t.index(j)
t.index(j) is going to give you the first index of that value.
You can make this:
m = min(t)
cont = 0
for j in t:
if j == m:
print cont+1,
cont += 1
Or
for idx, val in enumerate(t):
if val == m:
print idx + 1,
Related
I want to write to file elements of the three arrays: k= (/1, 2 /), kp = (/1, 2 /), w(k,kp) = (/1,2 ,3,4/)
in the following pattern, using Fortran:
k kp w(k,kp)
1 1 1
1 2 2
2 1 3
2 2 4
I know how to write for column "kp" and "w", but how can I write column "k" ?
I have my code as:
write(20,*) "k" , "kp", "W"
do i = 1,2
do j = 1, 2
write (20,*) k( ) kp(j) , W(i,j)
end do
end do
Is this homework problem?
program foo
implicit none
integer :: i, j, k(2) = [1,2], kp(2) = [1,2]
integer :: w(2,2) = reshape([1,3,2,4], [2,2])
do j = 1, 2
do i = 1, 2
write(*,'(*(1X,I0))') k(j), kp(i), w(k(j),kp(i))
end do
end do
end program foo
I have this list: l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] and I would like to iterate on it, printing something like
0: [1,2,3,4]
1: [2,3,4,5]
2: [3,4,5,6]
...
n: [17,18,19,20]
So far I made this code to print 5 elements at a time, but the last iteration prints 3:
for index, item in enumerate(l):
if index == 0 or index == 1 or index == 2:
continue
print index, l[index - 3:index + 2]
How can I solve this?
You're on the right track with your list slices. Here's a tweak to make it easier:
sub_len = 4
for i in range(len(mylist) - sub_len + 1):
print l[i:i + sub_len]
Where sub_len is the desired length of the slices you print.
demo
I'm trying to make a program that print every 100K-th odd prime number until 10M using Potion, my code:
last = 3
res = (last) # create array
loop:
last += 2
prime = true
len = res length -1
i = 0
while(i<len):
v = res at(i)
if(v*v > last): break.
if(last%v == 0): prime = false, break.
i += 1
.
if(prime):
res append(last)
if(res size % 100000 == 0): last print.
if(last>9999999): break.
.
.
But this gives Segmentation fault (core dumped), I wonder what's wrong?
For reference, the working Ruby version:
res = [last=3]
loop do
last += 2
prime = true
res.each do |v|
break if v*v > last
if last % v == 0
prime = false
break
end
end
if prime
res << last
puts last if res.length % 100000 == 0
break if last > 9999999
end
end
The output should be:
1299721
2750161
4256249
5800139
7368791
8960467
and no, this is not a homework, just out of curiosity.
you found it out by yourself, great!
println is called say in potion.
And it crashed in res size.
E.g. use this for debbugging:
rm config.inc
make DEBUG=1
bin/potion -d -Dvt example/100thoddprime.pn
and then press enter until you get to the crash.
(example/100thoddprime.pn:18): res append(last)
>
; (3, 5)
[95] getlocal 1 1 ; (3, 5)
[96] move 2 1 ; (3, 5)
[97] loadk 1 5 ; size
[98] bind 1 2 ; function size()
[99] loadpn 3 0 ; nil
[100] call 1 3Segmentation fault
so size on res returned nil, and this caused the crash.
And instead of last print, "\n" print.
Just do last say.
This came from perl6 syntax, sorry :)
My bad, I forgot to change from res length -1 to res length when changing from 0 to len (i), because this syntax not recognized as a loop (failed to receive break).
last = 3
res = (last)
loop:
last println
last += 2
prime = true
len = res length
i = 0
while(i<len):
v = res at(i)
if(v*v > last): break.
if(last%v == 0): prime = false, break.
i += 1
.
if(prime):
res append(last)
if(res length % 100000 == 0): last print, "\n" print.
if(last>9999999): break.
.
.
i am having trouble with aligning outcome values.
Alist = ["1,25,999",
"123.4,56.7890,13.571",
"1,23.45,6,7.8"]
c = 0
while c < len(Alist):
r = 0
tokens = Alist[c].split(',')
while r < len(Alist[c].split(',')):
if '.' in tokens[r]:
print "%7.2f" %float(tokens[r]), " ",
else :
print "%3d" %float(tokens[r]), " ",
r += 1
print
c += 1
I want to print such as
1 25 999
123.40 56.79 13.57
1 23.45 6. 7.80
but somehow it is printing
1
25
999
123.40
56.79
13.57
1
23.45
6
7.8
and i cannot figure out what is wrong with my coding.
after the r+1, you have a lone print statement. it is at the wrong indention level - move it to the left by 4 spaces (or one tab) and it should work fine.
The print statement should'nt in the 2nd while loop. just:
Alist = ["1,25,999",
"123.4,56.7890,13.571",
"1,23.45,6,7.8"]
c = 0
while c < len(Alist):
r = 0
tokens = Alist[c].split(',')
while r < len(Alist[c].split(',')):
if '.' in tokens[r]:
print "%7.2f" %float(tokens[r]), " ",
else :
print "%3d" %float(tokens[r]), " ",
r += 1
print
c += 1
In [59]: %paste
myList = ["1,25,999",
"123.4,56.7890,13.571",
"1,23.45,6,7.8"]
rows = [r.split(',') for r in myList]
widths = {i:max(len(c) for c in col) for i,col in enumerate(itertools.izip_longest(*rows, fillvalue=""))}
for row in rows:
for i,val in enumerate(row):
print " "*((widths[i] - len(val))/2), val, " "*((widths[i] - len(val))/2) if not (widths[i]-len(val))%2 else " "*((widths[i] - len(val)+1)/2),
print
## -- End pasted text --
1 25 999
123.4 56.7890 13.571
1 23.45 6 7.8
def compare_two_lists(list1,list2):
i=0
j=0
while i < len(list2) :
if i%2 == 0:
j == 0
else:
j == 1
for sublist2 in list2[i:] :
for sublist in list1[j:]:
#sublist.intersection(sublist2)
intersect = [x for x in sublist if x in sublist2]
print('from set ',sublist, len(intersect),' matched number(s): ', intersect)
i=i +1
compare_two_lists([[1,2,3,4,5],[20,30]],[[5,3,7,8,1],[20,10],[4,10,1,7,8],[30,20]])
I am trying to get list 0 and 1 of list 1 to compare appropriately list 0, 1, 2 and 3 of list 2 and return matches. The program almost works in the sense that it does return matches for the lists amongst other iterations. I cannot seem to be able to get the iteration to occur twice and return [1,3,5],[20], [1,4],[20,30]. Please help. I am going quite mad trying to understand how to space functions correctly and use loops logically!!
def compare_two_lists(list1,list2):
lRet = [] #a
for i in range(len(list2)): #b
j= i%2 #c
sublist1 = list1[j]
sublist2 = list2[i]
lRet.append([x for x in sublist1 if x in sublist2])
return lRet
compare_two_lists([[1,2,3,4,5],[20,30]],[[5,3,7,8,1],[20,10],[4,10,1,7,8],[30,20]])
This seems to do the trick. Here's an explanation (see line labels above):
a: lRet is initialised. We are doing to build up a list of lists in this variable
b: i runs through every index of list2
c: if i is even then j is zero, otherwise j is one. you had j==1 and j==0 in your code. These operators don't change any operand values. you meant j=1 etc. but this way is quicker
For explaining the rest I'll just talk through the actual iteration with your example input lists:
i = 0 (the first iteration)
j=0
sublist1 = [1,2,3,4,5]
sublist2 = [5,3,7,8,1]
intersection is [1,3,5]. we APPEND this to lRet.
thus lRet = [[1,3,5],]
i=1
j=1
sublist1 = [20,30]
sublist2 = [20,10]
the intersection is [20,]
thus lRet = [[1,3,5],[20]]
i=3
j=0
sublist1 = [1,2,3,4,5]
sublist2 = [4,10,1,7,8]
etc