I am running the following code, but I encounter an error:
name_map = dict(zip(face_names,
[e+'.png' for e in
[face_names[0]+(label.split()[0])]
if label=='suit'
elif label != 'suit' face_names
my error:
SyntaxError: invalid syntax
from 'elif' on, it fails.
I want ...if label == 'suit'
name_map={'john':'johnsuit.png''}
otherwise
name_map={'john':'john.png'}
Try or instead and always wrap independent boolean terms in brackets. This works:
l = [1,2,3]
[x for x in l if (x == 2) or (x == 1)]
name_map = dict(zip(face_names,
[e+'.png' for e in
[face_names[0]+(label.split()[0])]
if label=='suit'
elif label != 'suit' face_names
dict
comprehension, the zip
function, and the list comprehension (2nd argument to zip
) are all still open.if
) is illegal: look up the syntax; among other things, elif
is not part of such an expression. Let's see whether I understand this: if the label is "suit", you want to construct a file name; if not, you simply want to use face_names
as the file name. Then let's write that part:
[face_names[0] + label.split()[0]] if label == "suit"
else face_names
That is your expression as you wrote it. Within your entire statement, this might become
name_map = dict(zip(face_names,
[e+'.png' for e in
[ face_names[0] + label.split()[0] ]
] if label == "suit"
else face_names
)
)
Thus, if the label == "suit", you get a single element in the second list; otherwise, that argument is a list of face_names
, each with ".png" appended. If this is not what you wanted, please update the posting appropriately.
Update per OP comments
It appears that your issue is whether to add "suit" to the end of each file name. In this case, the logic is significantly simpler:
face_names = ["john"]
label = "suit"
name_map = dict(zip(face_names,
[e + ('suit.png' if label=="suit" else ".png")
for e in face_names]
)
)
print(name_map)
This produces:
{'john': 'johnsuit.png'}
If I change label to "not_a_suit"
, we get
{'john': 'john.png'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.