[英]Filter a list containing specific string
My Goal我的目标
I have a specific set of a long list containing many asset pairs with different endings: BTCUSDT
ETHBTC
ANKRETH
... From this list, I would like to filter out the symbols ending with USDT.我有一组特定的长列表,其中包含许多具有不同结尾的资产对: BTCUSDT
ETHBTC
ANKRETH
... 从这个列表中,我想过滤掉以 USDT 结尾的符号。
My Problem:我的问题:
I have tried using filter
and all
with iterations, however, none provides the exact result.我试过使用filter
和all
迭代,但是,没有一个提供确切的结果。
Here are my attempts:这是我的尝试:
Attempt 1:尝试 1:
my_set = ['LUNAUSDT', 'ETHBTC', 'ETHBNB', 'BTCUSDT', 'MANATUSD', 'ALICEETH' ]
my_set = [word for word in symbols if 'USDT' in my_set]
my_set
This results in an empty set.这导致一个空集。
Attempt 2:尝试 2:
keyword = ['USDT']
my_set = ['LUNAUSDT', 'ETHBTC', 'ETHBNB', 'BTCUSDT', 'MANATUSD', 'ALICEETH' ]
final = [ x for x in my_set in all(keyword in keyword for keyword in my_set)]
final
This results in the error:这导致错误:
TypeError: argument of type 'bool' is not iterable TypeError: 'bool' 类型的参数不可迭代
Attempt 3:尝试 3:
my_final_set = filter(lambda x:x.endswith(("USDT")), my_set)
my_final_set
This shows: <filter at 0x7fe537eebf10>这表明:<filter at 0x7fe537eebf10>
I basically want my final list with all symbols ending with USDT
我基本上想要我的最终列表,其中所有符号都以USDT
结尾
For example:例如:
my_set = ['LUNAUSDT', 'ETHBTC', 'ETHBNB', 'BTCUSDT', 'MANATUSD', 'ALICEETH' ]
results in: ['LUNAUSDT', 'BTCUSDT']
结果: ['LUNAUSDT', 'BTCUSDT']
Any help or advice on what I'm doing wrong would be massively appreciated.对我做错了什么的任何帮助或建议将不胜感激。 Thanks in advance!提前致谢!
You're almost there.你快到了。 Try this:尝试这个:
my_set = ['LUNAUSDT', 'ETHBTC', 'ETHBNB', 'BTCUSDT', 'MANATUSD', 'ALICEETH' ]
final = [word for word in my_set if 'USDT' in word]
print(final)
For your Attempt 3 , make this change:对于您的Attempt 3 ,进行以下更改:
my_final_set = list(filter(lambda x:x.endswith(("USDT")), my_set))
One another method is to use .endswith()
:另一种方法是使用.endswith()
:
final = [word for word in my_set if word.endswith('USDT')]
print(final)
Output: Output:
['LUNAUSDT', 'BTCUSDT']
solution using filter
function and list-comprehension
使用filter
function 和list-comprehension
解决方案
my_set = ['LUNAUSDT', 'ETHBTC', 'ETHBNB', 'BTCUSDT', 'MANATUSD', 'ALICEETH' ]
keyword = 'USDT'
result1 = list(filter(lambda word: word.endswith(keyword), my_set))
result2 = [ word for word in my_set if word.endswith(keyword)]
You can use regex:您可以使用正则表达式:
import re
my_set = ['LUNAUSDT', 'ETHBTC', 'ETHBNB', 'BTCUSDT', 'MANATUSD', 'ALICEETH' ]
my_filtered_set = [ i for i in my_set if re.search('USD$',i) ]
[word for word in symbols if 'USDT' in my_set]
This has two mistakes:这有两个错误:
'USDT' in my_set
checks if 'USDT'
is contained in the input list, not if it is contained in one of the words from the input list. 'USDT' in my_set
检查'USDT'
是否包含在输入列表中,而不是它是否包含在输入列表中的某个单词中。 You should have used 'USDT' in word
.你应该'USDT' in word
。
'USDT' in word
would check if 'USDT'
is contained anywhere in word
(not just at the end). 'USDT' in word
word
的任何地方是否包含'USDT'
(不仅仅是末尾)。 In order to check if a string ends with a particular suffix, use word.endswith('USDT')
.为了检查字符串是否以特定后缀结尾,请使用word.endswith('USDT')
。
[ x for x in my_set in all(keyword in keyword for keyword in my_set)]
This makes the least sense of your attempts.这使您的尝试毫无意义。 all(...)
returns either True
or False
, depending on whether a condition is true for all elements from an iterable. all(...)
返回True
或False
,具体取决于可迭代对象中的所有元素的条件是否为真。 In this case keyword in keyword
is obviously true for all words keyword
from my_set
, so this would be equivalent to在这种情况下keyword in keyword
对于my_set
中的所有单词keyword
显然都是正确的,因此这相当于
[x for x in my_set in True]
Here, Python would try to evaluate my_set in True
as if True
were some sort of collection.在这里,Python 会尝试评估 True 中的my_set in True
就好像True
是某种集合一样。 It attempts this by trying to iterate over True
(and then checking in turn if any item is equal to my_set
), which is not possible.它通过尝试迭代True
(然后依次检查是否有任何项目等于my_set
)来尝试这样做,这是不可能的。
filter(lambda x:x.endswith(("USDT")), my_set)
This is mostly correct, however filter
returns an iterator , which only returns the results as you iterate over it.这大部分是正确的,但是filter
返回一个iterator ,它只在您迭代它时返回结果。 In order to get a list, you have to consume the iterator:为了获得列表,您必须使用迭代器:
list(filter(lambda x: x.endswith(("USDT")), my_set))
which is approximately equivalent to这大约相当于
result = []
for y in filter(lambda x: x.endswith(("USDT")), my_set):
result.append(y)
See Why does foo = filter(...) return a <filter object>, not a list?请参阅为什么 foo = filter(...) 返回一个 <filter object>,而不是一个列表?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.