[Tutor] Help on this assignment

2017-02-09 Thread Sasiliyu Adetunji
Please can you assist me in this assignment


Write a function called remove_duplicates which will take one argument
called string. This string input will only have characters between a-z.

The function should remove all repeated characters in the string and return
a tuple with two values:

A new string with only unique, sorted characters.

The total number of duplicates dropp
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on this assignment

2017-02-09 Thread Alan Gauld via Tutor
On 09/02/17 08:10, Sasiliyu Adetunji wrote:

> Write a function called remove_duplicates which will take one argument
> called string. This string input will only have characters between a-z.
> 
> The function should remove all repeated characters in the string and return
> a tuple with two values:
> 
> A new string with only unique, sorted characters.
> 
> The total number of duplicates dropp

You could use set() to remove the duplicates.
Then compare the lengths of the set with the length
of the original to determine how many letters were
dropped.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-09 Thread Vusa Moyo
Hi Alan.

You are correct with the indentation.

class Cat:
name = ""
kind = "cat"
color = ""
value = 100.00

def description(self):
desc_str = "%s is a %s %s cat worth R%.2f." % (self.name,
self.color, self.kind, self.value)
return desc_str

The above code is the question, which I am not allowed to edit.

So just to test the lecturer's code, I run the command

print(Cat.description())

This returns an error.

 TypeError: description() missing 1 required positional argument: 'self'

To me, this is flawed. I should be able to get a fault less response from
that command.

Any other code I append to it by inheriting the class Cat, will still  have
that similar error.

Now, I've added the following code to inherit the class Cat: description.

class Cat1(Cat):
name = "Whiskers"
kind = "Burmese cat"
color = "grey"
value = 3000.00

When I run this command, I still receive the same error.

print(Cat1.description())

Please assist where possible.

Regards

Vusa

On Wed, Feb 8, 2017 at 11:06 AM, Alan Gauld via Tutor 
wrote:

> On 08/02/17 07:11, Vusa Moyo wrote:
> > I have a suspicion my lecturer's question is flawed, so I'd like to pose
> it
> > to you guys to confirm my suspicions.
>
> I think your interpretation of the question is flawed.
> See Peter's reply for why.
>
> However another point is
>
> >  class Cat:
> >  name = ""
> >  kind = "cat"
> >  color = ""
> >  value = 100.00
> >  def description(self):
> >
> > desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color,
> > self.kind, self.value)
>
> Python is sensitive to indentation. This line needs
> to be indented inside the def statement. (This may
> be a mail formatting issue but since the rest of
> your code looks OK I doubt it)
>
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-09 Thread Alan Gauld via Tutor
On 09/02/17 09:25, Vusa Moyo wrote:

> class Cat:
> name = ""
> kind = "cat"
> color = ""
> value = 100.00
> 
> def description(self):
> desc_str = "%s is a %s %s cat worth R%.2f." % (self.name,
> self.color, self.kind, self.value)
> return desc_str
> 
> The above code is the question, which I am not allowed to edit.
> 
> So just to test the lecturer's code, I run the command
> 
> print(Cat.description())

But the definition of description() take an argument - self.
self is expected to be an instance of Cat.
You can either pass that in manually

print( Cat.description(Cat()) )

or, more normally, create an instance of cat and call
description on that:

my_cat = Cat()
print( my_cat.description() )

> Any other code I append to it by inheriting the class Cat, will still  have
> that similar error.

I'm not sure what you mean by that, I'd need an example.
If you mean you just add the code after the above line then
obviously you will still get the error.

> Now, I've added the following code to inherit the class Cat: description.
> 
> class Cat1(Cat):
> name = "Whiskers"
> kind = "Burmese cat"
> color = "grey"
> value = 3000.00
> 
> When I run this command, I still receive the same error.
> 
> print(Cat1.description())

For the same reason; you are still not passing an instance
of Cat (or Cat1) to the method. You need to create an
instance and then call the method on that:

other_cat = Cat1()
print( other_cat.description() )


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-09 Thread Vusa Moyo
Thanks so much. You've been a great help.

You have confirmed that the lecture's question is flawed.

Appreciate the help.

Regards

Vusa

On Thu, Feb 9, 2017 at 12:02 PM, Alan Gauld via Tutor 
wrote:

