Python set remove Non Numeric values - python

I have set value like below:
set(['Virtual', '120', 'P', '130', '90', '250', '100', '10', 'Mar', 'indicates', '18', '50', '40', '1', '|'])
How do i remove all Non Numeric value?
Output expected:
set(['120', '130', '90', '250', '100', '10','18', '50', '40', '1'])

You can create a new set:
number_set = set()
for object in old_set:
try:
number_set.add(int(object))
except ValueError:
print("Not a number")
print(number_set)
You can also try removing all non-numeric objects from the set:
for object in old_set:
try:
x = int(object)
execpt ValueError:
old_set.remove(object)

You can use a filter to clean your set:
s = set(['Virtual', '120', 'P', '130', '90', '250', '100', '10', 'Mar', 'indicates', '18', '50', '40', '1', '|'])
def isInt(text):
"""Returns True for a text that is convertable to int() else False."""
try:
_ = int(text)
return True
except ValueError:
return False
# apply filter:
filteredSet = set( filter(lambda x:isInt(x), s))
print(filteredSet)
Output:
{'18', '90', '130', '120', '40', '50', '10', '1', '100', '250'}
This output differs from your want's - but thats how python prints a set with print.

Related

filter python list based on values in a different list

I think this is an entry level computer science 101 course question about algorithms and data structures.
I have a list:
VAV_ID_list = ['36','38','21','29','31','25','9','13','14','19','30','8','26','6','34','11','12028','20','27','15','12032','23','16','24','37','39','12033','10']
How I can I filter out these values in VAV_ID_exclude_list from VAV_ID_list?
VAV_ID_exclude_list = ['36','38','21','29','31','25','9','13','14','19','30','8','26','6']
This code below obviously doesnt do anything any tips greatly appreciated.
filtered_VAV_ID_list = [zone for zone in VAV_ID_list if zone == 36]
print(filtered_VAV_ID_list)
This is what you want
list2= [zone for zone in VAV_ID_list if zone not in VAV_ID_exclude_list]
You can do it in multiple ways:
This is the most straightforward way.
>>> [i for i in VAV_ID_list if i not in VAV_ID_exclude_list]
['34', '11', '12028', '20', '27', '15', '12032', '23', '16', '24', '37', '39', '12033', '10']
You can even use sets if the order is not important and you don't have duplicates.
>>> list(set(VAV_ID_list) - set(VAV_ID_exclude_list))
['24', '11', '39', '27', '20', '23', '12033', '12032', '16', '37', '34', '15', '12028', '10']
for el in VAV_ID_list:
if el not in VAV_ID_exclude_list:
print(el)
I think this will do it.

How to automate single line code, with changes in input?

I have created a variable named 'j' which has some values and I want my code to pick one value at a time and execute.
I tried writing code but it does not work. I'm sharing my code please see when it can be improved.
j = ['0', '1', '3', '4', '6', '7', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67']
for i in j:
labels('i') = mne.read_labels_from_annot('sub-CC721377_T1w', parc='aparc', subjects_dir=subjects_dir)['i']
done
According to the MNE documentation, the function read_labels_from_annot returns a list of labels.
Thus, instead of indexing the result with ...)[0] at the end, you should just capture the entire list:
labels = mne.read_labels_from_annot(...)
This would capture a list of labels, rather than a single label, which would have the effect of 'indexing at the end "[0]" from 0 - 67'.
You asked about adding all the results together into a label_all variable. You didn't specify (and I don't know anything about the MNE package), so it's not clear: do the labels ever repeat? Is is possible that "lab123" will occur in every input file? If so, should the label_all store multiple copies of the same value, or just the unique label names?
I think something like this is what you're after:
import mne
def get_labels_for_subject(sub, *, hemi='both', parc='aparc', **kwargs):
"""Get MNE labels for a given subject. **kwargs allows passing named
parameters like subjects_dir, regexp, and others that default to None."""
labels = mne.read_labels_from_annot(sub, hemi=hemi, parc=parc, **kwargs)
return labels
# List of all the subjects
subjects = [
'sub-CC721377_T1w',
'sub-next???',
]
label_all = []
for s in subjects:
label_all.extend(get_labels_for_subject(s, subjects_dir='.'))
print("Got labels:", label_all)

