[Tutor] Issue with set_value function

2017-02-13 Thread Yap Gerald
Hi Tutors,

I have a problem with the functions set_value which I could not find a
solution on the net. In my output below, I was able to set_value at the
first loop but not the second loop. The different check using print could
not identify what was the problem. I have attached my testset, function and
output below. Really appreciate if anyone can help me with this issue.
Thanks!

Regards
Gerald

Dataset plus processing:
Test_data as attached.
test_df = pd.read_csv("kargertest.txt", header = None, names = ["raw"])
test_df["raw_list"] = test_df["raw"].apply(lambda x : x.split('\t'))
test_df["vertexes"] = test_df["raw_list"].apply(lambda x : x[0])
test_df["edges"] = test_df["raw_list"].apply(lambda x : x[1:])

This is my function on min cut algorithm.


def min_cut(df,edges):
df2 = copy.deepcopy(df)
for i in range(len(df)):
print "df at start of loop is ", df2
if len(df2) == 2:
print len(df2)
print df2["edges"]
return len(df2["edges"].values[1])
else:
contract_edge = pick_rand(edges)
print "contract_edge is ", contract_edge
row_a = df2[df2['vertexes'] == contract_edge[0]]
row_b = df2[df2['vertexes'] == contract_edge[1]]

index_a = row_a.index[0]
index_b = row_b.index[0]

edges_a = row_a['edges'].tolist()[0]
edges_b = row_b['edges'].tolist()[0]

edges_a = filter(lambda a : a != contract_edge[1], edges_a)
edges_b = filter(lambda a : a != contract_edge[0], edges_b)

edges_c = edges_a + edges_b
print "edges_c is ", edges_c

print "index a of field edges is ", df2["edges"][index_a]
df2['edges'][index_a] = edges_c
print "df2 after setting value is", df2
df2 = df2.drop(index_b)

for x in range(len(df2)):
temp_list = df2.iloc[x]["edges"]
while contract_edge[1] in temp_list:
temp_list.remove(contract_edge[1])
temp_list.append(contract_edge[0])
df2["edges"][x] = temp_list

edges = filter(lambda a : a != contract_edge, edges)
edges = filter(lambda a : a != (contract_edge[1],
contract_edge[0]), edges)

for i in range(len(edges)):
if edges[i][0] == contract_edge[1]:
edges[i] = (contract_edge[0],edges[i][1])
if edges[i][1] == contract_edge[1]:
edges[i] = (edges[i][0],contract_edge[0])

edges = filter(lambda a : a != (contract_edge[0],
contract_edge[0]), edges)


My output is:

df at start of loop is

raw  raw_list vertexes  edges
0 1\t2\t3 [1, 2, 3]1 [2, 3]
1  2\t1\t3\t4  [2, 1, 3, 4]2  [1, 3, 4]
2  3\t1\t2\t4  [3, 1, 2, 4]3  [1, 2, 4]
3 4\t2\t3 [4, 2, 3]4 [2, 3]

contract_edge is  ('4', '2')

edges_c is  ['3', '1', '3']

index a of field edges is  ['2', '3']

df2 after setting value is

raw  raw_list vertexes  edges

0 1\t2\t3 [1, 2, 3]1 [2, 3]
1  2\t1\t3\t4  [2, 1, 3, 4]2  [1, 3, 4]
2  3\t1\t2\t4  [3, 1, 2, 4]3  [1, 2, 4]
3 4\t2\t3 [4, 2, 3]4  [3, 1, 3]
df at start of loop israw  raw_list vertexes  edges
0 1\t2\t3 [1, 2, 3]1 [3, 4]
2  3\t1\t2\t4  [3, 1, 2, 4]3  [1, 4, 4]
3 4\t2\t3 [4, 2, 3]4  [3, 1, 3]

contract_edge is  ('4', '3')

edges_c is  ['1', '1']

index a of field edges is  ['3', '1', '3']

df2 after setting value is


