E 'questo quello che stai cercando?
#!/usr/bin/python
dt={'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3}
st=""
for key,val in dt.iteritems():
st = st + key + str(val)
print st
uscita: q5w3d2g2f2
O questo?
#!/usr/bin/python
dt={'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3}
dt=sorted(dt.iteritems())
st=""
for key,val in dt:
st = st + key + str(val)
print st
uscita: d2f2g2q5w3
Esempio con join
:
#!/usr/bin/python
adict=dt={'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3}
' '.join('{0}{1}'.format(key, val) for key, val in sorted(adict.items()))
uscita: 'd2 f2 g2 q5 w3'
Come si desidera che la stringa venga formattata? – Amber
Puoi spiegare l'input e l'output? Stai cercando di creare una stringa dal dizionario 'Out [25]: {'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3} ' – shibly
I voglio l'output come d2f2g2q5w3 – TechacK