嘿,我有一个问题解决一个问题 ,看起来像这样:代码审查:Python列表拼接元素串
输出正是我需要的,但我不知道是心不是一个更优雅的 的方式做那?
list = [["key189","0","apples"],
["key622","1","bananas"],
["key233","2","bacon"],
["key454","3","bread"],
["key35","4","jam"],
["key6","5","coffee"]]
for e in list:
if e[0] == "key622":
key622 = e[2]
if e[0] == "key189":
key189 = e[2]
if e[0] == "key35":
key35 = e[2]
if e[0] == "key454":
key454 = e[2]
if e[0] == "key233":
key233 = e[2]
if e[0] == "key6":
key6 = e[2]
string_form = "|".join([key6, key35, key233, key189, key622, key454])
print(string_form)
# prints coffee|jam|bacon|apples|bananas|bread
我投票作为题外话,因为这个问题属于对姊妹网站['CodeReview'关闭这个问题](https://codereview.stackexchange.com/) –
你加入的顺序是相当随机的。 – schwobaseggl
这是一个非常糟糕的数据结构,你正在使用,我认为你的_key_变量是唯一的,所以我建议你使用'Dictionary' – Arman