Search This Blog
Popular Posts
-
Let say we have a String something like below. String sample="abc*123"; We want to split this String by '*'. We can...
-
sound -Convert matrix of signal data to sound Syntax sound(y,Fs) sound(y,Fs,bits) Description sound(y,Fs) sends audio signal y to the speak...
-
There are new phones will remove within next few weeks from sony. Sony xperia tipo, sony xperia tipo dual Sony xperia dual has dual sim...
-
When i try to configure mysql with CAS There were lots of problem occurred and i cannot find a good tutorials about this.I followed some tut...
-
PROBLEM 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st pr...
-
This delete the file f1 File f1 = new File(file); boolean success = f1.delete(); if (!success){ System.out.println("Deleti...
-
PROBLEM The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. ANSWER 142913828...
-
In this post i will show you how to configure tomcat 7.x for SSL protocol in windows First we want to generate certificate file using jav...
-
samsung smart app challenge 2012 This contest offers $4.08 million in cash prizes and mega marketing support for app promotion to the to...
-
If you did not format your flash drive, then check whether the files are in hidden mode. Then follow these steps: Click on the link below ...
Followers
Thursday, January 17, 2013
generate number of random numbers that have not repeat in a given range in python
random.sample(range(1, 16), 3)
[11, 10, 2]
get element, sublist and length of a list using python
d = range(10)
>>> d
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> d[9]
9
>>> d[-1]
9
>>> d[0:9]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> d[0:-1]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> len(d)
10
Matrix in python
There are no inbuilt function for define matrix. You can define the matrix as a list of list as follow
table= [ [ 0 for i in range(6) ] for j in range(6) ]
print table
result
[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
make a list of class objects
class Person(object):
"""__init__() functions as the class constructor"""
def __init__(self, name=None, job=None, quote=None):
self.name = name
self.job = job
self.quote = quote
# make a list of class Person(s)
personList = []
personList.append(Person("Payne N. Diaz", "coach", "Without exception, there is no rule!"))
personList.append(Person("Mia Serts", "bicyclist", "If the world didn't suck, we'd all fall off!"))
personList.append(Person("Don B. Sanosi", "teacher", "Work real hard while you wait and good things will come to you!"))
personList.append(Person("Hugh Jorgan", "organist", "Age is a very high price to pay for maturity."))
personList.append(Person("Herasmus B. Dragon", "dentist", "Enough people can't find work in America!"))
personList.append(Person("Adolph Koors", "master-brewer", "Wish you were beer!"))
personList.append(Person("Zucker Zahn", "dentist", "If you drink from the fountain of knowledge, quench your thirst slowly."))
print "Show one particular item:"
print personList[0].name
print "Sort the personList in place by job ..."
import operator
personList.sort(key=operator.attrgetter('job'))
print "... then show all quotes and who said so:"
for person in personList:
print "\"%s\" %s (%s)" % (person.quote, person.name, person.job)
print "Show the quote(s) from any dentist:"
look = 'dentist'
for person in personList:
if look in person.job:
# title() capitalizes the job's first letter
print "%s %s: \"%s\"" % (person.job.title(), person.name, person.quote)
print "What the heck did the person named Sanosi say?"
look = "Sanosi"
for person in personList:
if look in person.name:
print "%s: \"%s\"" % (person.name, person.quote)
for loops in python
for num in range(10,20): #to iterate between 10 to 20
for i in range(2,num): #to iterate on the factors of the number
if num%i == 0: #to determine the first factor
j=num/i #to calculate the second factor
print '%d equals %d * %d' % (num,i,j)
break #to move to the next number, the #first FOR
else: # else part of the loop
print num, 'is a prime number'
Subscribe to:
Comments (Atom)