raw  raw_list vertexes  edges
0 1\t2\t3 [1, 2, 3]1 [3, 4]
2  3\t1\t2\t4  [3, 1, 2, 4]3  [1, 4, 4]
3 4\t2\t3 [4, 2, 3]4  [3, 1, 3]

df at start of loop is


raw   raw_list vertexes  edges
0  1\t2\t3  [1, 2, 3]1 [4, 4]
3  4\t2\t3  [4, 2, 3]4  [1, 4, 4]

2
0   [4, 4]
3[1, 4, 4]
1[1, 4, 4]
Name: edges, dtype: object
1   2   3
2   1   3   4
3   1   2   4
4   2   3___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Can we talk with a Real Time Clock hardware by Python?

2017-02-13 Thread Thanh Tuan Le
Dear Mr, Mrs,

During working with Python, I have a matter that I am finding a solution. I
am not an experience programmer in Python, so may I raise this question to
you? I really appreciate for any help.

I have a board that have a Real Time Clock (RTC) chip on that board. I need
to communicate with that RTC chip by Python.

Following is my hardware info.
- Board from Variscite:
http://www.variscite.com/products/single-board-computers/dt6customboard
- RTC chip on board: ISL12057
- OS: Yocto

I need to do 2 basic operations for this RTC chip
- Write/Read to registers of that RTC.
- Detect the Interrupt at each time it asserted.

Do we have any technique or library of Python that can do the above
operations?

Thanks.

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


[Tutor] GUI for ANSI colors

2017-02-13 Thread Freedom Peacemaker
Hi tutor,
I'm trying to put my script with randomly colored letters (ANSI escape
code) into GUI but cant find any. I've tried tkinter but it isn't good
choice. Then i was searching for capabilities of PyQt4 again without
success. Thats why im writing this message. Is there any GUI that can print
word with randomly colored letters?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can we talk with a Real Time Clock hardware by Python?

2017-02-13 Thread Alan Gauld via Tutor
On 13/02/17 06:44, Thanh Tuan Le wrote:
> I have a board that have a Real Time Clock (RTC) chip on that board. I need
> to communicate with that RTC chip by Python.
> 
> Following is my hardware info.
> - Board from Variscite:
> http://www.variscite.com/products/single-board-computers/dt6customboard
> - RTC chip on board: ISL12057
> - OS: Yocto

I know nothing about this specific board or clock.
However, in general you need an API to talk to any
kind of hardware. Boards like this usually provide
a C API - do you have documentation for that?

If thee is a C API there is a good chance that
you can talk to it using the ctypes module.
But ctypes is not easy to use so be prepared
for a lot of trial and error.

It's also possible, if you know C, to build a
library and then wrap it up as a Python module
using SWIG or similar technology. But this is
only an option if you are already comfortable
working in C.

The best option is if a Google search shows
that somebody has already done that work and
made the python module available to download.


> 
> I need to do 2 basic operations for this RTC chip
> - Write/Read to registers of that RTC.
> - Detect the Interrupt at each time it asserted.

It would also help to know more about your environment.
What is your host computing platform and OS?
How is the board attached to the computer?
(USB, PCI, jumper, soldered?)
And which version of Python are you using?


-- 
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] Issue with set_value function

2017-02-13 Thread Peter Otten
Yap Gerald wrote:

> I have a problem with the functions set_value which I could not find a
> solution on the net. In my output below, I was able to set_value at the
> first loop but not the second loop. The different check using print could
> not identify what was the problem. I have attached my testset, function
> and output below. Really appreciate if anyone can help me with this issue.

I'm sorry, your description does not give me an idea what your problem might 
be. There's not even a set_value() function in your code sample...

Please make the code you provide self-contained so that we can run it 
without modifications. The script should be as small as possible. If one 
step of your code produces the expected output give a script that starts 
with that output and only includes the failing step.

Show both the output you get and the expected output you want. Rather than 
providing arbitrary debugging data try to concentrate on the relevant 
parts.


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


Re: [Tutor] Issue with set_value function

2017-02-13 Thread Alan Gauld via Tutor
On 13/02/17 07:23, Yap Gerald wrote:

> I have a problem with the functions set_value which I could not find a
> solution on the net.

This is very confusing. You don't use set_value() anywhere
in the code you posted? You don't describe what the actual
problem with set_ value() is.

And most of all you don't tell us what set_value() is - its
not a standard Python function.

In short, it's impossible for us to guess what set_value
is, how you use it or what the problem is.

Simply posting input and output data and a random function
doesn't help us at all. We are not familiar with your
work area, we don't know the context. We have no idea
what the output should be. You need to help us to help you.

One thing I would say is that it looks sufficiently
technical that you should probably be using SciPy/NumPy
if you aren't already; that may be where some of your
functions come from?

-- 
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] GUI for ANSI colors

2017-02-13 Thread Alan Gauld via Tutor
On 13/02/17 09:40, Freedom Peacemaker wrote:

> I'm trying to put my script with randomly colored letters (ANSI escape
> code) into GUI but cant find any. I've tried tkinter but it isn't good
> choice. 

Why not? It can certainly do what you want. As can virtually
any other GUI toolkit. But programming GUIs is much harder
than programming the console regardless of toolkit choice.

Here is a sample Tkinter program that displays text in
different colors:

###
from Tkinter import *

n = 0
top = Tk()
t = Text(top, width=25, height=2)
t.pack()

# set up tags for the colors
colors = ['red','blue','green']
for col in colors:
t.tag_config(col, foreground=col)

def addWords():
n = 0
for word in "Hello ", "bright ", "world ":
t.insert(END,word,colors[n])  #use the color tags
n +=1

Button(top,text="Add words",command=addWords).pack()

top.mainloop()
##

Most other GUIs will be similar.

-- 
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] Resending question with smaller file

2017-02-13 Thread Lisa Hasler Waters
Hello Python Tutor,

We are trying to use the random function in the Tkinter module in
PyCharmEDU 3.5. (on Macs running 10.11.6). However, we get a number of
error messages:

Here's the code:

from tkinter import *
import random
tk = Tk()

canvas = Canvas(tk, width=400, height=400)
canvas.pack()


def random_randrange(height):
pass


def random_randrange(width):
pass


def random_rectangle(width, height):
x1 = random_randrange(width)
y1 = random_randrange(height)
x2 = x1 + random.randrange(width)
y2 = y1 + random.randrange(height)
canvas.create_rectangle(x1, y1, x2, y2)
random_rectangle(400, 400)

tk.mainloop()

Here are the error messages:

Traceback (most recent call last):
  File "/Users/lwaters/PycharmProjects/tkinternew/rectangles.py", line
2, in 
import random
  File "/Users/lwaters/PycharmProjects/tkinternew/random.py", line 17,
in 
randomRects(100)
  File "/Users/lwaters/PycharmProjects/tkinternew/random.py", line 10,
in randomRects
x1 = random.randrange(150)
AttributeError: module 'random' has no attribute 'randrange'

Process finished with exit code 1

Your advice is greatly appreciated!

Lisa Waters

-- 
Lisa Waters, PhD
Technology Integration
Middle School Coding
Lower School Digital Literacy
Flint Hill School
703.584.2300
*www.flinthill.org* 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Resending question with smaller file

2017-02-13 Thread Tim Golden

On 13/02/2017 16:06, Lisa Hasler Waters wrote:

Hello Python Tutor,

We are trying to use the random function in the Tkinter module in
PyCharmEDU 3.5. (on Macs running 10.11.6). However, we get a number of
error messages:

Here's the code:

from tkinter import *
import random
tk = Tk()

canvas = Canvas(tk, width=400, height=400)
canvas.pack()


def random_randrange(height):
pass


def random_randrange(width):
pass


def random_rectangle(width, height):
x1 = random_randrange(width)
y1 = random_randrange(height)
x2 = x1 + random.randrange(width)
y2 = y1 + random.randrange(height)
canvas.create_rectangle(x1, y1, x2, y2)
random_rectangle(400, 400)

