Quantcast
Channel: User Mazdak - Stack Overflow
Browsing latest articles
Browse All 44 View Live

Comment by Mazdak on Efficient group substring search in Python?

@DeepDeadpool It doesn't make sense to have that string given the example you presented lol however you can use re.escape() to escape special characters. Check out the update.

View Article



Comment by Mazdak on Find duplicate items in all lists of a list of lists and...

@LakshmiRam Not the same

View Article

Comment by Mazdak on How do I multiply each element in a list by a number?

@AerinmundFagelson Here -> stackoverflow.com/questions/35091979/…

View Article

Comment by Mazdak on Indices of elements in a python list of tuples

Why there's a numpy tag here? Have you tried using Numpy arrays?

View Article

Comment by Mazdak on Removing spaces between items in list of Integers

@BlackThunder Using translate isn't a good idea here and not necessary at all. The error also occurs because you have to create the table with str.maketrans() first and then pass the table to...

View Article


Comment by Mazdak on Sort a list of strings containing numbers at the start...

Might be better to do split('-', 1) since you just want the first part of the split.

View Article

Comment by Mazdak on How to sort a list according to another array to...

@zhangboyu just reverse the order of argsort's result columns'; np.argsort(values)[:,::-1]

View Article

Comment by Mazdak on Expression valued differently and unexpectedly

Because True evaluates as 1 and False as 0. Or in other words, the value of True object is 1 and the value of False object is 0. Try int(True) or int(False).

View Article


Comment by Mazdak on Numpy float mean calculation precision

@EricPostpischil One other way is to look at all the items regardless of their types and then return one of them if they're all equal. So programmatically yes, there are other ways and since we're...

View Article


Comment by Mazdak on What is the difference between p = {} and p: dict= {} in...

They didn't want to make it like other languages that's all.

View Article

Comment by Mazdak on Problems with exec() in Python

Possible duplicate stackoverflow.com/questions/1463306/…

View Article

Comment by Mazdak on Why is a list comprehension so much faster than...

@guyarad It was but I still updated the code with a 3.8 version. Maybe your Python version is different because dis can yield slightly different results in different versions.

View Article

Comment by Mazdak on count the number of occurrences of a certain value in a...

@KellyBundy Yup, seems like some new under the hood optimizations. The countOf is also the best way tho.

View Article


Comment by Mazdak on How to Generate N random numbers in Python 3 between 0...

@theonlygusti Yup, that was a mistake, just updated the answer. Thanks for the comment.

View Article

Answer by Mazdak for Emulating a pass in a ternary operator?

You don't need else at all, just use if :[x for x in entity if x not in ignoreKeys]This will return the values that are not in ignoreKeys.

View Article


Answer by Mazdak for Numpy string array pad with string

You can't pad your array with string literals. Instead as it's mentioned in documentation you can use a pad_with function as follows:In [79]: def pad_with(vector, pad_width, iaxis, kwargs): ...:...

View Article

Answer by Mazdak for how to flatten a 2D list to 1D without using numpy?

Without numpy ( ndarray.flatten ) one way would be using chain.from_iterable which is an alternate constructor for itertools.chain :>>>...

View Article


Answer by Mazdak for Python: how to convert a dictionary into a subscriptable...

In python-3.X dict.values doesn't return a list object like how it used to perform in python-2.X. In python-3.x it returns a dict-value object which is a set-like object and uses a hash table for...

View Article

Answer by Mazdak for python is operator behaviour with string

One important thing about this behavior is that Python caches some, mostly, short strings (usually less than 20 characters but not for every combination of them) so that they become quickly accessible....

View Article

Answer by Mazdak for How do I concatenate two lists in Python?

For cases with a low number of lists you can simply add the lists together or use in-place unpacking (available in Python-3.5+):In [1]: listone = [1, 2, 3] ...: listtwo = [4, 5, 6] In [2]: listone +...

View Article

Answer by Mazdak for When reading in a txt matrix, how can i skip first column

Use a simple indexing:with open('file') as f: next(f) content = [x.strip().split()[1:] for x in f]This will give you the splitted zero and ones as a nested list.If you don't want to split the lines you...

View Article


Answer by Mazdak for How does numpy determine the array data type when it...

Numpy's array objects that are PyArrayObject types, have a NPY_PRIORITY attribute that denotes the priority of the types of items for cases where the array contains items with heterogeneous data types....

View Article


Answer by Mazdak for Existence of mutable named tuple in Python?

As a Pythonic alternative for this task, since Python-3.7, you can usedataclasses module that not only behaves like a mutable NamedTuple, because they use normal class definitions, they also support...

View Article

Answer by Mazdak for Moving elements in dictionary python to another index

