Rectangle 27 275

You can use a list comprehension:

indices = [i for i, x in enumerate(my_list) if x == "whatever"]
enumerate
filter()
print([i for i, x in enumerate([[1,1],[0,1]]) if x == 1])
[]
[[0, 1], [0, 0], [1, 1]]

@AndersonGreen: The term "multidimensional array" suggests a data structure that is guaranteed to have uniform size along each of its axes. There's no such data structure in plain Python. There's lists of lists, but they are very different from "multidimensional arrays". If you want the latter, you should consider using NumPy, which allows you to do things like (a == 1).nonzero() for a NumPy array a.

@AndersonGreen That's not what the OP asked for...

python - How to find all occurrences of an element in a list? - Stack ...

python list
Rectangle 27 63

While not a solution for lists directly, numpy really shines for this sort of thing:

import numpy as np
values = np.array([1,2,3,1,2,4,5,6,3,2,1])
searchval = 3
ii = np.where(values == searchval)[0]
ii ==>array([2, 8])

This can be significantly faster for lists (arrays) with a large number of elements vs some of the other solutions.

I noticed the [0] at the end converts what would be an array to a string. I'm curious why you chose to do this.

[0]
where
(array([2, 8], dtype=int64),)

Hey @Winand I put in [0] but still get both parts. Here is my code: (nrg.local_logs.all_id_resp_address is a list) "ste = "199.38.164.165" value = np.where(nrg.local_logs.all_id_resp_address == ste)[0]" I will be happy if you can tell me what I did wrong

all_id_resp_address
np.array
list

Thanks @Winand that was my problem (probably a newbie question since I am new)

python - How to find all occurrences of an element in a list? - Stack ...

python list
Rectangle 27 28

You need to filter out directories; os.listdir() lists all names in a given path. You can use os.path.isdir() for this:

basepath = '/path/to/directory'
for fname in os.listdir(basepath):
    path = os.path.join(basepath, fname)
    if os.path.isdir(path):
        # skip directories
        continue

os.walk() does the same work under the hood; unless you need to recurse down subdirectories, you don't need to use os.walk() here.

Exactly what I was looking for. More knowledge with example. I have to choose one answer, but all three added concepts I will try and research.

Python - how to find files and skip directories in os.listdir - Stack ...

python file-manager
Rectangle 27 26

As Peter mentioned, calendar.monthrange(year, month) returns weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month.

>>> import calendar
>>> print calendar.monthrange(2012,1)[1]
31
>>> calendar.monthrange(2012,2)[1]
29

Edit: updated answer to return the number of days of the current month

>>> import calendar
>>> import datetime
>>> now = datetime.datetime.now()
>>> print calendar.monthrange(now.year, now.month)[1]
29

Thanks!!!!but i dont want to hardcode the dates like (2012,1)..I want it to be the system date

Thank you so much.This works for windows..i am using ubuntu 11.10..and import calender produces the error "No module named calender"..any ideas??? or is there an alternate method of getting this done without using 'calender'???

calendAr
calendEr

python - How to find number of days in the current month - Stack Overf...

python datetime
Rectangle 27 23

A solution using list.index:

def indices(lst, element):
    result = []
    offset = -1
    while True:
        try:
            offset = lst.index(element, offset+1)
        except ValueError:
            return result
        result.append(offset)

It's much faster than the list comprehension with enumerate, for large lists. It is also much slower than the numpy solution if you already have the array, otherwise the cost of converting outweighs the speed gain (tested on integer lists with 100, 1000 and 10000 elements).

NOTE: A note of caution based on Chris_Rands' comment: this solution is faster than the list comprehension if the results are sufficiently sparse, but if the list has many instances of the element that is being searched (more than ~15% of the list, on a test with a list of 1000 integers), the list comprehension is faster.

You say this is faster than a list comp, can you show your timings that demonstrate this?

This was a long time ago, I probably used timeit.timeit with randomly generated lists. That's an important point though, and I suppose that may be why you ask. At the time it didn't occur to me, but the speed gains are only true if the results are sufficiently sparse. I just tested with a list full of the element to search for, and it's much slower than the list comprehension.

python - How to find all occurrences of an element in a list? - Stack ...