tk.mainloop()

Here are the error messages:

Traceback (most recent call last):
  File "/Users/lwaters/PycharmProjects/tkinternew/rectangles.py", line
2, in 
import random
  File "/Users/lwaters/PycharmProjects/tkinternew/random.py", line 17,
in 
randomRects(100)
  File "/Users/lwaters/PycharmProjects/tkinternew/random.py", line 10,
in randomRects
x1 = random.randrange(150)
AttributeError: module 'random' has no attribute 'randrange'


You've called your own module "random.py"; call it something else

TJG

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


[Tutor] Help with random in Tkinter

2017-02-13 Thread Lisa Hasler Waters
Hello Python Tutor,

We are trying to use the random function in the Tkinter module in
PyCharmEDU 3.5. However, we get a number of error messages (see screen
shot):
[image: Inline image 1]



It runs fine when we use IDLE. But, we love PyCharmEDU as it is much more
user-friendly and so we hope to run the same programs in it as we can in
IDLE.

We are running PyCharm EDU 3.5 on Macs 10.11.6.

Your advice will be greatly appreciated!

Thanks,

Lisa Waters

-- 
Lisa Waters, PhD
Technology Integration
Middle School Coding
Lower School Digital Literacy
Flint Hill School
703.584.2300
*www.flinthill.org* 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Resending question with smaller file

2017-02-13 Thread Alan Gauld via Tutor
On 13/02/17 16:06, Lisa Hasler Waters wrote:

> We are trying to use the random function in the Tkinter module 

> from tkinter import *
> import random

This looks for the random module and tries your local folder first.
If you have a file called random.py it tries to import that.

Based on your error messages there is a file is called random.py
so it tries to import it rather than the standard library version... Try
renaming your file to something more descriptive of what
makes it different from the library version.

> tk = Tk()
> 
> canvas = Canvas(tk, width=400, height=400)
> canvas.pack()
> 
> 
> def random_randrange(height):
> pass
> 
> 
> def random_randrange(width):
> pass

These two functions are the same since Python doesn't understand
the parameter names it just sees

def aFunctionName(aParameterName)

so the second def simply overwrites the first.
What you really mean (I think) is

def random_randrange(aNumber): pass

Although that's still not a very descriptive name...

> def random_rectangle(width, height):
> x1 = random_randrange(width)
> y1 = random_randrange(height)
> x2 = x1 + random.randrange(width)
> y2 = y1 + random.randrange(height)
> canvas.create_rectangle(x1, y1, x2, y2)
> random_rectangle(400, 400)

You call this same function recursively, this will
result in an infinite loop until you run out of memory.
I don't know what you are trying to do but this is
surely not it.

This function never gets called and, since
its not assigned as an event handler, it will
never be executed.


> tk.mainloop()

> Here are the error messages:
> 
> Traceback (most recent call last):
>   File "/Users/lwaters/PycharmProjects/tkinternew/rectangles.py", line
> 2, in 
> import random
>   File "/Users/lwaters/PycharmProjects/tkinternew/random.py", line 17,
> in 
> randomRects(100)

Notice the two files listed above are in the same folder and one
is called random.py


-- 
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 random in Tkinter

2017-02-13 Thread Alan Gauld via Tutor
On 13/02/17 13:30, Lisa Hasler Waters wrote:

> It runs fine when we use IDLE. But, we love PyCharmEDU as it is much more
> user-friendly and so we hope to run the same programs in it as we can in
> IDLE.

That shouldn't make much difference unless PyCharm sets its
default folders differently?

BTW running GUI programs from inside an IDE is always slightly
dodgy, the IDE tends to trap all kinds of things that might
happen when you run the program outside the IDE. Its always
worth testing GUI programs by launching them from the OS
 - eg by double clicking the file in Finder on OSX.

The IDE is fine while coding and debugging but it should
be thought of as a draft only, the final code needs to
be run outside the IDE before you can be sure it works.

-- 
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] Access a .tcl file in python