If you're using CPython 3.6+, since dict are insertion-based ordered, you can move an item to the end by popping it and then re-assigning it to the dictionary.>>> dicta = {1: 'a', 2: 'b', 'N':...

View Article

Answer by Mazdak for how to increment the iterator from inside for loop in...

You can't do this inside a for loop, because every time the loop is restarted it reassigns the variable i regardless of your changes on the variable.To be able to manipulate your loop counting...

View Article


Answer by Mazdak for Why does comparison of bytes with str fails in Python 3?

In Python 3, strings are Unicode. The type used to hold text is str and the type used to hold data is bytes.the str and bytes types cannot be mixed, you must always explicitly convert between them. Use...

View Article

Answer by Mazdak for How can I make a for-loop pyramid more concise in Python?

By using nested for-loops you're basically trying to create what's known as the (Cartesian) product of the input iterables, which is what the product function, from itertools module, is...

View Article

Answer by Mazdak for Why can generator expressions be iterated over only once?

My question is why it was decided to implement generator expressions not similar to other objects that seem to be equivalent or at least very similar?Because that's exactly what a generator is. If you...

View Article

Answer by Mazdak for How to find the number of nested lists in a list?

You can use a recursive function as following:In [14]: def count_lists(l): ...: return sum(1 + count_lists(i) for i in l if isinstance(i,list)) ...: In [15]: In [15]:...

View Article



Answer by Mazdak for Can I reference a dict value on dict creation?

Yes and No! You can use f-strings (available in Python-3.6+) to automatically format strings based on already available variables which in this case can be netdev["ipv6"]["prefix"]. If You're not aware...

View Article

Answer by Mazdak for Filter values in a list using an array with boolean...

Python list object, unlike Numpy array, doesn't support boolean indexing directly. For that you could use itertools.compress function:>>> from itertools import compress>>>...

View Article

Answer by Mazdak for Why does this take so long to match? Is it a bug?

First off, I must say that this is not a BUG. What your regex is doing is that it's trying all the possibilities due to the nested repeating patters. Sometimes this process can gobble up a lot of time...

View Article

Answer by Mazdak for connect wifi with python or linux terminal

Here is a general approach using python os module and Linux iwlist command for searching through the list of wifi devices and nmcli command in order to connect to a the intended device.In this code the...

View Article


Answer by Mazdak for Long type in Python-3, NameError: name 'long' is not...

The long() function is no longer supported by Python 3 (no pun intended). It only has one built-in integral type, named int; but it behaves mostly like the old long type. So you just need to use int()...

View Article

Answer by Mazdak for Search pattern to include square brackets

It's because of that regex engine assume the square brackets as character class which are regex characters for get ride of this problem you need to escape your regex characters. you can use re.escape...

View Article

Answer by Mazdak for Does the print function in python have a limit of string...

About the maximum size of your string which you can print in stdout using print function, because you have to pass your text as a python object to print function and since the max size of your variable...

View Article


Answer by Mazdak for Two integers in Python have same id, but not lists or...

Immutable objects don't have the same id, and as a matter of fact this is not true for any type of objects that you define separately. Generally speaking, every time you define an object in Python,...

View Article


Answer by Mazdak for Sort multidimensional NumPy array by norm over a dimension

Since sortidxs is contain the desire indices for each axis (from start to end) you can generate the first axis range by np.arange(a.shape[0]) and pass it as the first axis while indexing:In [31]: x,y,...

View Article

"TemplateSyntaxError Invalid block tag: 'trans'" error in Django Templates

After running the runserver command I get the following error:TemplateSyntaxError at /questions/ Invalid block tag: 'trans'Does anybody know that what's the reason?This is my template syntax:{% extends...

View Article

Answer by Mazdak for Python - Get Yesterday's date as a string in YYYY-MM-DD...

You just need to subtract one day from today's date. In Python datetime.timedelta object lets you create specific spans of time as a timedelta object.datetime.timedelta(1) gives you the duration of...

View Article

Answer by Mazdak for Inverting a dictionary with list values

You can not use list objects as dictionary keys, since they should be hashable objects. You can loop over your items and use dict.setdefault method to create the expected result:new = {}for k,value in...

View Article


Answer by Mazdak for Rounding errors in Python floor division

That's because there isn't any 0.4 in Python (floating-point finite representation). It's actually a float like 0.4000000000000001 which makes the floor of division to be 19.>>>...

View Article

Comment by Mazdak on Slicing a list into sublists based on condition

@storluffarn [0] chooses the first index of the np.where result because it tends to return a list that it's length is equal to number of indices the array that you've passed to the function (here it's...

View Article

Browsing latest articles
Browse All 44 View Live




Latest Images