Traversing through a list that has b' ahead - python

When I write
a1 = list([b'1,690569\n1,315892\n1,189226\n2,834328\n2,1615927\n2,1194519\n'])
print(a1)
for edge_ in a1:
print('edge =' + str(edge_))
z[(edge_[0], edge_[1])] = 1
print('edge_0 =' + str(edge_[0]))
print('edge_1 =' + str(edge_[1]))
print(z)
I get the output as
[b'1,690569\n1,315892\n1,189226\n2,834328\n2,1615927\n2,1194519\n']
edge =b'1,690569\n1,315892\n1,189226\n2,834328\n2,1615927\n2,1194519\n'
edge_0 =49
edge_1 =44
{(49, 44): 1}
Can anyone explain why it is 49 and 44? These values are coming irrespective of the element inside the list.

Firstly, as others have already mentioned, your array below is a byte array. This is evident due to the 'b' at the start. You don't need to use 'list()' by the way.
a1 = [b'1,690569\n1,315892\n1,189226\n2,834328\n2,1615927\n2,1194519\n']
Given that z is an empty dictionary (i.e. z = dict())
Below is just adding a tuple as a key and an integer as value:
z[(edge_[0], edge_[1])] = 1
We can see the following:
edge_ = a1[0] = b'1,690569\n1,315892\n1,189226\n2,834328\n2,1615927\n2,1194519\n'
edge_[0] = a1[0][0] = ord(b'1') = 49
edge_[1] = a1[0][1] = ord(b',') = 44
Hence z[(edge_[0], edge_[1])] = 1 becomes:
z[(49, 44)] = 1
z = {(49, 44): 1}

Related

How do I EXTRACT all values ending in .000 and print them?

OK so I have a for loop running an equation iterating it a 0.005. I need it to print any "L" value ending in .000 and nothing else. How do I do that?
import numpy as np
import math
for D in np.arange(7, 9, 0.0050):
N = 28
n = 11
A = 7.32
P = 0.25
C = float(D)/float(P) #(P/8)*(2*L-N-n+((2*L-N-n)**(2)-0.810*(N-n)**(2))**(0.5)
L = 2*C+(N+n)/2+A/C
print("L = ", "%.3f"% float(L), '\n')
Problems I had:
I had to use np.arange as it wouldn't allow a float in a loop. If you can show me how to get around that, that'd be great.
When using np.arange, I would get "D" values like
D = 7.0009999999999994
L = 75.76939122982431
D = 7.001499999999999
L = 75.7733725630222
D = 7.001999999999999
L = 75.77735389888602
D = 7.002499999999999
L = 75.78133523741519
this causes errors when I go to use these numbers later in the code
this loop takes forever to compute. If there's a better way, show me. I have to make this quick or it won't get used.
This post explained why float is not working well in python:
numpy arange: how to make "precise" array of floats?
I used below code and it gave me precise decimal 3 numbers for both D & L in your calculation:
for i in range(7000, 9000, 5):
D = i/1000
print(D)
N = 28
n = 11
A = 7.32
P = 0.25
C = float(D)/float(P) #(P/8)*(2*L-N-n+((2*L-N-n)**(2)-0.810*(N-n)**(2))**(0.5)
L = 2*C+(N+n)/2+A/C
print("L = ", "%.3f"% float(L), '\n')
L3 is the variable
"%.3f"% is the 3rd decimal place
% 1 == 0 I'm not sure what this does, but 0 is the number I'm looking for.
if float("%.3f"% L3) % 1 == 0: #L3 is the variable
do_something()

for each gives different result?

I tried to add the comma seprated value between the : seprated then multiply the whole value
For example, consider my value is 1,2,3:4,5,6
I want to add the 1+2+3 ,and 4+5+6 then multiply the result of this value so answer is 6 * 15 = 90
For my bellow data i want the result is 7.224 but this script gives 61.658886435
I don't know what is the problem in my script'
ar = "0.212,1.231,0.112:1.001,3.212,0.002:0.002,0.0001,1.1"
x_data = ar.split(":")
x_final = 1
x_add = 0
for i in x_data:
x_each = i.split(",")
for j in x_each:
x_add = x_add + float(j)
x_final = x_add * x_final
print x_final
Is any possible way to get the result without iterating loop? For above problem
This problem could be also solved in a functional way:
You have to multiply all values in the list - this is what functools.reduce + operator.mul for
You have to sum up all values in all inner lists - this is what sum for
Example:
In [5]: ar = "0.212,1.231,0.112:1.001,3.212,0.002:0.002,0.0001,1.1"
In [6]: import operator
In [7]: import functools
In [8]: functools.reduce(operator.mul, (sum(float(x) for x in s.split(',')) for s in ar.split(':')))
Out[8]: 7.223521582500001
I don't necessarily recommend this complicated expression, but you can do it with list comprehensions and avoid the for loops:
import operator
ar = "0.212,1.231,0.112:1.001,3.212,0.002:0.002,0.0001,1.1"
reduce(operator.mul, [sum([float(n) for n in e]) for e in [x.split(',') for x in ar.split(":")]], 1)
Use missed the initialize value as zero (x_add = 0) in each iterating. So your script add with the previous values
ar = "0.212,1.231,0.112:1.001,3.212,0.002:0.002,0.0001,1.1"
x_data = ar.split(":")
x_final = 1
for i in x_data:
x_each = i.split(",")
x_add = 0 # Here you not initialize it
for j in x_each:
x_add = x_add + float(j)
x_final = x_add * x_final
print x_final
!!! As from #jpmc26 and #soon comment. Avoid using eval, and conform your input string format.
Without looping use regex for to do it
Use regex for solve your problem without looping.
ar = "0.212,1.231,0.112:1.001,3.212,0.002:0.002,0.0001,1.1"
import re
ar = "("+ar #Add the ( with your data
ar = re.sub(r",","+",ar) #Substitute with + instead of ,
ar = re.sub(r"(?=\:|$)",")",ar) #look ahead for add `)` after colon
ar = re.sub(r"(?<=)\:","*(",ar) #Replace the color with *
#NOw you data look likes(0.212+1.231+0.112)*(1.001+3.212+0.002)*(0.002+0.0001+1.1)
#Finally evaluvate the string as a expression
print eval(ar)

How to fill a liblas.point.Point object using Liblas module?

I am using liblas in Python to read, manipulate and write a special point format *.las. I have a string as
s = "309437.95 6959999.84 118.98 16 1 1 0 0 1 0 112.992 5.9881"
Where the first is the X, the second the Y, the third element the Z etc.
Using Liblas, I create an empty liblas.point.Point object
>>> pt = liblas.point.Point()
>>> pt
<liblas.point.Point object at 0x0000000005194470>
After that I need to fill this object because is empty.
>>> pt.x, pt.y,pt.z
(0.0, 0.0, 0.0)
probably using
>>> pt.get_x
<bound method Point.get_x of <liblas.point.Point object at 0x0000000005194470>>
I wish to say thanks for all help and suggestion, I really need to solve this step.
from suggestion of Martijn Pieters
s = "%s %s %s" % (s, value, nh)
>>> s
'309437.95 6959999.84 118.98 16 1 1 0 0 1 0 112.992 5.9881'
# create a liblas.point.Point
pt = liblas.point.Point()
pt.x = float(s.split()[0])
pt.y = float(s.split()[1])
pt.z = = float(s.split()[11]) # the new Z value
pt.intensity = = int(s.split()[3])
pt.return_number= int(s.split()[4])
pt.number_of_returns = int(s.split()[5])
pt.scan_direction = int(s.split()[6])
pt.flightline_edge = int(s.split()[7])
pt.classification = int(s.split()[8])
pt.scan_angle = int(s.split()[9])
There are raw_x, raw_y and raw_z properties on a Point object; simply set those:
pt.raw_x = 309437.95
pt.raw_y = 6959999.84
pt.raw_z = 118.98
There are also x, y and z properties; it is not immediately clear from the source code what the difference is between the two types:
pt.x = 309437.95
pt.y = 6959999.84
pt.z = 118.98
but the library can produce these objects directly from a .las file for you, can't it? The File class you had trouble with before certainly does return these objects already.
And since you updated to show some code, here is a more readable version of that:
pt = liblas.point.Point()
s = map(float, s.split())
pt.x, pt.y, pt.z = s[0], s[1], s[11]
pt.intensity, pt.return_number = s[3], s[4]
pt.number_of_returns, pt.scan_direction = s[5], s[6]
pt.flightline_edge, pt.classification = s[7], s[8]
pt.scan_angle = s[9]

Create a Python list filled with the same string over and over and a number that increases based on a variable.

I'm trying to create a list that is populated by a reoccurring string and a number that marks which one in a row it is. The number that marks how many strings there will be is gotten from an int variable.
So something like this:
b = 5
a = range(2, b + 1)
c = []
c.append('Adi_' + str(a))
I was hoping this would create a list like this:
c = ['Adi_2', 'Adi_3', 'Adi_4', 'Adi_5']
Instead I get a list like this
c = ['Adi_[2, 3, 4, 5]']
So when I try to print it in new rows
for x in c:
print"Welcome {0}".format(x)
The result of this is:
Welcome Adi_[2, 3, 4, 5]
The result I want is:
Welcome Adi_2
Welcome Adi_3
Welcome Adi_4
Welcome Adi_5
If anybody has Ideas I would appreciate it.
You almost got it:
for i in a:
c.append('Adi_' + str(i))
Your initial line was transforming the whole list a as a string.
Note that you could get rid of the loop with a list comprehension and some string formatting:
c = ['Adi_%s' % s for s in a]
or
c = ['Adi_{0}'.format(s) for s in a] #Python >= 2.6
Or as a list comprehension:
b = 5
a = range(2, b + 1)
c = ["Adi_" + str(i) for i in a]
Using list comprehensions:
b = 5
a = range(2, b + 1)
c = ['Adi_'+str(i) for i in a]
for x in c:
print"Welcome {0}".format(x)
Or all on one line:
>>> for s in ['Welcome Adi_%d' % i for i in range(2,6)]:
... print s
...
Welcome Adi_2
Welcome Adi_3
Welcome Adi_4
Welcome Adi_5

Map function doesn't work in for loop

I'm trying to merge two lists if they contain a certain word.
My code works fine until I try to transfer it to under a function or under a for loop.
When I do I get:
TypeError: argument 2 to map() must support iteration
I also tried replacing map(None, a,b) with itertools.imap(None, a,b) as suggested in other posts but get :
TypeError: 'int' object is not iterable
Any suggestions?
a = 0
b = 0
row_combine = []
for row in blank3:
if 'GOVERNMENTAL' in row:
a = row
if 'ACTIVITIES' in row:
b = row
c = map(None, a,b) #problem is here
for row in c:
row1 = []
if row[0] == None:
row1.append(''.join([''] + [row[1]]))
else:
row1.append(''.join([row[0]] + [' '] + [row[1]]))
row_combine.append(''.join(row1))
output for a:
a = [' ', u'GOVERNMENTAL', u'BUSINESS-TYPE']
output for b:
b = [u'ASSETS', u'ACTIVITIES', u'ACTIVITIES', u'2009', u'2008', u'JEDO']
need it to be:
[ u'ASSETS', u'GOVERNMENTAL ACTIVITIES', u'BUSINESS-TYPE ACTIVITIES', u'2009', u'2008', u'JEDO']
hence the for for loop after map function.
If after iterating through blank3 you never encounter both 'GOVERNMENTAL' and 'ACTIVITIES', a or b could be 0, which will cause map to fail. You could start a and b off as empty lists, or check your input before the map()
Meanwhile, instead of the for loop:
row_combine = map(lambda x, y: ((x or '') + ' ' + (y or '')).strip(), a, b)
Which yields:
[u'ASSETS', u'GOVERNMENTAL ACTIVITIES', u'BUSINESS-TYPE ACTIVITIES', u'2009', u'2008', u'JEDO']

Categories

Resources