我有两个元素数量相等的python列表。我只需要减去忽略字符串元素的两个列表的int和float元素。
export class Example {
stores: Store[] = [];
constructor() {
this.stores = [
new Store("mike", "US"),
new Store("nelson", "UK")
];
}
clearData() {
this.stores = [];
}
}
现在第三个列表应该是两个列表元素的最终差异
list_1 = [1, 2, 3.0, 'test', 6.5]
list_2 = [2, 3, 10.0, 'test', 12.5]
我可以将字符串值保留在任一列表的list_3中。
答案 0 :(得分:0)
这是一种使用列表理解的方法。
演示:
list_1 = [1, 2, 3.0, 'test', 6.5]
list_2 = [2, 3, 10.0, 'test', 12.5]
res = [v if (isinstance(i, str) or isinstance(v, str)) else (i-v) for i,v in zip(list_2, list_1)]
print(res)
输出:
[1, 1, 7.0, 'test', 6.0]
答案 1 :(得分:0)
当两个值都是数字时,此列表推导将返回差值;如果不是这种情况,则将从<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OPTIMA Dashboard</title>
<!-- Bootstrap -->
<link href="vendors/bootstrap/dist/css/bootstrap.min.css"
rel="stylesheet">
<!-- Font Awesome -->
<link href="vendors/font-awesome/css/font-awesome.min.css"
rel="stylesheet">
<!-- NProgress -->
<link href="vendors/nprogress/nprogress.css" rel="stylesheet">
<!-- Animate.css -->
<link href="vendors/animate.css/animate.min.css" rel="stylesheet">
<!-- Custom Theme Style -->
<link href="build/css/custom.min.css" rel="stylesheet">
<!-- Google Recaptcha v2 -->
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body class="login">
<div>
<a class="hiddenanchor" id="signup"></a> <a class="hiddenanchor"
id="signin"></a>
<div class="login_wrapper">
<div class="animate form login_form">
<section class="login_content">
<form method="POST" action="/admin/submitLogin" commandName="index"
modelAttribute="login">
<h1>Login Form</h1>
<div>
<span
style="border: none; box-shadow: none; font-size: 12; color: red;">${status}</span>
<input type="text" class="form-control" placeholder="Username"
name="username" required="" />
</div>
<div>
<input type="password" class="form-control"
placeholder="Password" name="password" required="" />
</div>
<div class="g-recaptcha" data-sitekey="${key}"></div>
<br />
<div>
<button type="reset" class="btn btn-default submit">Reset</button>
<button type="submit" class="btn btn-default submit">Log
In</button>
</div>
<div class="clearfix"></div>
<div class="separator">
<div class="clearfix"></div>
<br />
<div>
<h1>
<i class="fa fa-paw"></i> OPTIMA
</h1>
<p>
<i class="fa fa-copyright"></i> 2017 Jatelindo Perkasa Abadi
</p>
</div>
</div>
</form>
</section>
</div>
</div>
</div>
</body>
</html>
返回值:
list_1
from numbers import Number
list_1 = [1, 2, 3.0, 'test', 6.5]
list_2 = [2, 3, 10.0, 'test', 12.5]
list_3 = [y-x if isinstance(x,Number) and isinstance(y,Number)
else x for x,y in zip(list_1,list_2)]
答案 2 :(得分:0)
您也可以尝试以下方法。
>>> list_1 = [1, 2, 3.0, 'test', 6.5]
>>> list_2 = [2, 3, 10.0, 'test', 12.5]
>>>
>>> list_3 = [item2-item1 if type(item1) is int or type(item1) is float else item1 if item1 else item2 for item1, item2 in zip(list_1, list_2)]
>>>
>>> list_3
[1, 1, 7.0, 'test', 6.0]
>>>