Making different things equal

M

M. Clift

Hi All,

Back again...

This code works nicely giving a list of names with no name repeated at a
space of 3 i.e. you won't get 'Bob','Bob' or 'Bob','Sue','Bob'.

What I'd like to do is add a condition to it which says that although the
names are different 'Bob' and 'Mary' are equal and so it won't generate
'Bob', 'Rita','Mary'

I've tried various ways to do it, but with no joy. Any help appreciated.

Thanks, M

<code>

from random import *

Name_Number = xrange(int(raw_input("Choose number of Names (1-20)? ")))

state = [None,None]

nextName = {'Bob':['Rita','Sue'],\
'Rita':['Mary','Sue','Bob'],\
'Sue':['Rita','Mary','Bob'],\
'Mary':['Sue','Rita']}

Name_List = []

tmp = choice(('Bob','Rita','Sue','Mary))

for x in Name_Number:
print state
while tmp in state[0:2]:
tmp = choice(nextName[Name_List[-1]])
print tmp, ", ",
print
print "Name ",x+1," is ", tmp
Name_List.append(tmp)
state[x%2] = tmp

print Name_List

<code>
 
W

wes weston

M. Clift said:
Hi All,

Back again...

This code works nicely giving a list of names with no name repeated at a
space of 3 i.e. you won't get 'Bob','Bob' or 'Bob','Sue','Bob'.

What I'd like to do is add a condition to it which says that although the
names are different 'Bob' and 'Mary' are equal and so it won't generate
'Bob', 'Rita','Mary'

I've tried various ways to do it, but with no joy. Any help appreciated.

Thanks, M

<code>

from random import *

Name_Number = xrange(int(raw_input("Choose number of Names (1-20)? ")))

state = [None,None]

nextName = {'Bob':['Rita','Sue'],\
'Rita':['Mary','Sue','Bob'],\
'Sue':['Rita','Mary','Bob'],\
'Mary':['Sue','Rita']}

Name_List = []

tmp = choice(('Bob','Rita','Sue','Mary))

for x in Name_Number:
print state
while tmp in state[0:2]:
tmp = choice(nextName[Name_List[-1]])
print tmp, ", ",
print
print "Name ",x+1," is ", tmp
Name_List.append(tmp)
state[x%2] = tmp

print Name_List

<code>
M,
The debugging prints are changed, a tick after Mary is
added so it will run, and a few comments are added.
'still can't figure out what you want to do.
wes





from random import *

Name_Number = xrange(int(raw_input("Choose number of Names (1-20)? ")))

state = [None,None]

nextName = {'Bob':['Rita','Sue'],\
'Rita':['Mary','Sue','Bob'],\
'Sue':['Rita','Mary','Bob'],\
'Mary':['Sue','Rita']}

Name_List = []

tmp = choice(('Bob','Rita','Sue','Mary')) #after Mary ' missing

for x in Name_Number:
print 'state',state
print 'tmp1',tmp
while tmp in state[0:2]: #or for x in range(2):
tmp = choice(nextName[Name_List[-1]])
print 'tmp2',tmp
print "Name ",x+1," is ", tmp
Name_List.append(tmp)
state[x%2] = tmp

print 'Name_List',Name_List
 
M

M. Clift

Hi Wes,

Thanks for looking at this! I appreciate your effort.

The system works very well, but I want it to see Bob as being the same thing
as Mary. So for example it won't give [Bob, whoever, Mary] as that would be
the same as [Mary, whoever, Mary] or [Bob, whoever, Bob].

M
 
W

wes weston

M. Clift said:
Hi Wes,

Thanks for looking at this! I appreciate your effort.

The system works very well, but I want it to see Bob as being the same thing
as Mary. So for example it won't give [Bob, whoever, Mary] as that would be
the same as [Mary, whoever, Mary] or [Bob, whoever, Bob].

M


M,
>>> list = ['Bob', 'whoever', 'Mary']
>>> synonyms = ['Bob', 'Mary']
>>> if synonyms[0] in list and synonyms[1] in list:
.... print "synonyms"
....
synonyms
>>>
>>> list.remove(synonyms[1])
>>> list ['Bob', 'whoever']
>>>

wes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top