Python printing Deepdiff value - python-2.7

I am using the deepdiff function to find the difference between 2 dictionaries, which gives the output as: A = {'dictionary_item_added': set(["root['mismatched_element']"])}. How to print just 'mismatched_element'?

Try this:
set_item = A['dictionary_item_added'].pop()
print set_item[set_item.find("['")+2 : set_item.find("']")]
The first line gets the element from the set, the second removes the [] and everything around them, and prints.
This code does the specific task you asked for, but it's hard to generalize the solution without a more generalized question..

Related

Why i'm facing Regex detection problem inside a loop in Python

I'm facing very weird problem while using regex.
weight='abcd'
description='ml'
for symbol in Syntax['symbol']:
print(symbol)
weight=re.findall(symbol,description)
print(weight)
output --> []
Syntax is a data frame that contains different units, also contains " ml " inside symbol column, i have manually printed the symbol variable it prints required unit that is "ml" which will be set as pattern in loop but still it returns [] OR None while using re.match().
But when i try code below
description='ml'
pattern='ml'
print(re.findall(pattern,description)
it prints "ml", Why ??? Both above and Top code are logically same.
In the top code, you're only printing the result of the final regex search, since print(weight) is outside your loop. It's all well and good if "ml" is somewhere in your data frame, but if the last value of symbol doesn't match anything in description, the regex won't find any matches and you won't get any output.
Try printing weight inside the for loop and see what output you get.
description='ml'
weight=0
for symbol in self.units['symbol']:
print("units in Syntax dataframe : ",symbol)
weight=re.findall(symbol,description)
if weight!=[]:
break
print(weight)
I have understood the problem, i was not stopping the loop when the 'ml' is found, that's why it was printing [] or None

Is there even a slight possibility to process two lists in one single list comprehension line?

I would like to ask if there's a possibility to process more than one list in just a single line with list comprehension? I'm using Python 2.7 .
Here is what the code looks like:
n=[1,2,3,4,5,6,7]
m=[1,7]
c=[]
for x in m:
if x in n:
c.append(x)
n.pop(n.index(x))
print n
print c
The output is:
[2,3,4,5,6]
[1,7]
Now I'm wondering if I could turn the code (line 5 to line 8) into a single line using a list comprehension?
I would appreciate your advice. Let me know if my question has a duplicate. Thank you very much.
You can do it this way since popping a value from the list returns the value
n=[1,2,3,4,5,6,7]
m=[1,7]
c=[n.pop(n.index(x)) for x in m if x in n]
print n
print c
n=[1,2,3,4,5,6,7]
m=[1,7]
print set(n)-set(m)
> [2,3,4,5,6]
Assign the sets to their own variables if you need to perform additional operations. Converting to a set will take some time on a big list but then membership, subtraction, union or intersection operations should be very fast.

Can you disable all print statements in SML

I currently have a lot of print statements in SML code, and I'm traversing a very large tree, so it takes a while for all the print statements to be printed, but right now I don't want to see any print statements and just want to see it run as fast as possible. But I don't want to comment all the prints because I later need them again to debug something else.
So I just want to be able to temporarily disable them for this run of the code.
I'm using the SML/NJ compiler.
As the first line in your code put the definition
fun print x = ();
Then -- your code will still work but the prints will do nothing.
Delete that line when you want to re-enable print.

How to create a list of numbers 1-1000 to simplify?

I am working on a "Whats my number?" program (http://goo.gl/upgkZ2) as posted on reddit and I was wondering if there was a way I could have a list of numbers 1-1000 and remove groups of numbers that follow a certain criteria. I was wondering if there was any simpler way to do this?
You can create a list of 1-1000 in a simpler way by using:
tons = list(xrange(1000))
You don't actually need a list at all to solve this problem (well, find the two solutions to this problem). Loop over the numbers and continue (return to the top of the loop for the next iteration) for each condition that fails to be met:
for i in xrange(10,1001):
s_i = str(i)
if '1' in s_i or '7' in s_i:
continue
if sum([int(d) for d in s_i]) > 10:
continue
...
print i, 'satisfies the conditions.'
You need to use filter on the initial list removing each case one at a time
For efficiency, cut out the biggest ones first, like the last case removes 90% of the list
You can also use numpy to create an array:
import numpy as np
a=np.arange(1,1001)

Erlang - Can one use Lists:append for adding an element to a string?

Here is my function that parses an addition equation.
expr_print({num,X}) -> X;
expr_print({plus,X,Y})->
lists:append("(",expr_print(X),"+",expr_print(Y),")").
Once executed in terminal it should look like this (but it doesn't at the moment):
>math_erlang: expr_print({plus,{num,5},{num,7}}).
>(5+7)
Actually one could do that, but it would not work the way wish in X in {num, X} is a number and not string representation of number.
Strings in Erlang are just lists of numbers. And if those numbers are in wright range they can be printed as string. You should be able to find detail explenation here. So first thing you wold like to do is to make sure that call to expr_print({num, 3}). will return "3" and not 3. You should be able to find solution here.
Second thing is lists:append which takes only one argument, list of list. So your code could look like this
expra_print({num,X}) ->
lists:flatten(io_lib:format("~p", [X]));
expr_print({plus,X,Y})->
lists:append(["(", expr_print(X),"+",expr_print(Y), ")"]).
And this should produce you nice flat string/list.
Another thing is that you might not need flat list. If you planning to writing this to file, or sending over TCP you might want to use iolist, which are much easier to create (you could drop append and flatten calls) and faster.