A newbie question for which I apologize if it's basic.
I have a set, myset, that is filled by reading an csv file see below printed representation.
set(['value1', 'value2'])
The number of elements on the set is arbitrary, depending upon the read file. I want to add entries on a csv file using the individual elements of the set. I've tried:
file_row = ['#Entry','Time', str(myset), 'cpu usage']
print file_row
filewriter.writerow(file_row)
However the output I get is:
#Entry,Time,"set(['value1', 'value2'])",cpu usage
where I actually wanted
#Entry,Time,value1,value2,cpu usage.
Can you suggest how to get my desired result ?
You could approach this as follows:
file_row = ['#Entry','Time'] # start with pre-myset elements
file_row.extend(myset) # add on myset
file_row.append('cpu usage') # add final item
Note that using a set means the order of elements will also be arbitrary.
If you want to do it all in one line:
file_row = ['#Entry','Time'] + [x for x in myset] + ['cpu usage']
Related
I am a beginner in python, and I have a question that perhaps is simple. I have a "file.txt", where in principle there can be a number n of strings.
> file.txt
John
Rafa
Marta
...
n
This is loaded into the program with:
with open('/media/names.txt') as f:
lines = f.read().splitlines()
Now, I load a dataframe from a csv, which has a column (with name "Identifier") that contains a lot of names.
Registration = pd.read_csv('/media/Registration.csv',
sep='\t', header=0)
The goal is to find the n strings separately for each variable. For example, in this case I have done it for the first data in the list:
names_1 = Registration[Registration['Identifier'].str.contains(lines[1])]
print(names_1)
Only keeping the lines that have "John" as an identifier. However, I am trying to create n dataframes as there are items in the "file.txt" list.
names_1 = Registration[Registration['Identifier'].str.contains(lines[1])]
names_2 = Registration[Registration['Identifier'].str.contains(lines[2])]
names_3 = Registration[Registration['Identifier'].str.contains(lines[3])]
names_n = Registration[Registration['Identifier'].str.contains(lines[n])]
But I'm a bit stuck and I don't know how to do this loop. Someone help me? Thanks!
Theoretically speaking, the answer to your question is that local variables are stored in a dictionary accessible with the function locals(). As a result, it is possible to generate variables in a loop exactly as asked.
for i, line in enumerate(lines):
locals()[f'names_{i}'] = Registration[Registration['Identifier'].str.contains(line)]
However, just because you can do it doesn't mean you should, it's generally not a good idea to generate variables in this manner.
Just ask yourself, how would you access the nth variable? You are going down a path that will make your data difficult to work with. A better approach is to use a data structure like a dictionary or a list to easily keep track of it.
names = []
for line in lines:
names.append(Registration[Registration['Identifier'].str.contains(line)])
Do note also that the first index is 0, not 1.
Python list indexes begin by 0.
Try with a for-loop like this:
for i in range(len(lines)):
names = Registration[Registration['Identifier'].str.contains(lines[i])]
But then you'll need to keep value of names. Maybe in a list:
name_list = []
for i in range(len(lines)):
names = Registration[Registration['Identifier'].str.contains(lines[i])]
name_list.append(names)
print(name_list)
Try this! Enjoy coding!
This is the JSON data I am using
In this dataset, I want to extract the value of "yaw" and store it into in camera_loc. My first attempt for the code was as follows:
with open(
"/home/siddhant/catkin_ws/src/view-adaptation/multi_sensors.json"
) as sensors:
multi_sensors = json.load(sensors)
camera_loc = [
multi_sensors["sensors"][0]["yaw"],
multi_sensors["sensors"][1]["yaw"],
multi_sensors["sensors"][2]["yaw"],
multi_sensors["sensors"][3]["yaw"],
multi_sensors["sensors"][4]["yaw"],
multi_sensors["sensors"][5]["yaw"],
]
This gives me the expected result.
But I want to generalize the same for any number of entries in the 'sensors' array.
I tried executing a 'for' loop and extracting the values for the same as follows:
for i in multi_sensors["sensors"]:
camera_loc = []
camera_loc.append(i["yaw"])
However, this method only gives a single value in the camera_loc list which is the last 'yaw' value from the JSON file. I am looking for a better approach or even any modifications to the way I execute the loop so that I can extract all the values of 'yaw' from the JSON file - in this example there are 6 entries but I want to generalize it for 'n' entries that may be created in other cases.
Thank you!
That's because you're defining your camera_loc array inside the loop, meaning every iteration resets it to []. This code should work if you remove the array definition from the loop:
camera_loc = []
for i in multi_sensors["sensors"]:
camera_loc.append(i["yaw"])
This is also a perfect use case for a List Comprehension:
camera_loc = [i["yaw"] for i in multi_sensors["sensors"]]
Both answers results in the same array though, so you may choose whichever you like most.
I am new to this sort of stuff, so sorry if it's really simple and I am just being stupid.
So I have this variable with some bytes in it (not sure if that's the right name.)
data = b'red\x00XY\x001\x00168.93\x00859.07\x00'
I need to convert this to a list. The intended output would be something like.
["red","XY","1","169.93","859.07"]
How would I go about doing this?
Thank you for your help.
We can use the following line:
[x.decode("utf8") for x in data.split(b"\x00") if len(x)]
Going part by part:
x.decode("utf8"): x will be a bytes string, so we need to convert it into a string via `.decode("utf8").
for x in data.split(b"\x00"): We can use python's built in bytes.split method in order to split the byte string by the nullbytes to get an array of individual strings.
if len(x): This is equivalent to if len(x) > 0, since we want to discard the empty string at the end.
This code may help you to understand if you want exact same output using the pop() function.
data = 'red/x00XY/x001/x00168.93/x00859.07/x00' # I change "/" mark from "\" because i'm using Linux otherwise it will give error in Linux
new_list = [] # There is a variable that contain empty list
for item in data.split('/x00'): # Here I use split function by default it splits variable where "," appears but in this case
new_list.append(item) # you need list should be separated by "/" so that's why I gave split('/x00') and one by list appended
print(new_list)
I have a set of values that get modified like so:
iskDropped = irrelevant1
iskDestroyed = irrelevant2
iskTotal = irrelevant3
iskDropped = condense_value(int(iskDropped[:-7].replace(',','')))
iskDestroyed = condense_value(int(iskDestroyed[:-7].replace(',','')))
iskTotal = condense_value(int(iskTotal[:-7].replace(',','')))
As you can see, all three lines go through the same changes. (condensed, shortened, and commas removed) before overwriting their original value.
I want to condense those three lines if possible because it feels inefficient.
I was trying something like this:
for value in [iskDropped,iskDestroyed,iskTotal]:
value = condense_value(int(value[:-7].replace(',','')))
which if you changed into a print statement successfully does print the correct values but it does not work in the regard of overwriting / updating the values (iskDropped,iskDestroyed, and iskTotal) that I need to call later in the program.
Is it possible to condense these lines in Python? If so can someone point me in the right direction?
You can do it like this:
iskDropped, iskDestroyed, iskTotal = [condense_value(int(value[:-7].replace(',',''))) for value in [iskDropped, iskDestroyed, iskTotal]]
This works by looping through the list of your 3 variables, performing the condense_value function on each and creates a list of the results, then finally unpacks the list back into the original values.
I'm using Python 3. I have two lists of strings and I'm looking for mismatches between the two. The code I have works for smaller lists but not the larger lists I'm writing it for.
Input from the non-working lists is in the following format:
mmec11.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec13.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec12.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec14.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
My function to compare two lists of data in the above format is:
result = []
for x in mmeList1:
if x not in mmeList2:
result.append(x)
return result
The problem is it's not working. I get an output file of both lists combined into one long list. When I put a test is to say "Hi" every time a match was made, nothing happened. Does anyone have any ideas where I'm going wrong. I work for a telecommunications company and we're trying to go through large database dumps to find missing MMEs.
I'm wondering if maybe my input function is broken? The function is:
for line in input:
field = line.split()
tempMME = field[0]
result.append(tempMME)
I'm not very experienced with this stuff and I'm wondering if the line.split() function is messing up due to the periods in the MME names?
Thank you for any and all help!
If you don't need to preserve ordering, the following will result in all mmes that exist in list2 but not list1.
result = list(set(mmeList2) - set(mmeList1))
I tested your compare function and it's working fine, assuming that the data in mmeList1 and mmeList2 is correct.
For example, I ran a test of your compare function using the following data.
mmeList1:
mmec11.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec13.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec12.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec14.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmeList2:
mmec11.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec13.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec12.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
mmec15.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
Result contained:
mmec14.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org
I suspect the problem is that mmeList1 and mmeList2 don't contain what you think they contain. Unfortunately, we can't help you more without seeing how mmeList1 and mmeList2 are populated.
If you want to see the differences in both, (i.e. Result should contain mmec14 AND mmec15), then what you want to use is Sets.
For example:
mmeSet1 = set(mmecList1)
mmeSet2 = set(mmecList2)
print mmeSet1.symmetric_difference(mmeSet2)
will result in:
['mmec14.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org', 'mmec15.mmegifffa.mme.epc.mnc980.mcc310.3gppnetwork.org']
At first, using set() on list is best way for decreasing iteration.Try this one
result = []
a=list(set(mmeList1))
b=list(set(mmeList2))
for x in a:
if x not in b:
result.append(x)
return result