Accessing the value of an object from a tuple without context information

Multi tool use
Multi tool use


Accessing the value of an object from a tuple without context information



I have a list of IDs (IDList) that are a subset of a list of tupels, containing these IDs and related IDs in String-Format. There can be tupels with one related ID, e.g. (1694, '1743'), two related
IDs seperated by comma e.g. (3252, '3253,3254') or no related ID ('none').



I would like to write all IDs from the tupels that are related to the IDs in IDList into one common list (final_list). Therefore the elements containing two IDs should be split by comma and those containing "none" should be dropped.



This is how my code looks right now:


IDList = [1694, 3252, 2779]
related = [(1694, '1743'), (3252, '3253,3254'), (3253, '3252,3254'), (3254, '3252,3253'), (1743, '1694'), (2779, 'none'), (3251, 'none'), (1677, 'none'), (1676, 'none'), (1678, '1679'), (1679, '1678')]
final_list =
related_frame = pd.DataFrame(related)
print(related_frame)
for n in l:
dset = related_frame.loc[related_frame[0] == n]
print (dset)
rel_set = dset[1]
rel_set = str(rel_set)

if "," in rel_set:
rel_set = rel_set.split(',')

print(rel_set)
if "none" in rel_set:
rel_set =
final_list.append(rel_set)

print(final_list)



In dataframe-format it still looks as expected:


print(related_frame)



out:


0 1
0 1694 1743
1 3252 3253,3254
2 3253 3252,3254
3 3254 3252,3253
4 1743 1694
5 2779 none
6 3251 none
7 1677 none
8 1676 none
9 1678 1679
10 1679 1678



However, once I start working with the related values only, Python for some reason adds information about the type to the values.



So the final_list looks like this:


print(final_list)

[['0 1743nName: 1', ' dtype: object'], ['1 3253', '3254nName: 1', ' dtype: object']]



I would like to have it this way:


print(final_list)

[1743, 3253, 3254]



Does anyone know how to access the values from the objects in the tupels without the nName and dtype-information etc.? So I just would like to read the plain value. The data in the related-list is obtained from a varchar-column in SAP HANA-Database, so these are not very exceptional objects by any means.



I know I could remove them using


str.replace("nName: 1', ' dtype: object", "")



But isn't there a more convenient way? The final_list would also look like this in that case (and I would prefer having it as stated above):


[['0 1743'], ['1 3253', '3254']]




1 Answer
1



You can use a combination of regex and list comprehension.


import re
final_list = [['0 1743nName: 1', ' dtype: object'], ['1 3253', '3254nName: 1', ' dtype: object']]
print([int(i) for sublist in [re.findall(r'(?<!Name: )(d+)(?=$|n)', i) for sublist in final_list for i in sublist] for i in sublist])



This outputs:


[1743, 3253, 3254]





Thanks for the suggestion. However I was wondering if there really is no way to simply access the plain value of the object (as in the dataframe "related_frame" there are only the plain values displayed). Your solution works, but it seems to be a lot of effort for a seemingly simple thing to do. If no one else comes up with a more convenient way to do it, e.g. a method, then I will accept as definite answer.
– WebScraper
Jul 3 at 8:20






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

eYKvFNUBM062B,5NtlaQwwt3DFPFlDsImJL,80 KWLwhPITu2WwP,q,ah8ac6QcBn0M UIdg2ylgfETW9Hr ma3B8Q,ZS6NNMHEVVh
JEsnd8sJ

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications