py notes 4

76

list files with specific pattern python

To list files with a specific pattern in Python, you can use the glob module, which provides a convenient way to search for files matching a specified pattern. Here's how you can do it:

import glob

# Directory path
directory = '/path/to/directory'

# Desired pattern
pattern = '*.txt'  # For example, to list all files ending with '.txt'

# List files with the specific pattern
files_with_pattern = glob.glob(directory + '/' + pattern)

# Print the list of files
for file_path in files_with_pattern:
    print(file_path)

In this example:

  • Replace '/path/to/directory' with the path to the directory where you want to search for files.

  • Set pattern to the specific pattern you want to match. For example, '*.txt' will match all files ending with '.txt'.

  • glob.glob(directory + '/' + pattern) searches for files matching the specified pattern in the specified directory and returns a list of file paths that match the pattern.

  • Finally, the list of file paths matching the pattern is printed.

Using the glob module is a concise and efficient way to list files with a specific pattern in Python.


77

get numbers in python using regex

To extract numbers from a string in Python using regular expressions, you can use the re.findall() function with a regex pattern that matches numbers. Here's how you can do it:

Output:

In this example:

  • r'\d+' is a regex pattern that matches one or more digits (\d matches any digit character and + matches one or more occurrences of the preceding pattern).

  • re.findall() searches the input text for all non-overlapping matches of the regex pattern and returns them as a list of strings.

  • The numbers list contains all the numbers found in the input text.

You can adjust the regex pattern as needed to match specific types of numbers or formats in your input text.


78

glob.glob with natural sorting + python

To perform natural sorting with glob.glob() in Python, you can use the sorted() function along with the key parameter to sort the file paths returned by glob.glob() in a natural order. Here's how you can do it:

In this example:

  • Replace '/path/to/directory' with the path to the directory where you want to search for files.

  • Set pattern to the specific pattern you want to match. For example, '*.txt' will match all files ending with '.txt'.

  • glob.glob(directory + '/' + pattern) searches for files matching the specified pattern in the specified directory and returns a list of file paths.

  • natural_sort_key() is a custom sorting function that extracts numbers from the file paths using a regex pattern and sorts them naturally.

  • The sorted() function is used to sort the file paths based on the natural sorting key.

By using natural sorting, the file paths will be sorted in a human-readable order, such as file1.txt, file2.txt, file10.txt, instead of the default lexicographic order, which would result in file1.txt, file10.txt, file2.txt.


79

how many times specific letter repeated in python

To count how many times a specific letter is repeated in a string in Python, you can use the count() method of strings. Here's how you can do it:

This will output:

In this example:

  • input_string.count(letter_to_count) counts the occurrences of the specified letter ("l") in the input string ("hello world").

  • The variable count holds the count of occurrences of the letter.

  • Finally, we print the count using an f-string for formatting.

You can replace "hello world" with any string, and "l" with any letter you want to count occurrences for.


80

how-do-i-sort-a-dictionary-by-value

To sort a dictionary by its values in Python, you can use the sorted() function with a custom sorting key. Here's how you can do it:

This will output:

In this example:

  • We use the sorted() function to sort the items of the dictionary (my_dict.items()) based on their values.

  • The key parameter specifies a custom sorting key, which is a lambda function that extracts the value (item[1]) from each key-value pair.

  • The sorted() function returns a list of tuples containing the sorted key-value pairs.

  • We convert this list of tuples back into a dictionary using the dict() constructor to obtain the sorted dictionary.

Now, sorted_dict contains the original dictionary sorted by its values in ascending order. If you want to sort in descending order, you can add the reverse=True parameter to the sorted() function.


81

count lines in string python

To count the number of lines in a string in Python, you can use the splitlines() method to split the string into lines and then use the len() function to count the number of resulting lines. Here's how you can do it:

Output:

In this example:

  • multiline_string.splitlines() splits the multiline string into a list of lines.

  • len() returns the number of elements in the list, which corresponds to the number of lines in the original string.

You can replace multiline_string with any string containing multiple lines, and the code will count the number of lines accordingly.


82

sort dictionary by key python

To sort a dictionary by its keys in Python, you can use the sorted() function with the items() method of the dictionary. Here's how you can do it:

This will output:

In this example:

  • We use the sorted() function to sort the items in the dictionary (my_dict.items()) based on their keys.

  • The sorted() function returns a list of tuples containing the sorted key-value pairs.

  • We convert this list of tuples back into a dictionary using the dict() constructor to obtain the sorted dictionary.

Now, sorted_dict contains the original dictionary sorted by its keys in lexicographical order. If you want to sort in descending order, you can add the reverse=True parameter to the sorted() function.


83

restrict the list with max item count in Python

To restrict a list to a maximum number of items in Python, you can use slicing. Slicing allows you to create a new list containing a subset of the elements from the original list. Here's how you can do it to limit the list to a maximum number of items:

Output:

In this example:

  • original_list[:max_items] slices the original_list from the beginning up to max_items elements.

  • The resulting restricted_list contains at most max_items elements from the original_list.

You can adjust the max_items variable to set the desired maximum number of items to keep in the restricted list.


84


85


86


87


88


89


90


Last updated