jeudi 1 mars 2018

Finding matching String in two Lists of Lists

I was trying to make a function so that random values from the 'next' list are assigned to 'One' and 'Two'. If the value that is assigned to 'One' and 'Two' coincide with the position of the values [i][0] and [i][2] in 'roundTwo' then 'One' and 'Two' are assigned new values.

After this the values 'one' and 'two' are then removed from the 'next' list.

As an example, if 'one' was to be assigned 'J090' and 'two' was to be assigned 'MA78', then they would both be assigned new values, as in 'roundTwo' there is:

['JO90', '1', 'MA78', '8']

However, if 'one' was assigned 'J090' and 'two' was assigned 'B208' it would be correct and the program would then continue to remove 'J090' and 'B208' from the 'next' list.

Below is an example of my code, apologies if what I have written doesn't make too much sense here, so just ask if you think it needs editing!

next = ['JO90', 'MA78', 'FE29', 'HT27', 'EQ37', 'BF50', 'LJ93', 'UT21', 'KJ40', 'WE82', 'WQ28', 'BV98', 'FE32', 'EF10', 'SA14', 'SP16']
roundTwo = [['JO90', '1', 'MA78', '8'], ['B208', '2', 'DF18', '3'], ['PD06', '5', 'BS07', '7'], ['SA14', '4', 'SP16', '7']]
one = next[random.randint(0, len(next) - 1)]
two = next[random.randint(0, len(next) - 1)]
for i in next:
    if one in roundTwo[i][0] and two in roundTwo[i][2]:  
    one = roundTwo[random.randint(0, len(roundTwo) - 1)]
    two = roundTwo[random.randint(0, len(roundTwo) - 1)]
if one in next:
    next.remove(one)
if two in next:
    next.remove(two)

I am getting this error when running through and I am not quite sure how to make it work correctly:

TypeError: list indices must be integers or slices, not str

Thanks!




Aucun commentaire:

Enregistrer un commentaire