> On 09/02/17 09:25, Vusa Moyo wrote:
>
> > class Cat:
> > name = ""
> > kind = "cat"
> > color = ""
> > value = 100.00
> >
> > def description(self):
> > desc_str = "%s is a %s %s cat worth R%.2f." % (self.name,
> > self.color, self.kind, self.value)
> > return desc_str
> >
> > The above code is the question, which I am not allowed to edit.
> >
> > So just to test the lecturer's code, I run the command
> >
> > print(Cat.description())
>
> But the definition of description() take an argument - self.
> self is expected to be an instance of Cat.
> You can either pass that in manually
>
> print( Cat.description(Cat()) )
>
> or, more normally, create an instance of cat and call
> description on that:
>
> my_cat = Cat()
> print( my_cat.description() )
>
> > Any other code I append to it by inheriting the class Cat, will still
> have
> > that similar error.
>
> I'm not sure what you mean by that, I'd need an example.
> If you mean you just add the code after the above line then
> obviously you will still get the error.
>
> > Now, I've added the following code to inherit the class Cat: description.
> >
> > class Cat1(Cat):
> > name = "Whiskers"
> > kind = "Burmese cat"
> > color = "grey"
> > value = 3000.00
> >
> > When I run this command, I still receive the same error.
> >
> > print(Cat1.description())
>
> For the same reason; you are still not passing an instance
> of Cat (or Cat1) to the method. You need to create an
> instance and then call the method on that:
>
> other_cat = Cat1()
> print( other_cat.description() )
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-09 Thread Alan Gauld via Tutor
On 09/02/17 10:42, Vusa Moyo wrote:
> Thanks so much. You've been a great help. 
>
> You have confirmed that the lecture's question is flawed.

It is not, it is exactly right.
(Albeit unusual in its use of class attributes) but there is
nothing wrong with the code, only the way you were trying to use it.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Find (list) strings in large textfile

2017-02-09 Thread Sylwester Graczyk

Hi all,
I'm try to write a code to search strings (from one textfile with lists 
of strings) in second large text file.

but script doesn't search my lists in entire file (not complete output file)

*[list_life.txt]*
1654
964563
41164
6165456
85248
999745
35496486
... +2000 row's

*[large_file.txt]
*16542017/02/02
6664452017/02/02
9645632017/02/02
411642017/02/02
24679242017/02/02
61654562017/02/02
11452017/01/02
852482017/01/02
33541102017/01/02
9997452017/01/02
87788732017/01/02
354964862017/01/02
6665646462017/01/02
... + 50 rows

*[code]*
file_list = open("list_life.txt")
file_large = open("large_file.txt")
save_file = open('output.txt', 'w')

for line_list in file_list:
splitted_line_list = line_list.split()
for line_large in file_large:
splitted_line_large = line_large.split()
if splitted_line_large[0] == splitted_line_list[0]:
save_file.write(line_large+"\n")

file_large.close()
file_list.close()

*
*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Find (list) strings in large textfile

2017-02-09 Thread Danny Yoo
Files don't rewind automatically, so once a loop goes through the file
once, subsequent attempts will finish immediately.

We might fix this by "seek", which will let us rewind files.

However, your data is large enough that you might want to consider
efficiency too.  The nested loop approach is going to be expensive, taking
time proportional to the product of the sizes of your input files.

The problem can be done more efficiently, iterating over each file exactly
once.  Here is a sketch:

Try storing the numbers you are looking to find, and keep it in a set.
That is the loop over the first file.

Then, loop over the second file, consulting the set to see if the line is a
candidate or not.  Set membership is expected to be an inexpensive
operation.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Find (list) strings in large textfile

2017-02-09 Thread Alan Gauld via Tutor
On 09/02/17 19:15, Sylwester Graczyk wrote:
> Hi all,
> I'm try to write a code to search strings (from one textfile with lists 
> of strings) in second large text file.
> but script doesn't search my lists in entire file (not complete output file)

The problem is that you open the data file once,
before the loop, but you never reset the cursor so when
it reaches the end after the first iteration it never
reads any more data. You need to seek(0) at the start
of each loop. However...

Your approach looks very inefficient however.
You will read 500,000 lines 2000 times. That's
a lot of file access - about 1 billion reads!

It is probably better to store your key file in memory
then loop over the large data file and check the
line against each key. Better to check 2000 data
keys in memory for one loop of the data file.
That way you only read the key file and data file
once each - 502,000 reads instead of a billion.

Also instead of splitting the line you could
just use

if line_large.startswith(key)

If the length of the comparison is critical
use the optional positional arguments:

if line_large.startswith(key,start,stop)

That should save a small amount of time compared
to splitting and indexing both lines each time.


> *[list_life.txt]*
> 1654
> 964563
> ... +2000 row's
> 
> *[large_file.txt]
> *16542017/02/02
> 6664452017/02/02
> 9645632017/02/02
> ... + 50 rows
> 
> *[code]*
> file_list = open("list_life.txt")
> file_large = open("large_file.txt")
> save_file = open('output.txt', 'w')
> 
> for line_list in file_list:
>  splitted_line_list = line_list.split()

   file_large.seek(0)# reset the data file cursor here

>  for line_large in file_large:
>  splitted_line_large = line_large.split()
>  if splitted_line_large[0] == splitted_line_list[0]:
>  save_file.write(line_large+"\n")
> 
> file_large.close()
> file_list.close()

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor