how to find in a parent string list, the indexes corresponding to a child string list

  • Last Update :
  • Techknowledgy :

np.searchsorted can take a sorting permutation as an optional argument:

>>> sorter = np.argsort(Shop_Products) >>>
   sorter[np.searchsorted(Shop_Products, Shop_Query, sorter = sorter)]
array([4, 1]) >>>
   Shop_Inventory[sorter[np.searchsorted(Shop_Products, Shop_Query, sorter = sorter)]]
array([8, 6])

This is probably faster than np.in1d, which also needs to sort the array. It also returns values in the same order as they come up in Shop_Query, while np.1d will return the values in the order they come up in Shop_Products, regardless of the ordering in the query:

>>> np.in1d(Shop_Products, ['Cheese', 'Bread']).nonzero()
   (array([1, 4]), ) >>>
   np.in1d(Shop_Products, ['Bread', 'Cheese']).nonzero()
   (array([1, 4]), )

You can use in1d() and nonzero() to find the indices of the items in Shop_Products:

>>> np.in1d(Shop_Products, Shop_Query).nonzero()
   (array([1, 4]), )

To look up the corresponding values in Shop_Inventory, use this result to index the array:

>>> i = np.in1d(Shop_Products, Shop_Query).nonzero() >>>
   Shop_Inventory[i]
array([6, 8])

Suggestion : 2

1.)Put your complete string into str variable. 2.)Take XmlDocument object and load the str file. 3.)Pick those nodes using "selectNodes" and "selectSingleNode" functions. ,Ignoring the order of the output nested list elements and assuming length of input lists are equal:,AngularJS bind a selected option from an array of objects to a model that stores the id only,If there is only atmost one blank between the elements, then create an index as in the OP's post and then do the replacement by the element corresponding to the index subtracted 1

>>> sorter = np.argsort(Shop_Products) >>>
   sorter[np.searchsorted(Shop_Products, Shop_Query, sorter = sorter)]
array([4, 1]) >>>
   Shop_Inventory[sorter[np.searchsorted(Shop_Products, Shop_Query, sorter = sorter)]]
array([8, 6])
use strict;
use warnings 'all';

my % side = (i => 24.56, j => 3.56);

my($sside, $bside) = $side {
   i
} < $side {
   j
} ? qw / i j / : qw / j i / ;

$side {
   $sside
} *= 20;
$side {
   $bside
} *= 21;
/**
 * @brief: Shows the subpages of the current page, or
 *         the adjacent sibling pages.
 **/
function show_subpages(){

    global $post;
    $subpages = wp_list_pages( array(
        'echo'=>0,
        'title_li'=>'',
        'depth'=>2,
        'child_of'=> ( $post->post_parent == 0 ? $post->ID : $post->post_parent)
    ));
    if ( !empty($subpages) ) {

        if ( $post->post_parent != 0 ) {
            echo '<p class="parent-link"><em>'. __('Back to') .' <a href="'. get_permalink($post->post_parent) .'">'. get_the_title($post->post_parent) .'</em></a><p>';
        }
        echo '<ul>';
        echo $subpages;
        echo '</ul>';
    } else {
        echo 'no subpages';
    }
}
d = REXML::Document.new('<root></root>')
root = d.root
e0 = REXML::Element.new('foo')
e1 = REXML::Element.new('bar')
root.add(e0) # =>
<foo />
root.add(e1) # =>
<bar />
root.add(e0) # =>
<foo />
root.add(e1) # =>
<bar />
root.index(e0) # => 0
root.index(e1) # => 1
def find_str(s, char):
   index = 0

if char in s:
   c = char[0]
for ch in s:
   if ch == c:
   if s[index: index + len(char)] == char:
   return index

index += 1

return -1

print(find_str("Happy birthday", "py"))
print(find_str("Happy birthday", "rth"))
print(find_str("Happy birthday", "rh"))
library(zoo)
na.locf(replace(string, string == "", NA))
#[1]
"1"
"2"
"3"
"3"
"5"
"6"
"6"

i1 < -which(string == "")
string[i1] < -string[i1 - 1]

Suggestion : 3

Last Updated : 19 Jul, 2022

Examples: 

1) Input: txt[] = "BACDGABCDA"
pat[] = "ABCD"
Output: Found at Index 0
Found at Index 5
Found at Index 6
2) Input: txt[] = "AAABABAA"
pat[] = "AABA"
Output: Found at Index 0
Found at Index 1
Found at Index 4

Approach 1 :

Brute Force:
   Consider the Input txt[] = "BACDGABCDA"
pat[] = "ABCD".
Occurrences of the pat[] and its permutations are found at indexes 0, 5, 6.
The permutations are BACD, ABCD, BCDA.
Let 's sort the pat[] and the permutations of pat[] in txt[].
pat[] after sorting becomes: ABCD
permutations of pat[] in txt[] after sorting becomes: ABCD, ABCD, ABCD.
So we can say that the sorted version of pat[] and sorted version of its
permutations yield the same result.

Found at Index 0
Found at Index 5
Found at Index 6

Found at Index 0
Found at Index 5
Found at Index 6

Suggestion : 4

Last modified: Aug 14, 2022, by MDN contributors

// Note that parg is an object reference to a <p> element

// First check that the element has child nodes
if (parg.hasChildNodes()) {
  let children = parg.childNodes;

  for (const node of children) {
    // Do something with each child as children[i]
    // NOTE: List is live! Adding or removing children will change the list's `length`
  }
}
// This is one way to remove all children from a node
// box is an object reference to an element
while (box.firstChild) {
   //The list is LIVE so it will re-index each call
   box.removeChild(box.firstChild);
}

Suggestion : 5