2017-02-13 Thread Lily ANTONY
Hi,
I have a .tcl file.Does anyone know how to load a .tcl file in to python?I
need to call the .tcl file in
a python program..Thanks!

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


[Tutor] Fwd: Re: GUI for ANSI colors

2017-02-13 Thread Alan Gauld via Tutor
Forwarding to list.

---
Ive tried your suggestion but didnt work. Look this is my code and i
need to print final_word into GUI. Im working on Ubuntu 16.04

#! usr/bin/python3

from random import choice
from string import ascii_letters, digits

chars = ascii_letters + digits
word = ''.join(choice(chars) for i in range(12))

red = '\033[91m'
yel = '\033[93m'
blu = '\033[34m'
grn = '\033[32m'

colors = [red, yel, blu, grn]

final_word = ''.join(choice(colors) + char for char in word)
print(final_word)

2017-02-13 12:28 GMT+01:00 Alan Gauld via Tutor mailto:tutor@python.org>>:

On 13/02/17 09:40, Freedom Peacemaker wrote:

> I'm trying to put my script with randomly colored letters (ANSI escape
> code) into GUI but cant find any. I've tried tkinter but it isn't good
> choice.

Why not? It can certainly do what you want. As can virtually
any other GUI toolkit. But programming GUIs is much harder
than programming the console regardless of toolkit choice.

Here is a sample Tkinter program that displays text in
different colors:

###
from Tkinter import *

n = 0
top = Tk()
t = Text(top, width=25, height=2)
t.pack()

# set up tags for the colors
colors = ['red','blue','green']
for col in colors:
t.tag_config(col, foreground=col)

def addWords():
n = 0
for word in "Hello ", "bright ", "world ":
t.insert(END,word,colors[n])  #use the color tags
n +=1

Button(top,text="Add words",command=addWords).pack()

top.mainloop()
##

Most other GUIs will be similar.

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


[Tutor] Q about .join() Thanks!

2017-02-13 Thread SIJIA CHEN
Dear, all,

I am a python novice and I am wondering if someone in the group could help me 
with a easy question about .join() method!

I find out that the outcome for using .join() on a dictionary is totally 
different than it using on list or string. for example,

  >>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
  >>> print ':'.join(seq4)
  boy:good:doiido:hello
So my question is why the outcome doesn't show sequentially as the same 
sequence of the original seq4?  What pattern do those "keys" of the dictionary 
in the outcome show ?

I would be really appreciate it Thanks!
Sincerely,
Sonia



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


Re: [Tutor] Access a .tcl file in python

2017-02-13 Thread Alan Gauld via Tutor
On 13/02/17 19:37, Lily ANTONY wrote:

> I have a .tcl file.Does anyone know how to load a .tcl file in to python?I
> need to call the .tcl file in
> a python program..Thanks!

In general you can't run code for one language in a
program written in another language. (There are a few
exceptions, usually where one language is written in
the other).

The nearest you can usually get is to execute the
Tcl program using the tcl interpreter (tclsh) from
the OS using a module like subprocess.

However, Tcl and Python do have a connection  via
the Tkinter module and you can call tcl code
using Tkinter like so(Python v3):

import tkinter
tcl = tkinter.Tcl()
tcl.eval("""
puts [expr 4+5]  # or any other arbitrary tcl
""")

So to execute a Tcl file you can read the contents
into a string and then execute that string. Note that
the return value will always be a string.

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] Q about .join() Thanks!

2017-02-13 Thread David Rock

> On Feb 13, 2017, at 12:34, SIJIA CHEN  wrote:
> 
> I find out that the outcome for using .join() on a dictionary is totally 
> different than it using on list or string. for example,
> 
 seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
 print ':'.join(seq4)
>  boy:good:doiido:hello
> So my question is why the outcome doesn't show sequentially as the same 
> sequence of the original seq4?  What pattern do those "keys" of the 
> dictionary in the outcome show ?

Dictionaries (in particular, their keys) are unordered.  You can not rely on 
them to be in a particular sequence.  The reason for this, from a practical 
perspective, is you are expected to ask for the specific key; you should not 
care about the ordering.

