简单 - 寻找一种方法来做元素存在检查.. [英] Simple - looking for a way to do an element exists check..
问题描述
大家好,
我有一个简单的列表,如果在列表中的任何地方找不到
元素0,我想追加另一个元组。
element =(''/smsc/chp/aztec/padlib/5VT.Cat'',
''/ smsc / chp / aztec / padlib '',
''5VT.Cat'',(33060))
element1 =(''/ smsc / chp / aztec / padlib / 5VT .Cat2'',
''/ smsc / chp / aztec / padlib'',
''5VT.Cat2'',(33060))
a = [(''/smsc/chp/aztec/padlib/5VT.Cat'',
''/ smsc / chp / aztec / padlib'',
''5VT.Cat'',(33060)),
(''/smsc/chp/aztec/padlib/padlib.TopCat%'',,br / >
''/ smsc / chp / aztec / padlib'',
''padlib.TopCat%'',(33204)),
(' '/smsc/chp/aztec/padlib/Regulators.Cat%'',
''/ smsc / chp / aztec / padlib'',
''监管机构。 Cat%'',(33204))]
所以我的代码看起来很像喜欢这个。
found = False
for item中的项目:
如果item [0] == element [0 ]
found = True
break
如果找不到:
a.append(element)
但这只是丑陋 - 有没有一种简单的方法可以在不使用找到的旗帜的情况下对所有
物品进行交互?
谢谢
2月22日,11:20 * am,rh0dium< steven.kl ... @ gmail.comwrote:
>
found = False
for item中的物品:
* if item [0] == element [0]
* * found = True
* * break
如果找不到:
* a.append(元素)
但这只是丑陋 - 是否有一种更简单的方法可以对所有
项目进行交互没有使用找到的旗帜?
谢谢
for item中的物品:
if item [0] ==元素[0]
休息
:其他:#只有在我们永远不会'打破''for for循环时才会被调用
a.append(元素)
但是dict怎么样?
adict = dict((elem [0],elem)for a中的elem
如果item [0]不是adict:
adict [item [0]] = item
#需要最终列表吗?
a = adict.values()
没有列表搜索,如果真的很长就会很好地扩展。
- Paul
< blockquote> 2月22日,11:20 * am,rh0dium< steven.kl ... @ gmail.comwrote:
大家好,
我有一个简单的列表,如果在列表中的任何地方找不到
元素0,我想追加另一个元组。
element = *(''/ smsc / chp / aztec / padlib / 5VT.Cat'',
*''/ smsc / chp / aztec / padlib'',
*'''5VT.Cat'',(33060))
element1 = *(''/ smsc / chp / aztec / padlib / 5VT.Cat2'',
*''/ sm sc / chp / aztec / padlib'',
*'''5VT.Cat2'',(33060))
a = * [(''/ smsc / chp / aztec / padlib / 5VT.Cat'',
*''/ smsc / chp / aztec / padlib'',
*'''5VT.Cat '',(33060)),
*(''/ smsc / chp / aztec / padlib / padlib.TopCat%'',
*''/ smsc / chp / aztec / padlib'',
*''padlib.TopCat%'',(33204)),
*(''/ smsc / chp / aztec / padlib / Regulators.Cat%'',
*''/ smsc / chp / aztec / padlib'',
*''Regulators.Cat%'',( 33204))]
所以我的代码看起来像这样。
found = False
for item在a:
*如果item [0] == element [0]
* * found = True
* * break
如果找不到:
* a.append(元素)
但这只是丑陋 - 是否有一种更简单的交互方式所有
项目中没有找到标志?
谢谢
嗯,这就是我在思考之前输入的内容......
如果每个元素元组中的其余项目对于任何
给定元素[0],然后只使用一个集合。
aset = set(a)
for list_of_new_element_tuples中的元素:
aset.add(元素)
- Paul
2月22日,10: 20 am,rh0dium< steven.kl ... @ gmail.comwrote:
大家好,
我有一个简单的列表,如果在列表中的任何地方找不到
元素0,我想附加另一个元组。
element =(''/ smsc / chp / aztec / padlib / 5VT.Cat'',
''/ smsc / chp / aztec / padlib'',
''5VT.Cat'',( 33060))
element1 =(''/smsc/chp/aztec/padlib/5VT.Cat2'',
''/ smsc / chp / aztec / padlib'',
''5VT.Cat2'',(33060))
a = [(' /smsc/chp/aztec/padlib/5VT.Cat'',
''/ smsc / chp / aztec / padlib'',
''5VT.Cat' ',(33060)),
(''/smsc/chp/aztec/padlib/padlib.TopCat%'',
''/ smsc / chp / aztec / padlib'',
''padlib.TopCat%'',(33204)),
(''/smsc/chp/aztec/padlib/Regulators.Cat %'',
''/ smsc / chp / aztec / padlib'',
''Regulators.Cat%'',(33204))]
所以我的代码看起来像这样。
found = False
for item中的项目:
如果item [0] == element [0]
found = True
break
如果找不到:
a.append(元素)
但这只是丑陋 - 是否有一种简单的方法可以在没有内容的情况下对所有
项进行交互使用找到的标志?
谢谢
如何使用生成器表达式和Python的内置 ;在
运算符:
>> def example(myData,newData):
....如果newData [0]不在(my_x中的x为x [0]):
.... myData.append(newData)
....
>> l = []
示例(l,(''''',''apple'',''aviary''))/ $ l
[('''',''apple'',''aviary'')]
>> example(l,('s'',''spam'',''傻''' ))
l
[(''''',''apple'',''aviary'' ),('s'',''sp am'',''傻''')
>> example(l ,('s'',''suck-tastic''))
l
[(''' a'',''''''''''aviary''),('s'',''spam'',''傻'')]
< blockquote class =post_quotes>
>>>
Hi all,
I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.
element = (''/smsc/chp/aztec/padlib/5VT.Cat'',
''/smsc/chp/aztec/padlib'',
''5VT.Cat'', (33060))
element1 = (''/smsc/chp/aztec/padlib/5VT.Cat2'',
''/smsc/chp/aztec/padlib'',
''5VT.Cat2'', (33060))
a = [ (''/smsc/chp/aztec/padlib/5VT.Cat'',
''/smsc/chp/aztec/padlib'',
''5VT.Cat'', (33060)),
(''/smsc/chp/aztec/padlib/padlib.TopCat%'',
''/smsc/chp/aztec/padlib'',
''padlib.TopCat%'', (33204)),
(''/smsc/chp/aztec/padlib/Regulators.Cat%'',
''/smsc/chp/aztec/padlib'',
''Regulators.Cat%'', (33204))]
So my code would look something like this.
found = False
for item in a:
if item[0] == element[0]
found = True
break
if not found:
a.append(element)
But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?
Thanks
On Feb 22, 11:20*am, rh0dium <steven.kl...@gmail.comwrote:>
found = False
for item in a:
* if item[0] == element[0]
* * found = True
* * break
if not found:
* a.append(element)
But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?
Thanks
for item in a:
if item[0] == element[0]
break
else: # only called if we never ''break'' out of the for loop
a.append(element)
But what about a dict?
adict = dict((elem[0],elem) for elem in a)
if item[0] not in adict:
adict[item[0]] = item
# need the final list?
a = adict.values()
No list searching, and will scale well if a gets real long.
-- Paul
On Feb 22, 11:20*am, rh0dium <steven.kl...@gmail.comwrote:Hi all,
I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.
element = *(''/smsc/chp/aztec/padlib/5VT.Cat'',
* ''/smsc/chp/aztec/padlib'',
* ''5VT.Cat'', (33060))
element1 = *(''/smsc/chp/aztec/padlib/5VT.Cat2'',
* ''/smsc/chp/aztec/padlib'',
* ''5VT.Cat2'', (33060))
a = *[ (''/smsc/chp/aztec/padlib/5VT.Cat'',
* ''/smsc/chp/aztec/padlib'',
* ''5VT.Cat'', (33060)),
*(''/smsc/chp/aztec/padlib/padlib.TopCat%'',
* ''/smsc/chp/aztec/padlib'',
* ''padlib.TopCat%'', (33204)),
*(''/smsc/chp/aztec/padlib/Regulators.Cat%'',
* ''/smsc/chp/aztec/padlib'',
* ''Regulators.Cat%'', (33204))]
So my code would look something like this.
found = False
for item in a:
* if item[0] == element[0]
* * found = True
* * break
if not found:
* a.append(element)
But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?
ThanksWell, that''s what I get for typing before thinking...
If the remaining items in each element tuple are the same for any
given element[0], then just use a set.
aset = set(a)
for element in list_of_new_element_tuples:
aset.add(element)
-- Paul
On Feb 22, 10:20 am, rh0dium <steven.kl...@gmail.comwrote:Hi all,
I have a simple list to which I want to append another tuple if
element 0 is not found anywhere in the list.
element = (''/smsc/chp/aztec/padlib/5VT.Cat'',
''/smsc/chp/aztec/padlib'',
''5VT.Cat'', (33060))
element1 = (''/smsc/chp/aztec/padlib/5VT.Cat2'',
''/smsc/chp/aztec/padlib'',
''5VT.Cat2'', (33060))
a = [ (''/smsc/chp/aztec/padlib/5VT.Cat'',
''/smsc/chp/aztec/padlib'',
''5VT.Cat'', (33060)),
(''/smsc/chp/aztec/padlib/padlib.TopCat%'',
''/smsc/chp/aztec/padlib'',
''padlib.TopCat%'', (33204)),
(''/smsc/chp/aztec/padlib/Regulators.Cat%'',
''/smsc/chp/aztec/padlib'',
''Regulators.Cat%'', (33204))]
So my code would look something like this.
found = False
for item in a:
if item[0] == element[0]
found = True
break
if not found:
a.append(element)
But this is just ugly - Is there a simpler way to interate over all
items in a without using a found flag?
ThanksHow-about using a generator expression and Python''s built-in "in"
operator:
>>def example(myData, newData):
.... if newData[0] not in (x[0] for x in myData):
.... myData.append( newData )
....>>l = []
example( l, (''a'', ''apple'', ''aviary'') )
l
[(''a'', ''apple'', ''aviary'')]
>>example( l, (''s'', ''spam'', ''silly'') )
l
[(''a'', ''apple'', ''aviary''), (''s'', ''spam'', ''silly'')]
>>example( l, (''s'', ''suck-tastic'') )
l
[(''a'', ''apple'', ''aviary''), (''s'', ''spam'', ''silly'')]
>>>
这篇关于简单 - 寻找一种方法来做元素存在检查..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!