[Tutor] Help with loops

2018-04-30 Thread Shannon Evans via Tutor
Hi, is there any way that i can add a loop or iteration or something so
that i dont have to write out every person who has fruit. This information
is coming from the following json files:
*queue.json* file

[

  ["James Bruce", "Bananas"],

  ["Katherine Newton", "Bananas"],

  ["Deborah Garcia", "Pears"],

  ["Marguerite Kozlowski", "Pineapples"],

  ["Kenneth Fitzgerald", "Pineapples"],

  ["Ronald Crawford", "Bananas"],

  ["Donald Haar", "Apples"],

  ["Al Whittenberg", "Bananas"],

  ["Max Bergevin", "Bananas"],

  ["Carlos Doby", "Pears"],

  ["Barry Hayes", "Pineapples"],

  ["Donald Haar", "Bananas"]

]



*stock.json* file

{

"Apples": 14,

"Bananas": 14,

"Pineapples": 0,

"Pears": 8

}

This is what i've done so far:

import json

#Load the stock and queue files
queue=json.load(open("queue.json"))
stock=json.load(open("stock.json"))

if (stock[queue[0][1]]>0):
#in stock
print "Gave Bananas to James Bruce"
else:
print "Could not give Bananas to James Bruce"

if (stock[queue[1][1]]>0):
#in stock
print "Gave Bananas to "+ queue[1][0]
else:
print "Could not give Bananas to "+ queue[1][0]

if (stock[queue[2][1]]>0):
#in stock
print "Gave Pears to "+ queue[2][0]
else:
print "Could not give Pears to "+ queue[2][0]

if (stock[queue[3][1]]>0):
#in stock
print "Gave Pineapples to "+ queue[3][0]
else:
print "Could not give Pineapples to "+ queue[3][0]

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


Re: [Tutor] Help with loops

2018-04-30 Thread Alan Gauld via Tutor
On 30/04/18 14:35, Shannon Evans via Tutor wrote:
> Hi, is there any way that i can add a loop or iteration or something so
> that i dont have to write out every person who has fruit. 

Yes that's what loops are for.
You have two options in Python: a 'for' loop or a 'while' loop

In your case I suspect a 'for' loop is most appropriate.

This information
> is coming from the following json files:
> *queue.json* file
> 
> [
> 
>   ["James Bruce", "Bananas"],
> 
...
> ]
> 
> 
> 
> *stock.json* file
> 
> {
> 
> "Apples": 14,
> 
> "Bananas": 14,
> 
> "Pineapples": 0,
> 
> "Pears": 8
> 
> }

> import json
> 
> #Load the stock and queue files
> queue=json.load(open("queue.json"))
> stock=json.load(open("stock.json"))
> 

You need to start the loop here then indent everything
below. The syntax is like

for data in queue:
name, product = data

Now change the queue access items to use name and product.

> if (stock[queue[1][1]]>0):
> #in stock
> print "Gave Bananas to "+ queue[1][0]
> else:
> print "Could not give Bananas to "+ queue[1][0]
> 
> if (stock[queue[2][1]]>0):
> #in stock
> print "Gave Pears to "+ queue[2][0]
> else:
> print "Could not give Pears to "+ queue[2][0]
> 
> if (stock[queue[3][1]]>0):
> #in stock
> print "Gave Pineapples to "+ queue[3][0]
> else:
> print "Could not give Pineapples to "+ queue[3][0]

Try it, if you get stuck come back and show us what you did.
You will find more about Looping in my tutorial(see below)

-- 
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] Matplotlib scatterplot help

2018-04-30 Thread Daniel Bosah
I have a function in which returns scatterplot of a Isomap
function, which takes output from a
TF-IDF  function, which
calculated TF-IDF values of certain articles online. I used four articles
and I want to show the 4 articles by a 3D scatterplot.

Below is the function to turn my Isomap values to a 3D scatterplot :

def Isomap(tfidf):
  jon = pd.read_csv(tfidf)
  le = preprocessing.LabelEncoder()
  tims = jon.apply(le.fit_transform)
  iso = manifold.Isomap(n_neighbors=2, n_components=3)
  john = iso.fit_transform(tims)
  fig = plt.figure()
  ax = fig.add_subplot(111, projection='3d')
  use_colors = 'rybg'
  ax.scatter(john[:,0], john[:,1],john[:,2],color=use_colors,alpha=.5) #
x,y,z coord. jon 1-3
  plt.title('Isomap of candiates')
  plt.xlabel('x')
  plt.ylabel('y')
  plt.show()
  plt.savefig('isomap.png')

The problem is that I usually only get one color returned. And even if I
get the code to print out 4 colors, I'm not sure how to get those colors
to  correspond to the four web articles.

Thanks for the help in advance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] updating stock list