python list
Rectangle 27 12

Here is a nice little one-liner in the form of a list comprehension:

[f for f in os.listdir(your_directory) if os.path.isfile(os.path.join(your_directory, f))]

This will return a list of filenames within the specified your_directory.

filter(os.path.isfile, os.listdir(your_directory))

This doesn't actually work, as f is a filename, not a full path. You'd need os.path.isfile(os.path.join(your_directory, f)) there.

Python - how to find files and skip directories in os.listdir - Stack ...

python file-manager
Rectangle 27 10

In [1]: l=[1,2,3,4,3,2,5,6,7]

In [2]: [i for i,val in enumerate(l) if val==3]
Out[2]: [2, 4]

python - How to find all occurrences of an element in a list? - Stack ...

python list
Rectangle 27 6

import os
filter(os.path.isfile, os.listdir( os.curdir ))

os.curdir is the current directory

Python - how to find files and skip directories in os.listdir - Stack ...

python file-manager
Rectangle 27 1009

You're looking for os.path.isdir, or os.path.exists if you don't care whether it's a file or a directory.

import os
print(os.path.isdir("/home/el"))
print(os.path.exists("/home/el/myfile.txt"))

@syedrakib While parentheses can be used to indicate that an object is callable, that's not useful in Python, since even classes are callable. Also, functions are first-class values in Python, and you can use them without the parentheses notation, like in existing = filter(os.path.isdir(['/lib', '/usr/lib', '/usr/local/lib'])

os.path.isdir()
os.path.exists()

You can pass functions to other functions, like map, but in the general case, you call functions with arguments and parentheses. Also, there is some typo in your example. presumably you mean filter(os.path.isdir, ['/lib', '/usr/lib', '/usr/local/lib']).

How to find if directory exists in Python - Stack Overflow

python directory
Rectangle 27 13

The location of the local minima can be found for an array of arbitrary dimension using Ivan's detect_peaks function, with minor modifications:

import numpy as np
import scipy.ndimage.filters as filters
import scipy.ndimage.morphology as morphology

def detect_local_minima(arr):
    # https://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array/3689710#3689710
    """
    Takes an array and detects the troughs using the local maximum filter.
    Returns a boolean mask of the troughs (i.e. 1 when
    the pixel's value is the neighborhood maximum, 0 otherwise)
    """
    # define an connected neighborhood
    # http://www.scipy.org/doc/api_docs/SciPy.ndimage.morphology.html#generate_binary_structure
    neighborhood = morphology.generate_binary_structure(len(arr.shape),2)
    # apply the local minimum filter; all locations of minimum value 
    # in their neighborhood are set to 1
    # http://www.scipy.org/doc/api_docs/SciPy.ndimage.filters.html#minimum_filter
    local_min = (filters.minimum_filter(arr, footprint=neighborhood)==arr)
    # local_min is a mask that contains the peaks we are 
    # looking for, but also the background.
    # In order to isolate the peaks we must remove the background from the mask.
    # 
    # we create the mask of the background
    background = (arr==0)
    # 
    # a little technicality: we must erode the background in order to 
    # successfully subtract it from local_min, otherwise a line will 
    # appear along the background border (artifact of the local minimum filter)
    # http://www.scipy.org/doc/api_docs/SciPy.ndimage.morphology.html#binary_erosion
    eroded_background = morphology.binary_erosion(
        background, structure=neighborhood, border_value=1)
    # 
    # we obtain the final mask, containing only peaks, 
    # by removing the background from the local_min mask
    detected_minima = local_min - eroded_background
    return np.where(detected_minima)

which you can use like this:

arr=np.array([[[0,0,0,-1],[0,0,0,0],[0,0,0,0],[0,0,0,0],[-1,0,0,0]],
              [[0,0,0,0],[0,-1,0,0],[0,0,0,0],[0,0,0,-1],[0,0,0,0]]])
local_minima_locations = detect_local_minima(arr)
print(arr)
# [[[ 0  0  0 -1]
#   [ 0  0  0  0]
#   [ 0  0  0  0]
#   [ 0  0  0  0]
#   [-1  0  0  0]]