Lists, on the other hand, have a specific order by design so will always be in 
the order they were created.

What are you trying to do with join() on a dictionary in the first place?  Is 
there a specific outcome you are trying to get?  It’s unlikely that using join 
on a dictionary is what you actually want.

—
David Rock
da...@graniteweb.com




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


Re: [Tutor] Fwd: Re: GUI for ANSI colors

2017-02-13 Thread Alan Gauld via Tutor
On 14/02/17 00:58, Alan Gauld forwarded:

> red = '\033[91m'
> yel = '\033[93m'
> blu = '\033[34m'
> grn = '\033[32m'

These are indeed the ANSI codes for the colours but ANSI
codes only work in an ANSI terminal and GUIs are not
ANSI terminals. Instead they have their own ideas on
colours and you need to use those. They all support
the standard 16 colours used in ANSI however, so in
your case (for Tkinter at least) just use the names
'red', 'yellow', 'blue' and 'green'

> colors = [red, yel, blu, grn]

colors = ['red', 'yellow', 'blue', 'green']


> final_word = ''.join(choice(colors) + char for char in word)

But again that won't work in a GUI, you need to use the GUIs
way of colouring. In Tkinter that's via tags as per my
previous post, reproduced here).

> ###
> from Tkinter import *
> 
> n = 0
> top = Tk()
> t = Text(top, width=25, height=2)
> t.pack()
> 
> # set up tags for the colors
> colors = ['red','blue','green']
> for col in colors:
> t.tag_config(col, foreground=col)
> 
> def addWords():
> n = 0
> for word in "Hello ", "bright ", "world ":
> t.insert(END,word,colors[n])  #use the color tags
> n +=1
>

You need to change the loop to iterate over the letters
instead of words and choose the tags at random. But that
should be fairly straightforward.

> Button(top,text="Add words",command=addWords).pack()
> 
> top.mainloop()
> ##


-- 
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] Q about .join() Thanks!

2017-02-13 Thread Alan Gauld via Tutor
On 13/02/17 18:34, SIJIA CHEN wrote:
> I find out that the outcome for using .join() on a dictionary is 
> totally different than it using on list or string.

Not really, it just looks like that :-)

> 
>   >>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
>   >>> print ':'.join(seq4)
>   boy:good:doiido:hello
> So my question is why the outcome doesn't show sequentially

That's because dictionaries are not stored sequentially and the
order of retrieval is not guaranteed - it can even change
during the execution of a program so you should never
depend on it. That's because dictionaries are optimised
for random access via the keys not to be iterated over.

You can sort the keys and then pull the values and that will
give you a guaranteed order but simply printing the dictionary
(or joining it as you did) is not reliable.

I've a vague memory that recent versions of Python may have
a special dictionary type that does return items in the order
they were inserted, but I may be mixing that up with
another language... Hopefully someone else can provide
a steer there.

-- 
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] Q about .join() Thanks!

2017-02-13 Thread Danny Yoo
> That's because dictionaries are not stored sequentially and the
> order of retrieval is not guaranteed - it can even change
> during the execution of a program so you should never
> depend on it. That's because dictionaries are optimised
> for random access via the keys not to be iterated over.

Moreover, most implementations *deliberately* randomize their iteration
order to avoid a particular kind of hash collision attack out there in
the wild.  See:

https://en.wikipedia.org/wiki/Collision_attack


https://arstechnica.com/business/2011/12/huge-portions-of-web-vulnerable-to-hashing-denial-of-service-attack/

https://mail.python.org/pipermail/python-dev/2011-December/115116.html

for some details.


> I've a vague memory that recent versions of Python may have
> a special dictionary type that does return items in the order
> they were inserted, but I may be mixing that up with
> another language... Hopefully someone else can provide
> a steer there.

In Python, it's collections.OrderedDict:

https://docs.python.org/3.6/library/collections.html#collections.OrderedDict

Other languages have similar libraries.  Java has LinkedHashMap, for example:

https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html


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