0

I am currently making a system for my robotics team and I have a problem where instead of just printing the team number, I it prints the team number and frc in front of it. I was able to figure it out for just a list of all the teams with [3:], but when I do it for the match's it just removes one of the teams. I think i has something to do with fact that the match numbers are a dictionary. I was wondering if there was a way to remove the frc in a dictionary? Thanks! This is my code:

for match in eventMatches:
    matchNumber = {}
    matchNumber['blue'] = match.alliances.get('blue').get('team_keys')
    eventmatchList.append(matchNumber)
for key in matchNumber.keys():
    worksheet.write_row(row, col, matchNumber[key])
row += 1
row = 1
col = 4
for match in eventMatches:
    matchNumber = {}
    matchNumber['red'] = match.alliances.get('red').get('team_keys')
    eventmatchList.append(sorted(matchNumber))
for key in matchNumber.keys():
    worksheet.write_row(row, col, matchNumber[key])
row += 1
Dan yu
  • 47
  • 4
  • code and problem aren't clear.. can you elaborate – shahaf Apr 01 '18 at 19:49
  • Right now the code is outputting "frc" then the team number. I want to have it so it only outputs the team number, so i tried doing [3:], but it didnt do anything. It was working in a list but these teams are going in a dictionary. Is there a way to remove the "frc" in a dictionary? – Dan yu Apr 01 '18 at 19:52
  • are you saying you have a dict like `dict[somekey] = 'frc34'`and you want to print only '34'? – shahaf Apr 01 '18 at 20:03
  • yes I am thats exactly what i want. – Dan yu Apr 01 '18 at 21:57
  • have you looks at how to this [qa on splitting strings](https://stackoverflow.com/questions/12572362/get-a-string-after-a-specific-substring)? – NonCreature0714 Apr 01 '18 at 22:34

2 Answers2

1

a small example of changing string in dict values, hope it what you wanted...

d = {'key1' : 'frc1', 'key2' : 'frc2', 'key3' : 'frc3'}

for key in d.keys():
  d[key] = d[key].replace('frc', '')

for k, v in d.items():
  print(k, v)
shahaf
  • 4,750
  • 2
  • 29
  • 32
0

You can make use of regular expressions as well. Using regular expressions will replace all alphabets and print only numbers. See sample code below.

 >>import re
 >>re.sub("[^0-9]","","frc123")
 '123'

re.sub(Pattern, Replace string, String to search)

"[^0-9]+" - look for any group of characters that are NOT 0-9. "" - Replace the matched characters with " ".

Make use of this concept in the dictionary. See example below.

>>import re
>>d = {'key1' : 'frc123', 'key2' : 'frc234', 'key3' : 'frc345'}
>>for i in d:
      d[i] = re.sub("[^0-9]","",d[i])
>>print(d)

 {'key1': '123', 'key2': '234', 'key3': '345'}
Aravind
  • 534
  • 1
  • 6
  • 18