#  [[ 0  0  0  0]
#   [ 0 -1  0  0]
#   [ 0  0  0  0]
#   [ 0  0  0 -1]
#   [ 0  0  0  0]]]
print(local_minima_locations)
# (array([0, 0, 1, 1]), array([0, 4, 1, 3]), array([3, 0, 1, 3]))
print(arr[local_minima_locations])
# [-1 -1 -1 -1]

Nice! It runs about 65 times as fast as my original, and works for any number of dimensions.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

python - How to find the local minima of a smooth multidimensional arr...

python numpy discrete-mathematics mathematical-optimization
Rectangle 27 13

The location of the local minima can be found for an array of arbitrary dimension using Ivan's detect_peaks function, with minor modifications:

import numpy as np
import scipy.ndimage.filters as filters
import scipy.ndimage.morphology as morphology

def detect_local_minima(arr):
    # https://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array/3689710#3689710
    """
    Takes an array and detects the troughs using the local maximum filter.
    Returns a boolean mask of the troughs (i.e. 1 when
    the pixel's value is the neighborhood maximum, 0 otherwise)
    """
    # define an connected neighborhood
    # http://www.scipy.org/doc/api_docs/SciPy.ndimage.morphology.html#generate_binary_structure
    neighborhood = morphology.generate_binary_structure(len(arr.shape),2)
    # apply the local minimum filter; all locations of minimum value 
    # in their neighborhood are set to 1
    # http://www.scipy.org/doc/api_docs/SciPy.ndimage.filters.html#minimum_filter
    local_min = (filters.minimum_filter(arr, footprint=neighborhood)==arr)
    # local_min is a mask that contains the peaks we are 
    # looking for, but also the background.
    # In order to isolate the peaks we must remove the background from the mask.
    # 
    # we create the mask of the background
    background = (arr==0)
    # 
    # a little technicality: we must erode the background in order to 
    # successfully subtract it from local_min, otherwise a line will 
    # appear along the background border (artifact of the local minimum filter)
    # http://www.scipy.org/doc/api_docs/SciPy.ndimage.morphology.html#binary_erosion
    eroded_background = morphology.binary_erosion(
        background, structure=neighborhood, border_value=1)
    # 
    # we obtain the final mask, containing only peaks, 
    # by removing the background from the local_min mask
    detected_minima = local_min - eroded_background
    return np.where(detected_minima)

which you can use like this:

arr=np.array([[[0,0,0,-1],[0,0,0,0],[0,0,0,0],[0,0,0,0],[-1,0,0,0]],
              [[0,0,0,0],[0,-1,0,0],[0,0,0,0],[0,0,0,-1],[0,0,0,0]]])
local_minima_locations = detect_local_minima(arr)
print(arr)
# [[[ 0  0  0 -1]
#   [ 0  0  0  0]
#   [ 0  0  0  0]
#   [ 0  0  0  0]
#   [-1  0  0  0]]

#  [[ 0  0  0  0]
#   [ 0 -1  0  0]
#   [ 0  0  0  0]
#   [ 0  0  0 -1]
#   [ 0  0  0  0]]]
print(local_minima_locations)
# (array([0, 0, 1, 1]), array([0, 4, 1, 3]), array([3, 0, 1, 3]))
print(arr[local_minima_locations])
# [-1 -1 -1 -1]

Nice! It runs about 65 times as fast as my original, and works for any number of dimensions.

python - How to find the local minima of a smooth multidimensional arr...

python numpy discrete-mathematics mathematical-optimization
Rectangle 27 5

In Python3 there is the bit_length method:

>>> import math

>>> math.ceil(0x16.bit_length()/8)
1
>>> math.ceil(0x1234.bit_length()/8)
2
>>> math.ceil(0x12345678.bit_length()/8)
4
>>> math.ceil(0x1234567887654321.bit_length()/8)
8

In Python2.7, long integers also have the bit_length method:

>>> (10**20).bit_length()
67

>>> x = 123
>>> long(x).bit_length()
7

This is fantastic and answers my question. I am from the C world and am porting a C program to Python. These values I mention represent register width and they come in all sizes. I need to know the size of the register, thank you very much unutbu

However one issue please ... I have to use the variable name, i cannot use the actual number right? In my case the variable name is always reg. reg can hold any one of the values, byte, word ... Just for an example I gave different names. reg.bit_length does not work. So how do I do do the same work with a variable name?

