我在解决下一个问题时遇到了一些问题:
我们有一个元素列表(整数),我们应该返回一个仅包含此列表中非唯一元素的列表。在不改变列表顺序的情况下,我认为最好的方法是删除或删除所有唯一元素。
请注意,我刚刚开始学习 Python,并且只想要最简单的解决方案。
这是我的代码:
def checkio(data):
for i in data:
if data.count(i) == 1: #if element seen in the list just ones, we delet this el
ind = data.index(i)
del data[ind]
return data
Run Code Online (Sandbox Code Playgroud) 我需要编辑对象中的值.在对象的值中添加一个新单词(值).但是如果它已经存在,那就加上这个新词.
例如:
var obj = {className:'open menu'}
新词(值) - '新',结果 - > obj.className ='打开菜单新'
新词(值) - '打开',结果 - > obj.className ='打开菜单'(相同)
这是我的代码:
function addClass(object, value){
var str = object.className; //taking a string from object value
var arr = str.split(' '); // converting it to massive
if (arr.indexOf(value) !== undefined) { //proceed if value exist
str = str + " " + value // making new sring
object.className = str
{
return object.className
}
var obj = {
className: 'open menu' …Run Code Online (Sandbox Code Playgroud)