Posts

Showing posts with the label string

Iterate over every nth element in string in loop - python

Iterate over every nth element in string in loop - python How can iterate over every second element in string ? One way to do this would be(If I want to iterate over nth element): sample = "This is a string" n = 3 # I want to iterate over every third element i = 1 for x in sample: if i % n == 0: # do something with x else: # do something else with x i += 1 Is thery any "pythonic" way to do this? (I am pretty sure my method is not good) for i,x in enumerate(sample,1): – Jean-François Fabre Jul 1 at 9:11 for i,x in enumerate(sample,1): 3 Answers 3 If you want to do something every nth step, and something else for other cases, you could use enumerate to get the index, and use modulus: enumerate sample = ...

convert object to string using JSON.stringify show empty object

Image
convert object to string using JSON.stringify show empty object I'm trying to convert object to string using JSON.stringify and I get empty object console.log('typeof',typeof e,' e value is',e, 'JSON stringify is',JSON.stringify(e)) the error message when I try to print typeof object e value is Error: Error: A network error (such as timeout, interrupted connection or unreachable host) has occurred. JSON stringify is {} @ManuallyOverridden console.log is variadic — you can pass multiple objects, which are separated by a , . – Mark Meyer Jul 1 at 6:38 console.log , It looks like e is an error object. In node, stringifying an error results in {} . Not sure how you are running your code. – Mark Meyer Jul 1 at 6:40 ...

How to extract a substring from inside a string in Python?

How to extract a substring from inside a string in Python? Let's say I have a string 'gfgfdAAA1234ZZZuijjk' and I want to extract just the '1234' part. 'gfgfdAAA1234ZZZuijjk' '1234' I only know what will be the few characters directly before AAA , and after ZZZ the part I am interested in 1234 . AAA ZZZ 1234 With sed it is possible to do something like this with a string: sed echo "$STRING" | sed -e "s|.*AAA(.*)ZZZ.*|1|" And this will give me 1234 as a result. 1234 How to do the same thing in Python? 12 Answers 12 Using regular expressions - documentation for further reference import re text = 'gfgfdAAA1234ZZZuijjk' m = re.search('AAA(.+?)ZZZ', text) if m: found = m.group(1) # found: 1234 or: import re text = 'gfgfdAAA1234ZZZuijjk' try: found = re.search('AAA(.+?)ZZZ', text).group(1) except Attribute...

How to check if “ ” exist?

How to check if “ ” exist? When I print a variable, I get a blank result and when I inspect the element I see that   .   I tried to check if the variables is empty or equals this value: ir( empty($string) || $string == " " || strpos($string, " ") || $string == " "){ //Do Something. } But the code inside that condition is not executed. When I var_dump($string) , I get: var_dump($string) string(2) " " What should I do to check if the variable equals or contain that? I would suggest to check for null or empty string using trim(): (!isset($string) || trim($string) === '') – wp78de Jul 1 at 6:00 (!isset($string) || trim($string) === '') what is ir ?? – smith ...

my function() is not working or the value is not converting to a number

my function() is not working or the value is not converting to a number Been stuck here ALL day long trying to figure out what is wrong with my syntax. Here’s my code… I’d like to verify if Number1 is > than Number 2 . However, whenever I tried to run the code I always getting false. Please help me.. PS. I am new to coding :( var num1 = document.getElementById('num1'); var num2 = document.getElementById('num2'); var x = Number(num1); var y = Number(num2); function higherThan(x, y){ x > y? alert(true) : alert(false); } Numb1: <input type="number" id="num1"><br> Numb2: <input type="" id="num2"><br> <button onclick="higherThan();">Is Higher Than?</button> document.getElementById('num1') gets the whole element. Change it to document.getElementById('num1').value to get the value – Manually Overridden ...

How do I use variables in single quoted strings?

How do I use variables in single quoted strings? I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it). echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE} any help would be greatly appreciated 7 Answers 7 Variables are expanded in double quoted strings, but not in single quoted strings: $ name=World $ echo "Hello $name" Hello World $ echo 'Hello $name' Hello $name If you can simply switch quotes, do so. If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument: $ echo 'single quoted. '"Double quoted. "'Single quoted again.' single quoted. Double quoted. Single quoted again. $ echo '"$name" has the value '"$name...

check string lifetime duration

check string lifetime duration I am currently wondering if it is possible to check if a C-string has a static lifetime duration or not when only the pointer to the string is known. Basically, I want to implement such a function: bool isStaticString(const char *str) { //do magic stuff } and the results should be like this: isStaticString("Hello World!"); //returns true char str[5] = {''}; isStaticString(str); //returns false Is there any portable way to implement such a function? PS: I hope such question was not already asked,I did not really know what to search for. My title did not give me any results. No, and why do you need this? – UnholySheep Jun 30 at 20:19 Are you asking about C or C++? – melpomene Jun 30 at 20:22 ...

swift: working with string

swift: working with string We have a simple string: let str = ""abc", "def","ghi" , 123.4, 567, "qwe,rty"" If we do this: let parsedCSV = str .components(separatedBy: .newlines) .filter { !$0.isEmpty } .map { $0.components(separatedBy: ",") } .map { $0.map { $0.trimmingCharacters(in: .whitespaces) } } print(parsedCSV) we get this: [[""abc"", ""def"", ""ghi"", "123.4", "567", ""qwe", "rty""]] Is there a simple solution (using functional programming) not to split the last element "qwe,rty" , because we know that it's one whole thing? "qwe,rty" That's not so simple. You need context to handle the " and in general it's easier to just go loop by the columns one by one. – Sulthan ...

Valgrind reports SIGSEGV and use of uninitialised value

Valgrind reports SIGSEGV and use of uninitialised value Edit: All valgrind errors where solved by initialising i . i I am working with strings; reading strings from a file, separating them using strsep() , converting them to int using atol() . My code compiles on gcc and runs without errors, but when checked with valgrind I get the error mentioned in the title. strsep() atol() I am trying populate a two-dimensional array grid[20][20] with int values from a text file. The text file contains lines of numbers separated by spaces, something like 20 81 65 07 54 72 13 28 66 95 00 20 00 84 06 30 85 43 15 73n grid[20][20] 20 81 65 07 54 72 13 28 66 95 00 20 00 84 06 30 85 43 15 73n My code, (excluding all of the file IO) is as below. (Note that I do handle the file correctly: checking the result of fopen , closing with fclose ). fopen fclose #include <stdio.h> #include <stdlib.h> #include <string.h> #define STR_LEN 20 #define NUM_LINES 20 #define MAX_SIZE 100 int ma...

Regular Expression to extract data between two bracket

Regular Expression to extract data between two bracket For example I have a string line as follows What is your family Occupation? [RMB 2011] I want to extract two data from here. The RMB part can be of any length. I have used following regex pattern to extract the data ^[([A-Z]{3}sdddd[)]]$ But it does not match anything in the string. Please let me know what I am doing wrong. 1 Answer 1 You do not need the string start with ] . Try this online: ] [([A-Z]{3})s(d{4})] And if RMB part can be of any length , you can change [A-Z]{3} to [A-Z]{1,} . [A-Z]{3} [A-Z]{1,} Exactly what I wanted :). Thanks – Mars Moon Jun 30 at 9:54 By clicking "Post Your Answer", you acknowledge that you have read our updated ...