@SanthoshKumar: reg.bit_length() will work. Are you sure you need to worry about this in Python? I suspect you are doing too-literal a translation from C to Python, and there is an easier way to accomplish your goal. What will you do with these "integer sizes"?

In C this is the program:

Sorry ignore the above comment. My idea is to check if each bit is set from Right to left (LSB to MSB) and if the bit set then to print out the meaning of that bit. Like the error registers or status registers. for i in range(reg): shift = reg & (1 << i) the problem here is val will be the actual value of the number where as I want it to be number of bits

python - How to find number of bytes in a variable? - Stack Overflow

python
Rectangle 27 5

In Python3 there is the bit_length method:

>>> import math

>>> math.ceil(0x16.bit_length()/8)
1
>>> math.ceil(0x1234.bit_length()/8)
2
>>> math.ceil(0x12345678.bit_length()/8)
4
>>> math.ceil(0x1234567887654321.bit_length()/8)
8

In Python2.7, long integers also have the bit_length method:

>>> (10**20).bit_length()
67

>>> x = 123
>>> long(x).bit_length()
7

This is fantastic and answers my question. I am from the C world and am porting a C program to Python. These values I mention represent register width and they come in all sizes. I need to know the size of the register, thank you very much unutbu

However one issue please ... I have to use the variable name, i cannot use the actual number right? In my case the variable name is always reg. reg can hold any one of the values, byte, word ... Just for an example I gave different names. reg.bit_length does not work. So how do I do do the same work with a variable name?

@SanthoshKumar: reg.bit_length() will work. Are you sure you need to worry about this in Python? I suspect you are doing too-literal a translation from C to Python, and there is an easier way to accomplish your goal. What will you do with these "integer sizes"?

In C this is the program:

Sorry ignore the above comment. My idea is to check if each bit is set from Right to left (LSB to MSB) and if the bit set then to print out the meaning of that bit. Like the error registers or status registers. for i in range(reg): shift = reg & (1 << i) the problem here is val will be the actual value of the number where as I want it to be number of bits

python - How to find number of bytes in a variable? - Stack Overflow

python
Rectangle 27 4

for fname in os.listdir('.'):
    if os.path.isdir(fname):
       pass  # do your stuff here for directory
    else:
       pass  # do your stuff here for regular file

I think you meant os.path, not of.path.

The comment indicating "do your stuff here" should not be inside the if block.

Python - how to find files and skip directories in os.listdir - Stack ...

python file-manager
Rectangle 27 4

occurrences = lambda s, lst: (i for i,e in enumerate(lst) if e == s)
list(occurrences(1, [1,2,3,1])) # = [0, 3]

python - How to find all occurrences of an element in a list? - Stack ...

python list
Rectangle 27 138

The python-magic method suggested by toivotuo is outdated. Python-magic's current trunk is at Github and based on the readme there, finding the MIME-type, is done like this.

# For MIME types
>>> import magic
>>> mime = magic.Magic(mime=True)
>>> mime.from_file("testdata/test.pdf")
'application/pdf'
>>>

thanks for the comment! please note, that "above" is a difficult concept in stackoverflow, since the ordering is grouped by votes and ordered randomly inside the groups. I am guessing you refer to @toivotuo's answer.

Yeh, I didn\t have enough "points" to create comments at the time of writing this reply. But I probably should have written it as a comment, so that the @toivotuo could have edited his question.

rpm -qf /usr/lib/python2.7/site-packages/magic.py -i URL : darwinsys.com/file Summary : Python bindings for the libmagic API rpm -qf /usr/bin/file -i Name : file URL : darwinsys.com/file python-magic from darwinsys.com/file and which comes with Linux Fedora works like @toivotuo's said. And seems more main stream.

