This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(31 answers)
Closed 8 years ago.
I want to print all the characters in a string as a list but for each character to be printed once even if recurring. So far I have:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
symbolsx.append(i)
This prints every character, even if the character is repeated.
symbolsx = list(set(symbolsx))
First pass the list to set function to remove duplicates, then reverted that set back to list by passing it to list function.
How about:
symbolsx = []
for line in ''.join(word_lines):
for i in line:
if i not in symbolsx:
symbolsx.append(i)
Related
This question already has answers here:
Sort list of strings by integer suffix
(4 answers)
Is there a built in function for string natural sort?
(23 answers)
Closed 1 year ago.
I have a list of ['rs12345','rs21','rs55189'];
I need to sort them as numbers after strip the prefix 'rs'.
How can I do it in Python ?
# row.keys() is ['rs12345','rs21','rs55189']
fieldnames = sorted(list(row.keys()),key=itemgetter(slice(2, None)))
This code will not working after add int(''.join(xxx)).
And the dict row is a generator so I have to put it into list() to get its values.
_fieldnames = list(row.keys())
_fieldnames.remove(sidname)
_fieldnames = sorted(_fieldnames, key=lambda i: int(i[2:]))
Got it working.
I forgot that I have to remove sampleid, which contains no ^rs, first.
This question already has answers here:
How to get a sublist of a list between two words
(2 answers)
Closed 3 years ago.
I would like to keep a list of items that fall between two values in the list.
Below is a representation of the list I have:
List = ['Waste','Waste','Start','Data','Data','End','Waste','Waste']
I need to keep the 'Data' strings.
Desired result below.
Res = ['Start','Data','Data','End']
I'm currently converting the list to a string, splitting on 'Start', converting back to a string, and then splitting on 'End' whilst indexing the correct side of the split I want to keep. It's messy.
Thank you for any help.
Assuming 'Start' and 'End' elements exist, with 'Start' occurring before 'End', you can use:
List[List.index('Start'):List.index('End')+1]
This question already has answers here:
Removing elements that have consecutive duplicates
(9 answers)
Closed 3 years ago.
I have the list of APIs,
Input = [WriteConsoleA, WSAStartup, RegCloseKey, RegCloseKey, RegCloseKey, NtTerminateProces, RegCloseKey]
expected output = [WriteConsoleA, WSAStartup, RegCloseKey, NtTerminateProces, RegCloseKey]
Input = ["WriteConsoleA", "WSAStartup", "RegCloseKey", "RegCloseKey", "RegCloseKey", "NtTerminateProces", "RegCloseKey"]
Output = []
api=Input[0]
for index in range(1,len(Input)):
if api!=Input[index]:
Output.append(api)
api=Input[index]
Output.append(api)
print(Output)
Try this hopefully it will work in your case.
you can simply convert set(list) i.e. set(Input) to remove all the duplicates.
This question already has answers here:
How to use Python sets and add strings to it in as a dictionary value
(2 answers)
Closed 4 years ago.
I am trying to convert a string to a set containing that string. How can I do this without splitting?
when I write:
set("abc")
the result is:
{'a','b','c'}
but I want it to be:
{"abc"}
Doku set(iterable) will create a set of each element of an iterable - strings are iterable - hence you create a set of the characters of the string.
If you want whole strings, use
k = {"mystring",}
or
k = set( ["mystring"] ) # wrap your string into another iterable
This question already has answers here:
How to delete the very last character from every string in a list of strings
(5 answers)
Closed 5 years ago.
Suppose,
a = ["Power is nothing"]
How to remove last string in variable 'a' which is 'g'?
You can use list slicing:
a = a[0][:-1]
a[0] selects the first element in your list. in your case, a[0] is "Power is nothing". Then [:-1] returns a new string without the last letter.
You can check the documentation for any detail regarding list slicing in python