2018-04-30 Thread Shannon Evans via Tutor
Hi, i'm wanting to update the stock list at the end so that the fruit
that's given is taken subtracted from the original stock list. The code
i've written isn't updating it it's just staying the same. Any idea what i
can do to fix this?


import json

stock_json= json.load(open("stock.json"))
queue_json= json.load(open("queue.json"))

queue=[
["James Bruce", "Bananas"],
["Katherine Newton", "Bananas"],
["Deborah Garcia", "Pears"],
["Marguerite Kozlowski", "Pineapples"],
["Kenneth Fitzgerald", "Pineapples"],
["Ronald Crawford", "Bananas"],
["Donald Haar", "Apples"],
["Al Whittenberg", "Bananas"],
["Max Bergevin", "Bananas"],
["Carlos Doby", "Pears"],
["Barry Hayes", "Pineapples"],
["Donald Haar", "Bananas"]
]

stock={
"Apples": 14,
"Bananas": 14,
"Pineapples": 0,
"Pears": 8
}

for i in queue:
if stock[i[1]]>0:
print("Gave {} to {}".format(i[1],i[0]))
else:
print("Could not give {} to {}".format(i[1],i[0]))

def total_stock(fruit):
total=0
for i in fruit:
if stock[i]>0:
stock[i]=stock[i]-1
return total
print stock
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] updating stock list

2018-04-30 Thread Alan Gauld via Tutor
On 30/04/18 23:15, Shannon Evans via Tutor wrote:
> Hi, i'm wanting to update the stock list at the end so that the fruit
> that's given is taken subtracted from the original stock list. The code
> i've written isn't updating it it's just staying the same. Any idea what i
> can do to fix this?
> 
> 
> import json
> 
> stock_json= json.load(open("stock.json"))
> queue_json= json.load(open("queue.json"))
> 
> queue=[
> ["James Bruce", "Bananas"],
> ["Katherine Newton", "Bananas"],
...
> ]
> 
> stock={
> "Apples": 14,
> "Bananas": 14,
> "Pineapples": 0,
> "Pears": 8
> }
> 
> for i in queue:

This should work OK but by convention programmers use single
letter names like i to indicate indeces or other temporary
integers or characters. It might be better to use a more
descriptive name than i for your data.

> if stock[i[1]]>0:
> print("Gave {} to {}".format(i[1],i[0]))
> else:
> print("Could not give {} to {}".format(i[1],i[0]))

But the loop should work and display the appropriate messages.

> def total_stock(fruit):
> total=0
> for i in fruit:
> if stock[i]>0:
> stock[i]=stock[i]-1
> return total

Notice that this defines a function but...

> print stock

You never call it. Instead you print stock which
is just your original list. Oncwe you have defined
the function you need to explicitly call it for it
to do anything.

Notice too that in your function you use i as a
key into stock. That implies that fruit is a
list of strings(ie names of fruit) - is that
going to be true? Or will it be the pairs
from the queue data?In which case you need
to extract the fruit name.

HTH
-- 
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] Matplotlib scatterplot help

2018-04-30 Thread Alan Gauld via Tutor
On 30/04/18 16:57, Daniel Bosah wrote:
> I have a function in which returns scatterplot of a Isomap

Below you show a function that creates a scatterplot
but it does not return anything. Sorry to nitpick but correct
terminology is quite important in understanding what you
are trying to achieve.

> Below is the function to turn my Isomap values to a 3D scatterplot :
> 
> def Isomap(tfidf):
>   jon = pd.read_csv(tfidf)
>   le = preprocessing.LabelEncoder()
>   tims = jon.apply(le.fit_transform)
>   iso = manifold.Isomap(n_neighbors=2, n_components=3)
>   john = iso.fit_transform(tims)
>   fig = plt.figure()
>   ax = fig.add_subplot(111, projection='3d')
>   use_colors = 'rybg'
>   ax.scatter(john[:,0], john[:,1],john[:,2],color=use_colors,alpha=.5) #
> x,y,z coord. jon 1-3
>   plt.title('Isomap of candiates')
>   plt.xlabel('x')
>   plt.ylabel('y')
>   plt.show()
>   plt.savefig('isomap.png')
> 
> The problem is that I usually only get one color returned. 

Do you mean displayed? Nothing is returned (or to be
pedantic, None is returned)

> get the code to print out 4 colors, 

Again nothing is printed. Are you talking about the
displayed scatterplot?

> I'm not sure how to get those colors
> to  correspond to the four web articles.

Sadly neither do I, you need a mmatplotlib user
to answer that bit. The only reference to
colors I can see is the line

use_colors - 'rybg'

Which I assume stands for red, yellow, blue,green?
I further assume that the order is significant?
But I'm only guessing...


-- 
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