简体   繁体   English

Python - For 循环不会用它的匹配值替换每个字典键......只合作字典中的最后一个键和值

[英]Python - For loop will not replace every dictionary key with it's matching value... Only cooperating for last key & value in dictionary

So I have some code to make a module and I need help utilizing a for loop with .replace() in it.所以我有一些代码来制作一个模块,我需要帮助使用一个带有.replace()for循环。 The problem is that when I loop through variables , and use .replace(k, v) , my code will only replace the last key with it's matching last value in the dictionary.问题是,当我遍历variables并使用.replace(k, v) ,我的代码只会用它匹配字典中的最后一个值来替换最后一个键。 This leaves all the previous keys left in the output without being replaced with their matching value... I need every key to be replaced with every value, and it will only comply by doing the last key in the dict这将所有先前的键留在输出中,而不会被替换为其匹配的值......我需要用每个值替换每个键,并且它只会通过执行 dict 中的最后一个键来遵守

variables = {'font1': 'Harlow Solid', 'grey': '#454545', 'font2': 'mistral'}

style_sheet ='''/* Pyle Sheets */
hashone{
    font: Italic 70px font1;
}
hashtwo{                       # scroll down to see 'for' loop in question
    font: bold 45px font2;     # scroll down to see 'for' loop in question
}
hashthree{
    font: Italic 25px Harlow Solid;
}
dotsample{
    text-align: center;
}
hashbodyid{
    background: grey;
}
'''
replace_num = len(variables.keys()) # <--for loop/.replace(k, v) function
for k, v in variables.items():      # <--for loop/.replace(k, v) function
    content = style_sheet.replace(k, v, replace_num) # <--- here it is...

looped_content = str(content)
id_content = looped_content.replace("hash", "#")
clss_idContent = id_content.replace("dot", ".")
#------------
print(clss_idContent) # view output

在替换的源字符串( style_sheetstyle_sheet您在循环中一次又一次地覆盖content

There is a logical mistake in your code. 您的代码中存在逻辑错误。 Let's analyze your code. 让我们分析一下您的代码。

Suppose style_sheet equals to aabbcc , and variables equals to {"a": 1, "b" : 2, "c": 3} . 假设style_sheet等于aabbccvariables等于{"a": 1, "b" : 2, "c": 3}

What will happen during loop? 循环期间会发生什么?

First loop: content = "aabbcc".replace("a", 1, 3) , so content = "11bbcc" . 第一个循环: content = "aabbcc".replace("a", 1, 3) ,所以content = "11bbcc"

Second loop: content = "aabbcc".replace("b", 2, 3) , so content = "aa22cc" . 第二个循环: content = "aabbcc".replace("b", 2, 3) ,所以content = "aa22cc"

Last loop: content = "aabbcc".replace("c", 3, 3) , so content = "aabb33" . 最后一个循环: content = "aabbcc".replace("c", 3, 3) ,所以content = "aabb33"

So do you notice where is the key point? 那么,您注意到关键点在哪里吗?

You should update original string during every loop too! 您也应该在每个循环中更新原始字符串!

You can change your code like the following: 您可以像下面这样更改代码:

content = style_sheet
for k, v in variables.items():
    content = content.replace(k, v, replace_num)  #update original string

What you could do in the loop is instead of setting it to a variable you could append it to aa list, or do update on a dictionary. 您可以在循环中执行的操作是代替将其设置为变量,而可以将其附加到列表中,或者对字典进行更新。

For instance, since looped_content is a string, you could initialize it before the loop and append content to it each time in the loop. 例如,由于looped_content是一个字符串,因此您可以在循环之前对其进行初始化,并在每次循环中将内容附加到该字符串。 Or if it is a dict, update it. 或者,如果是字典,请对其进行更新。 This would prevent content from being overwritten each time, as mentioned by another poster. 如另一位张贴者所述,这将防止内容每次被覆盖。

looped_content = ""



replace_num = len(variables.keys()) # <--for loop/.replace(k, v) function
for k, v in variables.items():      # <--for loop/.replace(k, v) function
    content = style_sheet.replace(k, v, replace_num) # <--- here it is...
    looped_content.append(content)

id_content = looped_content.replace("hash", "#")
clss_idContent = id_content.replace("dot", ".")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM