JavaScript
x
5
1
Element = ["abc1", "abc2", "abc3", "abc abc abc4", "abc abc5", "abc6"]
2
3
swaps = {'{}'.format(Element[0]): random.choice(('{ff}')).format(ff = Element[1:])}
4
print(swaps)
5
If you run
JavaScript
1
2
1
{'abc1':'f'}
2
Come out like this or
JavaScript
1
2
1
ValueError: Single'}' encountered in format string`
2
It comes out like this
What I want is that the elements except abc1 are inserted as ff and a randomly selected value is output. For example
JavaScript
1
8
1
swaps = {'{}'.format(Element[0]): random.choice(('{ff}')).format(ff = Element[1:])}
2
print(swaps)
3
>>>{'abc1':'abc3'}
4
print(swaps)
5
>>>{'abc1':'abc abc5'}
6
print(swaps)
7
>>>{'abc1':'abc abc abc4'}
8
Advertisement
Answer
Given your desired output, you are looking to create new dictionaries, not string representations of dictionaries, so this will do:
JavaScript
1
2
1
swaps = {Element[0]: random.choice(Element[1:])}
2
If you still need a string representation of the dictionary you can get it with
JavaScript
1
2
1
str(swaps)
2