use*_*636 4 python string list
如果我有变量x和y,使得:
x 总是一个字符串y 可以是字符串或字符串列表我该如何创建列表z == [x, <all elements of y>]?
例如:
x = 'x'
y = 'y'
# create z
assert z == ['x', 'y']
Run Code Online (Sandbox Code Playgroud)
x = 'x'
y = ['y', 'y2']
# create z
assert z == ['x', 'y', 'y2']
Run Code Online (Sandbox Code Playgroud)
Dav*_*son 10
z = [x] + (y if isinstance(y, list) else [y])
Run Code Online (Sandbox Code Playgroud)
通常我会避免使用y字符串或列表,但这似乎是不必要的.