Since the magic library is not a standard python lib, this is very clumsy :-( Isn't there some way how to use unix file command directly? Unfortunately s = os.system("file -b --mime-type /home/me/myfile.bz2") doesn't write the MIME into s, but only prints it to stdout :-(

Beware that the debian/ubuntu package called python-magic is different to the pip package of the same name. Both are import magic but have incompatible contents. See stackoverflow.com/a/16203777/3189 for more.

How to find the mime type of a file in python? - Stack Overflow

python mime
Rectangle 27 137

The python-magic method suggested by toivotuo is outdated. Python-magic's current trunk is at Github and based on the readme there, finding the MIME-type, is done like this.

# For MIME types
>>> import magic
>>> mime = magic.Magic(mime=True)
>>> mime.from_file("testdata/test.pdf")
'application/pdf'
>>>

thanks for the comment! please note, that "above" is a difficult concept in stackoverflow, since the ordering is grouped by votes and ordered randomly inside the groups. I am guessing you refer to @toivotuo's answer.

Yeh, I didn\t have enough "points" to create comments at the time of writing this reply. But I probably should have written it as a comment, so that the @toivotuo could have edited his question.

rpm -qf /usr/lib/python2.7/site-packages/magic.py -i URL : darwinsys.com/file Summary : Python bindings for the libmagic API rpm -qf /usr/bin/file -i Name : file URL : darwinsys.com/file python-magic from darwinsys.com/file and which comes with Linux Fedora works like @toivotuo's said. And seems more main stream.

Since the magic library is not a standard python lib, this is very clumsy :-( Isn't there some way how to use unix file command directly? Unfortunately s = os.system("file -b --mime-type /home/me/myfile.bz2") doesn't write the MIME into s, but only prints it to stdout :-(

Beware that the debian/ubuntu package called python-magic is different to the pip package of the same name. Both are import magic but have incompatible contents. See stackoverflow.com/a/16203777/3189 for more.

How to find the mime type of a file in python? - Stack Overflow

python mime
Rectangle 27 3

You can use the chardet package. Read this tutorial.

If you are using Ubuntu:

sudo apt-get install python3-chardet

If you are using pip:

pip install chardet2

thanks @kev, I ran into the universaldetector import error ,but then your link to the dip3 case study helped..much obliged

python - How to find the encoding of a python3 bytes object - Stack Ov...

python unicode encoding decode
Rectangle 27 3

def find_closest(alist, target):
    return min(alist, key=lambda x:abs(x-target))

X = [ 84.04467948,  52.42447842,  39.13555678,  21.99846595]
Y = [ 78.86529444,  52.42447842,  38.74910101,  21.99846595]

def list_matching(list1, list2):
    list1_copy = list1[:]
    pairs = []
    for i, e in enumerate(list2):
        elem = find_closest(list1_copy, e)
        pairs.append([i, list1.index(elem)])
        list1_copy.remove(elem)
    return pairs

This allows multiple match-uses for each element in y, which might be ok or not. It's quite a non-symmetric algorithm in that way (each X only used once, but not necessarily in Y).

@sascha Fixed, but it will work like you are checking second list against first. I think you can update it to work more smart.

@SardorbekImomaliev Sry for a late reply, how about a=[1,2,3,6] and b=[7,2,3,6]. It will lead a wrong result. But I think adding a sort will solve this problem. And in fact in my case, your code is good enough. Thanks a lot.

Actually, I don't think such a approach can work for all cases, as you always can reach the "not close to any"-element in your enumeration and then match it to the nearest value, which actually would match to another value, regardless which list you iterate over.

I have to provide another exception. I use sorted array, but the result is strange, X [ 80.06192623 51.27128419 33.81534928 25.49749915], y[ 73.26784071 51.27128419 26.60918437 25.49749915] and output is [[0, 0], [1, 1], [2, 3], [3, 2]]. But the good news is that if I sort the array from small to big, things are better.

python - How to find closest elements in two array? - Stack Overflow

python python-2.7
Rectangle 27 99

>>> import sys
>>> print sys.maxunicode
65535
Since the implementation of PEP 393 in Python 3.3, Unicode objects internally use a variety of representations

@Jan-PhilipGehrcke: deficient_unicode_build = (sys.maxunicode < 0x10ffff) works on any Python version (even if the flexible internal representation is used where sys.maxunicode == 0x10ffff). The flexible representations allows to get correct results like ucs4 did on previous versions while using less memory than ucs4 in some cases.

unicode - How to find out if Python is compiled with UCS-2 or UCS-4? -...

python unicode ucs2