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

Search Discussions

  • Wes weston at Aug 22, 2004 at 3:46 pm

    M. Clift wrote:
    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. Clift at Aug 22, 2004 at 4:18 pm
    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
  • Wes weston at Aug 23, 2004 at 3:53 am

    M. Clift wrote:
    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
  • M. Clift at Aug 23, 2004 at 3:18 pm
    Cheers Wes,

    That works a treat.

    Thanks,

    Malcolm

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 22, '04 at 2:11p
activeAug 23, '04 at 3:18p
posts5
users2
websitepython.org

2 users in discussion

M. Clift: 3 posts Wes weston: 2 posts

People

Translate

site design / logo © 2022 Grokbase