The advantage to using a string is that the in-python linkage between user and user_preference is resolved only when first needed, so that table objects can be easily spread across multiple modules and defined in any order.,The behavior we’ve seen in tutorials and elsewhere involving foreign keys with DDL illustrates that the constraints are typically rendered “inline” within the CREATE TABLE statement, such as:,ForeignKeyConstraint.use_alter and ForeignKey.use_alter, when used in conjunction with a drop operation, will require that the constraint is named, else an error like the following is generated:,The naming convention feature may be combined with these types as well, normally by using a convention which includes %(constraint_name)s and then applying a name to the type:

user_preference = Table('user_preference', metadata_obj,
   Column('pref_id', Integer, primary_key = True),
   Column('user_id', Integer, ForeignKey("user.user_id"), nullable = False),
   Column('pref_name', String(40), nullable = False),
   Column('pref_value', String(100))
)
ForeignKey(user.c.user_id)
invoice = Table('invoice', metadata_obj,
   Column('invoice_id', Integer, primary_key = True),
   Column('ref_num', Integer, primary_key = True),
   Column('description', String(60), nullable = False)
)
invoice_item = Table('invoice_item', metadata_obj,
   Column('item_id', Integer, primary_key = True),
   Column('item_name', String(60), nullable = False),
   Column('invoice_id', Integer, nullable = False),
   Column('ref_num', Integer, nullable = False),
   ForeignKeyConstraint(['invoice_id', 'ref_num'], ['invoice.invoice_id', 'invoice.ref_num'])
)
CREATE TABLE addresses(
   id INTEGER NOT NULL,
   user_id INTEGER,
   email_address VARCHAR NOT NULL,
   PRIMARY KEY(id),
   CONSTRAINT user_id_fk FOREIGN KEY(user_id) REFERENCES users(id)
)
node = Table(
   'node', metadata_obj,
   Column('node_id', Integer, primary_key = True),
   Column(
      'primary_element', Integer,
      ForeignKey('element.element_id')
   )
)

element = Table(
   'element', metadata_obj,
   Column('element_id', Integer, primary_key = True),
   Column('parent_node_id', Integer),
   ForeignKeyConstraint(
      ['parent_node_id'], ['node.node_id'],
      name = 'fk_element_parent_node_id'
   )
)

Suggestion : 6

Use the Element.select(String selector) and Elements.select(String selector) methods:,Select returns a list of Elements (as Elements), which provides a range of methods to extract and manipulate the results.,The select method is available in a Document, Element, or in Elements. It is contextual, so you can filter by selecting from a specific element, or by chaining select calls.,:has(selector): find elements that contain elements matching the selector; e.g. div:has(p)

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

Elements links = doc.select("a[href]"); // a with href
Elements pngs = doc.select("img[src$=.png]");
// img with src ending .png

Element masthead = doc.select("div.masthead").first();
// div with class=masthead

Elements resultLinks = doc.select("h3.r > a"); // direct a after h3

Suggestion : 7

Apply three different styles to list items to demonstrate that :eq() is designed to select a single element while :nth-child() or :eq() within a looping construct such as .each() can select multiple elements.,Note that since JavaScript arrays use 0-based indexing, these selectors reflect that fact. This is why $( ".myclass:eq(1)" ) selects the second element in the document with the class myclass, rather than the first. In contrast, :nth-child(n) uses 1-based indexing to conform to the CSS specification., version added: 1.8jQuery( ":eq(-index)" ) indexFromEnd: Zero-based index of the element to match, counting backwards from the last element. ,indexFromEnd: Zero-based index of the element to match, counting backwards from the last element.

<!doctype html>
<html lang="en">

<head>
   <meta charset="utf-8">
   <title>eq demo</title>
   <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>

<body>
   <table border="1">
      <tr>
         <td>TD #0</td>
         <td>TD #1</td>
         <td>TD #2</td>
      </tr>
      <tr>
         <td>TD #3</td>
         <td>TD #4</td>
         <td>TD #5</td>
      </tr>
      <tr>
         <td>TD #6</td>
         <td>TD #7</td>
         <td>TD #8</td>
      </tr>
   </table>
   <script>
      $("td:eq( 2 )").css("color", "red");
   </script>
</body>

</html>
<!doctype html><html lang="en"><head>  <meta charset="utf-8">  <title>eq demo</title>  <script src="https://code.jquery.com/jquery-3.5.0.js"></script></head><body> <ul class="nav">  <li>List 1, item 1</li>  <li>List 1, item 2</li>  <li>List 1, item 3</li></ul><ul class="nav">  <li>List 2, item 1</li>  <li>List 2, item 2</li>  <li>List 2, item 3</li></ul> <script>// Applies yellow background color to a single <li>$( "ul.nav li:eq(1)" ).css( "backgroundColor", "#ff0" ); // Applies italics to text of the second <li> within each <ul class="nav">$( "ul.nav" ).each(function( index ) {  $( this ).find( "li:eq(1)" ).css( "fontStyle", "italic" );}); // Applies red text color to descendants of <ul class="nav">// for each <li> that is the second child of its parent$( "ul.nav li:nth-child(2)" ).css( "color", "red" );</script> </body></html>
<!doctype html><html lang="en"><head>  <meta charset="utf-8">  <title>eq demo</title>  <style>  .foo {    color: blue;    background-color: yellow;  }  </style>  <script src="https://code.jquery.com/jquery-3.5.0.js"></script></head><body> <ul class="nav">  <li>List 1, item 1</li>  <li>List 1, item 2</li>  <li>List 1, item 3</li></ul><ul class="nav">  <li>List 2, item 1</li>  <li>List 2, item 2</li>  <li>List 2, item 3</li></ul> <script>$( "li:eq(-2)" ).addClass( "foo" )</script> </body></html>