find a substring in a selected portion of a list elements
up vote
1
down vote
favorite
I have the below string list (list1) and I want to find if str b
is present anywhere in left hand side portion of an element before the decimal in list1.
I tried the below code but it finds all the elements where str b is found.
list1= ['4.39', '5.25', '2.29', '3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16', '4.4']
b=str(1)
print([s for s in list1 if b in s])
it returns the following:
['3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16']
However, I want to get only 1.5
because this is the only element where string b matches the left hand side part before decimal. Remember the elements are in string format. Any fast way of checking this thing?
python string list list-comprehension python-3.5
add a comment |
up vote
1
down vote
favorite
I have the below string list (list1) and I want to find if str b
is present anywhere in left hand side portion of an element before the decimal in list1.
I tried the below code but it finds all the elements where str b is found.
list1= ['4.39', '5.25', '2.29', '3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16', '4.4']
b=str(1)
print([s for s in list1 if b in s])
it returns the following:
['3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16']
However, I want to get only 1.5
because this is the only element where string b matches the left hand side part before decimal. Remember the elements are in string format. Any fast way of checking this thing?
python string list list-comprehension python-3.5
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have the below string list (list1) and I want to find if str b
is present anywhere in left hand side portion of an element before the decimal in list1.
I tried the below code but it finds all the elements where str b is found.
list1= ['4.39', '5.25', '2.29', '3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16', '4.4']
b=str(1)
print([s for s in list1 if b in s])
it returns the following:
['3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16']
However, I want to get only 1.5
because this is the only element where string b matches the left hand side part before decimal. Remember the elements are in string format. Any fast way of checking this thing?
python string list list-comprehension python-3.5
I have the below string list (list1) and I want to find if str b
is present anywhere in left hand side portion of an element before the decimal in list1.
I tried the below code but it finds all the elements where str b is found.
list1= ['4.39', '5.25', '2.29', '3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16', '4.4']
b=str(1)
print([s for s in list1 if b in s])
it returns the following:
['3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16']
However, I want to get only 1.5
because this is the only element where string b matches the left hand side part before decimal. Remember the elements are in string format. Any fast way of checking this thing?
python string list list-comprehension python-3.5
python string list list-comprehension python-3.5
edited Nov 22 at 15:37
jpp
87.8k195099
87.8k195099
asked Nov 22 at 15:33
HT121
817
817
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You need to split each string by .
and extract the first split:
print([s for s in list1 if '1' in s.split('.')[0]])
['1.5']
For a precise match, use ==
:
print([s for s in list1 if s.split('.')[0] == '1'])
['1.5']
And do you know how could i find the actual index of matched element in list1?
– HT121
Nov 22 at 15:47
1
[idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]]
should do it.
– jpp
Nov 22 at 15:59
Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])
– HT121
Nov 22 at 16:08
@HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).
– jpp
Nov 22 at 16:10
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You need to split each string by .
and extract the first split:
print([s for s in list1 if '1' in s.split('.')[0]])
['1.5']
For a precise match, use ==
:
print([s for s in list1 if s.split('.')[0] == '1'])
['1.5']
And do you know how could i find the actual index of matched element in list1?
– HT121
Nov 22 at 15:47
1
[idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]]
should do it.
– jpp
Nov 22 at 15:59
Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])
– HT121
Nov 22 at 16:08
@HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).
– jpp
Nov 22 at 16:10
add a comment |
up vote
2
down vote
accepted
You need to split each string by .
and extract the first split:
print([s for s in list1 if '1' in s.split('.')[0]])
['1.5']
For a precise match, use ==
:
print([s for s in list1 if s.split('.')[0] == '1'])
['1.5']
And do you know how could i find the actual index of matched element in list1?
– HT121
Nov 22 at 15:47
1
[idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]]
should do it.
– jpp
Nov 22 at 15:59
Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])
– HT121
Nov 22 at 16:08
@HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).
– jpp
Nov 22 at 16:10
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You need to split each string by .
and extract the first split:
print([s for s in list1 if '1' in s.split('.')[0]])
['1.5']
For a precise match, use ==
:
print([s for s in list1 if s.split('.')[0] == '1'])
['1.5']
You need to split each string by .
and extract the first split:
print([s for s in list1 if '1' in s.split('.')[0]])
['1.5']
For a precise match, use ==
:
print([s for s in list1 if s.split('.')[0] == '1'])
['1.5']
edited Nov 22 at 15:37
answered Nov 22 at 15:35
jpp
87.8k195099
87.8k195099
And do you know how could i find the actual index of matched element in list1?
– HT121
Nov 22 at 15:47
1
[idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]]
should do it.
– jpp
Nov 22 at 15:59
Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])
– HT121
Nov 22 at 16:08
@HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).
– jpp
Nov 22 at 16:10
add a comment |
And do you know how could i find the actual index of matched element in list1?
– HT121
Nov 22 at 15:47
1
[idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]]
should do it.
– jpp
Nov 22 at 15:59
Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])
– HT121
Nov 22 at 16:08
@HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).
– jpp
Nov 22 at 16:10
And do you know how could i find the actual index of matched element in list1?
– HT121
Nov 22 at 15:47
And do you know how could i find the actual index of matched element in list1?
– HT121
Nov 22 at 15:47
1
1
[idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]]
should do it.– jpp
Nov 22 at 15:59
[idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]]
should do it.– jpp
Nov 22 at 15:59
Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])
– HT121
Nov 22 at 16:08
Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])
– HT121
Nov 22 at 16:08
@HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).
– jpp
Nov 22 at 16:10
@HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).
– jpp
Nov 22 at 16:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434191%2ffind-a-substring-in-a-selected-portion-of-a-list-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown