import re
data = ['A p=45 n=200 SNR=12', 'B p=2232 n=22 SNR=2']
result = []
for x in data:
result.append( map( int, re.search('p=(\d+).*n=(\d+).*SNR=(\d+)', x).groups()) )
print result
[[45, 200, 12], [2232, 22, 2]]
Answer from furas on Stack Overflowpython - How to extract multiple values from a string - Stack Overflow
python - Check if multiple strings exist in another string - Stack Overflow
Regex: Help to find multiple values in string (Python) - Stack Overflow
python - How do I store multiple strings as one variable - Stack Overflow
import re
data = ['A p=45 n=200 SNR=12', 'B p=2232 n=22 SNR=2']
result = []
for x in data:
result.append( map( int, re.search('p=(\d+).*n=(\d+).*SNR=(\d+)', x).groups()) )
print result
[[45, 200, 12], [2232, 22, 2]]
Another option is this:
def funz(l):
return [tuple(int(i.split('=')[1]) for i in item.split(' ')[1:]) for item in l]
Edited re @bvukelic's comment.
You can use any:
a_string = "A string is more than its parts!"
matches = ["more", "wholesome", "milk"]
if any(x in a_string for x in matches):
Similarly to check if all the strings from the list are found, use all instead of any.
any() is by far the best approach if all you want is True or False, but if you want to know specifically which string/strings match, you can use a couple things.
If you want the first match (with False as a default):
match = next((x for x in a if x in a_string), False)
If you want to get all matches (including duplicates):
matches = [x for x in a if x in a_string]
If you want to get all non-duplicate matches (disregarding order):
matches = {x for x in a if x in a_string}
If you want to get all non-duplicate matches in the right order:
matches = []
for x in a:
if x in a_string and x not in matches:
matches.append(x)
Try this:
(C\d{3})([A-RT-Z\d]+)?(S[\d\-_]+)?(?:_\d+)
Result: https://regex101.com/r/FETn0U/1
You can extract values like this (using Avinash's regex)
import re
regex = re.compile(r"(C\d{3})([A-RT-Z\d]+)?(S[\d\-_]+)?(?:_\d+)")
text = "C001F1S15_08"
match = regex.match(text)
print(match.group(1)) # C001
print(match.group(2)) # F1
print(match.group(3)) # S15
print(match.groups()) # ('C001', 'F1', 'S15')
print(list(match.groups()[:3])) # ['C001', 'F1', 'S15']
See here for more information. Keep in mind that .group(0) refers to the entire match, in this case the input string.
if user_name == ["Jake", "Bake"]:
checks whether user_name equals the list ["Jake", "Bake"].
If you want test check whether the name is in the list, use the keyword in:
if user_name in ["Jake", "Bake"]:
Your problem is that the program checks to see if the username is the list ["Jake", "Bake"] and the password is the list ["hello", "notem"].
The correct code is:
user_name = input ("Hello, user please enter your username!")
if user_name in ["Jake", "Bake"]:
Password = input("Please enter your password %s" %user_name)
if Password in ["hello", "notem"]:
print ("Welcome back %s" %user_name)
else:
print ("You are an imposter! Begone!!")
else:
if user_name == ("Bake"):
Password = input("Please enter your password %s" %user_name)
if Password == ("hell"):
print ("Welcome back %s" %user_name)
else:
print ("You are an imposter! Begone!!")
Edit: you forgot to treat the case in which the user name is neither Jake nor Bake.
Hey there folks I wrote a small script with PRAW to post a list of top 10 users with highest karma within a subreddit.
I have a function that fetches the data from my DB, it returns a list of 10 tuples with 2 values, so something like this [(user, karma), (user1, karma1) ... ].
Normally I would just iterate over it and display the data in a print statement, however this time I need to "submit" all the values at once as a whole inside a table.
This table:
snippet 1
REPLY_TEXT="""-|user|+karma
:-:|:-:|:-:
1|{}|{}
2|{}|{}
3|{}|{}
4|{}|{}
5|{}|{}
6|{}|{}
7|{}|{}
8|{}|{}
9|{}|{}
10|{}|{}"""
snippet 2
result_list = my_list_from_db()
reddit.subreddit("test").submit(title="This is a title", selftext=REPLY_TEXT.format(result_list[0][0], result_list[0][1], result_list[1][0], result_list[1][1], result_list[2][0], result_list[2][1], result_list[3][0], result_list[3][1], result_list[4][0], result_list[4][1], result_list[5][0], result_list[5][1], result_list[6][0], result_list[6][1], result_list[7][0], result_list[7][1], result_list[8][0], result_list[8][1], result_list[9][0], result_list[9][1]))This works but is tedious to write and doesn't look good, how can I improve this? Can I loop through the list inside the .format() or a more efficient way to pass a lot of values all in once?
No, you can't assign multiple values to a single variable.
The expression x or y returns y if x is false, or x if not.
A string only evaluates false if it's empty. Your string "b" is not empty, so it will never be false. Thus there's no way for that expression "b" or [] to equal [], it will always be "b".
Not, it is not possible.
What you have done is assign to a the value of the expression (1 or 2); that is, the result of or-ing 1 and 2.
It's called iterable unpacking. If the right hand side of an assignment is an iterable object, you can unpack the values into different names. Strings, lists and tuples are just a few examples of iterables in Python.
>>> a, b, c = '123'
>>> a, b, c
('1', '2', '3')
>>> a, b, c = [1, 2, 3]
>>> a, b, c
(1, 2, 3)
>>> a, b, c = (1, 2, 3)
>>> a, b, c
(1, 2, 3)
If you are using Python 3, you have access to Extended Iterable Unpacking which allows you to use one wildcard in the assignment.
>>> a, *b, c = 'test123'
>>> a, b, c
('t', ['e', 's', 't', '1', '2'], '3')
>>> head, *tail = 'test123'
>>> head
't'
>>> tail
['e', 's', 't', '1', '2', '3']
You are correct.
The first statement will split the string given as input in one character strings and unpack the list. Therefore with this syntax you need as many variables in the left hand side expression as characters in your string.