英文:
In Python how can I assign a set of random values as value pair to a dictionary data structure?
问题
import random
def sample() -> None:
file_name = "something" + '.txt'
with open('C:/Users/xyz/PycharmProjects/some/{0}'.format(file_name), 'w') as f:
shop_list = [Fruit_shop, Games_shop, Dress_Shop]
random.shuffle(shop_list) # Shuffle the list for random order
for i, random_shop in enumerate(shop_list, start=1):
f.write(f'{i}st Iteration -\n\n')
for key, value in random_shop.items():
f.write(f' {key} {{\n')
for key1, value1 in value.items():
f.write(f' {key1} := {value1}\n')
f.write(f' }}\n')
f.write(f'\n')
Fruit_shop = {
'Fruits {':{
'A':'Apple',
'B':'Ball'
}
}
Games_shop = {
'Games {':{
'A':'Cricket',
'B':'Football'
}
}
Dress_Shop = {
'Dresses {':{
'A':'Jeans',
'B':'Shirts'
}
}
sample()
你期望的输出如下:
1st Iteration -
Fruits {
A := Apple
B := Ball
}
2st Iteration -
Games {
A := Cricket
B := Football
}
3st Iteration -
Dresses {
A := Jeans
B := Shirts
}
英文:
import random
def sample() -> None:
file_name = "something" + '.txt'
with open('C:/Users/xyz/PycharmProjects/some/{0}'.format(file_name), 'w') as f:
shop_list = [Fruit_shop, Games_shop, Dress_Shop]
random_shop = random.choice(shop_list)
for key, value in random_shop.items():
f.write(key + '\n')
for key1, value1 in value.items():
f.write(f"{key1} :={value1}\n")
f.write(f"{'}'}")
f.write(f"\n\n")
Fruit_shop = {
'Fruits {':{
'A':'Apple',
'B':'Ball'
}
}
Games_shop = {
'Games {':{
'A':'Cricket',
'B':'Football'
}
}
Dress_Shop = {
'Dresses {':{
'A':'Jeans',
'B':'Shirts'
}
}
sample()
Expecting an output something like this:-
1st Iteration -
Fruits {
A :=Apple
B :=Ball
}
2nd iteration -
Games{
A :=Jeans
B :=Shirts
}
3rd itertation -
Dresses{
A :=Cricket
B :=Football
}
Whenever the above code is run, with each iteration I want the value of A to be either Apple or Cricket or Jeans, similarly B: Ball or Football or Shirts, I think we need to use the random module here, can any one help me how to do that please? Thanks in Advance!
答案1
得分: 1
以下是已翻译的代码部分:
import random
shops = ["Fruit", "Games", "Dresses"]
productA = ["Apple", "Jeans", "Cricket"]
productB = ["Ball", "Shirts", "Football"]
def sample(shops, productA, productB, outputfile="something.txt"):
with open(outputfile, "w") as fp:
shop = random.choice(shops) # 随机选择商店
prodA = random.choice(productA) # 随机选择产品A
prodB = random.choice(productB) # 随机选择产品B
shopdict = {"A": prodA, "B": prodB}
fp.write(shop)
fp.write("{\n")
for key, value in shopdict.items():
fp.write(f"{key} :={value}\n")
fp.write("}\n")
sample(shops, productA, productB)
英文:
I think the following should do what you are after:
import random
shops = ["Fruit", "Games", "Dresses"]
productA = ["Apple", "Jeans", "Cricket"]
productB = ["Ball", "Shirts", "Football"]
def sample(shops, productA, productB, outputfile="something.txt"):
with open(outputfile, "w") as fp:
shop = random.choice(shops) # pick random shop
prodA = random.choice(productA) # pick random product A
prodB = random.choice(productB) # pick random product B
shopdict = {"A": prodA, "B": prodB}
fp.write(shop)
fp.write("{\n")
for key, value in shopdict.items():
fp.write(f"{key} :={value}\n")
fp.write("}\n")
sample(shops, productA, productB)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论