Value overrided after round played, visible when printed out

My code:
users = []
users.append({"name" : "",
"numerics" : [],
"score" : 0 })
users[0]["name"] = input("Your name plz: ")
def getNumbers():
for i in range(len(users)):
numbers = input("Assign 8 different numbers (separate each with a comma ','): ")
userNumbers = numbers.split(",")
return userNumbers
users[0]["numerics"] = getNumbers()
scores = []
scores.append(users[:])
print(scores)
users[0]["numerics"] = getNumbers()
scores.append(users[:])
print(scores)
Running example:
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> users = []
>>> users.append({"name" : "",
"numerics" : [],
"score" : 0 })
>>> users[0]["name"] = input("Your name plz: ")
Your name plz: Gladiator
>>> def getNumbers():
for i in range(len(users)):
numbers = input("Assign 8 different numbers (separate each with a comma ','): ")
userNumbers = numbers.split(",")
return userNumbers
>>> users[0]["numerics"] = getNumbers()
Assign 8 different numbers (separate each with a comma ','): 99,23,55,11,19,100,1000,89
>>> scores = []
>>> scores.append(users[:])
>>> print(scores)
[[{'numerics': ['99', '23', '55', '11', '19', '100', '1000', '89'], 'name': 'Gladiator', 'score': 0}]]
>>> users[0]["numerics"] = getNumbers()
Assign 8 different numbers (separate each with a comma ','): 100,56,77,32,99,22,45,2
>>> scores.append(users[:])
>>> print(scores)
[[{'numerics': ['100', '56', '77', '32', '99', '22', '45', '2'], 'name': 'Gladiator', 'score': 0}], [{'numerics': ['100', '56', '77', '32', '99', '22', '45', '2'], 'name': 'Gladiator', 'score': 0}]]
>>>
I want to be able to for each new round in the game, make changes to the user numerics and score, and append it to the score list and then print that out. However, it seems like my current way is just overriding the numerics and the score. But mostly the numerics part.
This doesn't seem to work at all: users[0]["score"].append(getNumbers())
Or should I simply clear the values in the list, e.g. score and numerics for each new round for the player.
Round 1:
Player: Gladiator
Numerics: ['99', '23', '55', '11', '19', '100', '1000', '89']
Round 2:
Player: Gladiator
Numerics: ['100', '56', '77', '32', '99', '22', '45', '2']
But the print out shows:
[[{'numerics': ['100', '56', '77', '32', '99', '22', '45', '2'],
'name': 'Gladiator', 'score': 0}],
[{'numerics': ['100', '56', '77', '32', '99', '22', '45', '2'],
'name': 'Gladiator', 'score': 0}]]
EDIT: I tried resetting the list of numerics:
users[0]['numerics'] = []
TypeError: list indices must be integers or slices, not tuple
Test ran the code example by #Sarathsp
Your name plz: Gladiator
Assign 8 different numbers (separate each with a comma ','): 99,100,1000,4,66,77,11,9
[[{'name': 'Gladiator', 'numerics': ['99', '100', '1000', '4', '66', '77', '11', '9'], 'score': 0}]]
Assign 8 different numbers (separate each with a comma ','): 100,33,44,55,0,1,9,2
[[{'name': 'Gladiator', 'numerics': ['99', '100', '1000', '4', '66', '77', '11', '9'], 'score': 0}], [{'name': 'Gladiator', 'numerics': ['100', '33', '44', '55', '0', '1', '9', '2'], 'score': 0}]]
Assignment statements in Python do not copy objects, they create bindings between a target and an object. You have to use deep copy to copy the users list
there is a module copy
you may use it as
copy.deepcopy(users)
Here is how your program look like
import copy
users = []
users.append({"name" : "",
"numerics" : [],
"score" : 0 })
users[0]["name"] = input("Your name plz: ")
def getNumbers():
for i in range(len(users)):
numbers = input("Assign 8 different numbers (separate each with a comma ','): ")
userNumbers = numbers.split(",")
return userNumbers
users[0]["numerics"] = getNumbers()
scores = []
scores.append(copy.deepcopy(users))
print(scores)
users[0]["numerics"] = getNumbers()
scores.append(copy.deepcopy(users))
print(scores)

