-7

I have a list like:

Users = ['Protein("SAHDSJDSFJH"), {"id": "s1"}',
         'Protein("ACGTWZJSFNM"), {"id": "s2"}',
         'Protein("ABHZZEQTAAB"), {"id": "s3"}']

I want the same list as:

Users = [Protein("SAHDSJDSFJH"), {"id": "s1"},
         Protein("ACGTWZJSFNM"), {"id": "s2"},
         Protein("ABHZZEQTAAB"), {"id": "s3"}]

Without making the second list as a string, I just want the single quotes to be removed from the list items. Since, I am parsing it to a library in python to calculate a number iteratively using the ids. The function gives an error when encounters a quotes in the list items.

shalini
  • 11
  • 1
  • 7
  • 2
    If you print these out you will not see the quotes, they are already strings. The quotes are simply to notate their type. – Easton Bornemeier Aug 07 '17 at 19:36
  • 3
    If you're concerned about the quotes being a part of the string, they aren't; they're just used to enclose whatever is already INSIDE the quotes. – ajc2000 Aug 07 '17 at 19:36
  • Possible duplicate of [Remove string quotes from array in Python](https://stackoverflow.com/questions/21352016/remove-string-quotes-from-array-in-python) – Murat Seker Aug 07 '17 at 19:36
  • 2
    How are you using the list, and why is it relevant whether its members are displayed with quotes (signifying string literals)? This looks to me like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – ChrisGPT was on strike Aug 07 '17 at 19:37
  • @Chris I have edited the question – shalini Aug 07 '17 at 19:57
  • @shalini How did you even get a list like that? – Artyer Aug 07 '17 at 19:57
  • Erm, okay, now we've got a very different situation. Where did the string values originally come from? (It's usually best to fix these types of issues where they originate.) What is the function that "gives an error when [it] encounters quotes in the list items"? Please read [ask] for tips on asking effective questions. – ChrisGPT was on strike Aug 07 '17 at 19:58
  • @Chris Using a for loop, I appended the list items. after that, I am trying to compute a distance matrix, using DistanceMatrix.from_iterable(users, metric= kmer_distance, key= 'id') – shalini Aug 07 '17 at 20:02
  • But where did the items themselves come from? How did the strings like `'Protein("SAHDSJDSFJH"), {"id": "s1"}'` get formed in the first place? – ChrisGPT was on strike Aug 07 '17 at 20:05
  • @Artyer I have appended the items in the list using a for loop – shalini Aug 07 '17 at 20:06
  • @Chris I used `users.append('Protein (" ' +dsspSeqList[i]+ ' ", {"id" : "s' +str(i +1)+ ' "}) ')` – shalini Aug 07 '17 at 20:12
  • @shalini You should instead do `users.append(Protein(dsspSeqList[i]))` `users.append({"id": "s" + str(i + 1)})` – Artyer Aug 07 '17 at 20:15
  • Thanks @Artyer Now, it gets no quotes and even makes it a list. But my code doesn't recognizes the key ='id' metadata. :( I don't know. – shalini Aug 07 '17 at 20:30

2 Answers2

0

Instead of printing like list with

print(Users)

try

print("Users = [%s]" % ", ".join(Users))

This formats the output so that it looks like you mentioned.

RagingRoosevelt
  • 2,046
  • 19
  • 34
  • This makes it is a string... OP asked a meaningless question. – AChampion Aug 07 '17 at 19:46
  • This make it a string. I want it to still be a list. – shalini Aug 07 '17 at 19:58
  • @shalini your question doesn't make sense if you're looking for it to be a list. The quote marks just show what data type python thinks your list contains. The only way you could remove the quote marks is if you were trying to display the contents of the list. – RagingRoosevelt Aug 07 '17 at 20:16
0

You may use below list comprehension using map with eval(...) as:

import ast

Users = ['Protein("SAHDSJDSFJH"), {"id": "s1"}',
         'Protein("ACGTWZJSFNM"), {"id": "s2"}',
         'Protein("ABHZZEQTAAB"), {"id": "s3"}']

new_list = [y for x in map(eval, Users) for y in x]

where new_list will hold the value:

[Protein("SAHDSJDSFJH"), {'id': 's1'}, 
 Protein("ACGTWZJSFNM"), {'id': 's2'}, 
 Protein("ABHZZEQTAAB"), {'id': 's3'}]

PS: Note that there should exists a class definition Protein in the scope whose __init__ expects one string variable as an argument, and __repr__ function to display the Protein's object in the format you require . For example:

class Protein:
    def __init__(self, x):
        self.x = x

    def __repr__(self):
        return 'Protein("%s")' % self.x

Note: Usage of eval in Python code is not a good practice. You should not be using it for the live application, however it is fine to use it for home-work (if that's the case). Take a look at: Why is using 'eval' a bad practice? for the details.

Edit: Based on the comment by the OP. Instead of using:

users.append('Protein (" ' +dsspSeqList[i]+ ' ", {"id" : "s' +str(i +1)+ ' "}) ')

you should be using:

users.append(Protein(dsspSeqList[i], {"id" : "s{}".format(i +1)}))

This way you don't need a eval function. But Note part will still be applicable.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126