我有一个像这样的字符串元组列表
['(-315.0, 106.0)\n', '(-179.0, -163.0)\n', '(90.0, 76.0)']
我将如何将其转换为此
[(-315.0, 106.0), (-179.0, -163.0), (90.0, 76.0)]
我在代码中尝试了eval()
,但根本没有改变列表。该代码是
with open("{}.ctd".format(load), "r") as f:
data = f.readlines()
positions = []
for i in range(len(data)):
data[i].rstrip()
print(eval(data[i]))
positions.append(data[i])
print(positions)
draw()
答案 0 :(得分:0)
您可以使用extension UIView {
enum GlowEffect: Float {
case small = 0.4, normal = 2, big = 30
}
func doGlowAnimation(withColor color: UIColor, withEffect effect: GlowEffect = .normal) {
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowRadius = 0
layer.shadowOpacity = 0.8
layer.shadowOffset = .zero
let glowAnimation = CABasicAnimation(keyPath: "shadowRadius")
glowAnimation.fromValue = 0
glowAnimation.toValue = effect.rawValue
glowAnimation.fillMode = .removed
glowAnimation.repeatCount = .infinity
glowAnimation.duration = 2
glowAnimation.autoreverses = true
layer.add(glowAnimation, forKey: "shadowGlowingAnimation")
}
}
:
ast.literal_eval
答案 1 :(得分:0)
data = ['(-315.0, 106.0)\n', '(-179.0, -163.0)\n', '(90.0, 76.0)\n']
result = []
for item in data:
exec('result.append({})'.format(item), {'result':result})
print(result)
或
for item in data:
result.append(eval(item))
print(result)
输出
[(-315.0,106.0),(-179.0,-163.0),(90.0,76.0)]
答案 2 :(得分:0)
将元组列表转换为列表的Python代码
# List of tuple initialization
lt = [(-315.0, 106.0), (-179.0, -163.0), (90.0, 76.0)]
# using list comprehension
out = [item for t in lt for item in t]
# printing output
print(out)