Random data generator mathing a regex in python

In python, I am looking for python code which I can use to create random data matching any regex. For example, if the regex is
\d{1,100}
I want to have a list of random numbers with a random length between 1 and 100 (equally distributed)
There are some 'regex inverters' available (see here) which compute ALL possible matches, which is not what I want, and which is extremely impracticable. The example above, for example, has more then 10^100 possible matches, which never can be stored in a list. I just need a function to return a match by random.
Maybe there is a package already available which can be used to accomplish this? I need a function that creates a matching string for ANY regex, not just the given one or some other, but maybe 100 different regex. I just cannot code them myself, I want the function extract the pattern to return me a matching string.
If the expressions you match do not have any "advanced" features, like look-ahead or look-behind, then you can parse it yourself and build a proper generator
Treat each part of the regex as a function returning something (e.g., between 1 and 100 digits) and glue them together at the top:
import random
from string import digits, uppercase, letters
def joiner(*items):
# actually should return lambda as the other functions
return ''.join(item() for item in items)
def roll(item, n1, n2=None):
n2 = n2 or n1
return lambda: ''.join(item() for _ in xrange(random.randint(n1, n2)))
def rand(collection):
return lambda: random.choice(collection)
# this is a generator for /\d{1,10}:[A-Z]{5}/
print joiner(roll(rand(digits), 1, 10),
rand(':'),
roll(rand(uppercase), 5))
# [A-C]{2}\d{2,20}#\w{10,1000}
print joiner(roll(rand('ABC'), 2),
roll(rand(digits), 2, 20),
rand('#'),
roll(rand(letters), 10, 1000))
Parsing the regex would be another question. So this solution is not universal, but maybe it's sufficient
Two Python libraries can do this: sre-yield and Hypothesis.
sre-yield
sre-yeld will generate all values matching a given regular expression. It uses SRE, Python's default regular expression engine.
For example,
import sre_yield
list(sre_yield.AllStrings('[a-z]oo$'))
['aoo', 'boo', 'coo', 'doo', 'eoo', 'foo', 'goo', 'hoo', 'ioo', 'joo', 'koo', 'loo', 'moo', 'noo', 'ooo', 'poo', 'qoo', 'roo', 'soo', 'too', 'uoo', 'voo', 'woo', 'xoo', 'yoo', 'zoo']
For decimal numbers,
list(sre_yield.AllStrings('\d{1,2}'))
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']
Hypothesis
The unit test library Hypothesis will generate random matching examples. It is also built using SRE.
import hypothesis
g=hypothesis.strategies.from_regex(r'^[A-Z][a-z]$')
g.example()
with output such as:
'Gssov', 'Lmsud', 'Ixnoy'
For decimal numbers
d=hypothesis.strategies.from_regex(r'^[0-9]{1,2}$')
will output one or two digit decimal numbers: 65, 7, 67 although not evenly distributed. Using \d yielded unprintable strings.
Note: use begin and end anchors to prevent extraneous characters.
From this answer
You could try using python to call this perl module:
https://metacpan.org/module/String::Random

Python raises a KeyError (for an out of dictionary key) even though the key IS in the dictionary

I'm getting a KeyError for an out of dictionary key, even though I know the key IS in fact in the dictionary. Any ideas as to what might be causing this?
print G.keys()
returns the following:
['24', '25', '20', '21', '22', '23', '1', '3', '2', '5', '4', '7', '6', '9', '8', '11', '10', '13', '12', '15', '14', '17', '16', '19', '18']
but when I try to access a value in the dictionary on the next line of code...
for w in G[v]: #note that in this example, v = 17
I get the following error message:
KeyError: 17
Any help, tips, or advice are all appreciated. Thanks.
That's simple, 17 != '17'
The keys are strings, you are trying to access them as ints.
try with v = '17'. You must convert your int to string

Categories

Resources