Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python中的字典替换列表中的字符_Python_List_Dictionary_Replace_Key - Fatal编程技术网

如何使用python中的字典替换列表中的字符

如何使用python中的字典替换列表中的字符,python,list,dictionary,replace,key,Python,List,Dictionary,Replace,Key,我想编写一个代码,使用字典以有效的方式替换列表中的某些字符 如果我有: key = {'a':'z','b':'y','c':'x'} List = ['a','b','c'] 如何获得输出 zyx 编辑以澄清。我想要的输出是真的 随机变量或某物=['z','y','x'] 我道歉 如果dict中没有键,[key[x]for x in List]会起作用吗?在python中,键和List都是可以与现有对象或方法冲突的词(dict.keys()和List对象),因此为了获得最佳实践,我将它们分别

我想编写一个代码,使用字典以有效的方式替换列表中的某些字符

如果我有:

key = {'a':'z','b':'y','c':'x'}
List = ['a','b','c']
如何获得输出

zyx

编辑以澄清。我想要的输出是真的

随机变量或某物=['z','y','x'] 我道歉


如果dict中没有键,[key[x]for x in List]会起作用吗?

在python中,键和List都是可以与现有对象或方法冲突的词(
dict.keys()
List
对象),因此为了获得最佳实践,我将它们分别替换为k和lst:

[k[x] for x in lst]

使用
get
join

>>> ''.join(key.get(e,'') for e in List)
'zyx'
如果“替换”的意思是按照原始列表元素的顺序将列表更改为dict的值,则可以执行以下操作:

>>> List[:]=[key.get(e,'') for e in List]
>>> List
['z', 'y', 'x']
如果您只想将其打印为字符串,则:

print(*[key.get(x,"No_key") for x in List],sep="")

#### Output ####

zxy
以防您需要不加入的解决方案

ss = ''
def fun_str(x):
    global ss
    ss = ss + x
    return(ss)
print([fun_str(x) for x in List][-1])

#### Output ####

zxy

这些标识符既不是现有标识符的阴影,也不是关键字
keys
是某些类的方法名,并且存在
typing.List
,但这两者都不影响此功能。你说得对,我说错了。为清晰起见进行了编辑,谢谢请澄清:您的输出似乎是一个字符串。在这种情况下,“替换”是什么意思?如果列表中的条目在dict中没有相应的键,该怎么办<代码>['a','b','w']?为清晰起见进行了编辑。抱歉,我必须习惯于在问题中指定内容。我正要用
键回答问题。获取(e,e)
,因此我想我们应该确定提问者想要什么,以防口述中没有键。谢谢你的帮助,我将模糊的问题编辑得更准确。输出=['z','y','x']显然不是OP想要的(
['z','y','x']
不是
zyx
),解决方案的核心与公认的答案相同。那么,这有什么好处呢?他已经清楚地提到了要在列表中作为输出。以你的评论为例,反馈已经编辑了一些代码,也许这会有所帮助。你是对的。“我想要的输出是真的”语句仅出现在突出显示的“如何才能获得输出”zyx之后。无论如何,你的解决方案并不是新的-仅供参考,它被标记为“低质量帖子”。我想现在好多了。
ss = ''
def fun_str(x):
    global ss
    ss = ss + x
    return(ss)
print([fun_str(x) for x in List][-1])

#### Output ####

zxy