Is there any way of applying a function to every item in a set. For example, i want to make a every item in a set lowercase. If it was a list, i could do this as:
new_list = [item.lower() for item in old_list]
Obviously i could convert the set to a list, then do this, and then convert it back to a set again, but is there a more direct way?
You can simply use:
new_set = {set.lower() for item in old_set}
This is a set comprehension rather than a list comprehension.
Related
bigList = [list1,list2,list3]
I need a way to get the name of list1 using the zero index
bigList[0] just gives you all the items in the list
My original code prints all the items in the lists while the modified one says the index is out of range. I want it to give me the ilness names not all the symptoms.
Original code
https://i.stack.imgur.com/HDlA9.png
Modified
https://i.stack.imgur.com/5Qdh2.png
a = {0:[[1,2,3], [1,3,4,5]]}
print([1,2,3] in a.values())
I get False. Because this list is in values I need True. Is it possible to check all lists in nested list as a value in dictionary? Maybe without loops?
Since you're using python3 you can do this:
[1,2,3] in list(a.values())[0]
a.values() returns a dictionary view. dictionary views
Then you can wrap the dictionary view into list but this list will contain only one element which can be accessed by index 0. However, if your dictionary contains several keys and corresponding values then list(a.values()) will contain the same number of elements (values mapped to keys) as keys in the dictionary.
Note that when you use some_value in some_collection construct and don't use loops explicitly it will still iterate through the collection.
I have a list(rs_data) with sublists obtained from a Dataframe, and some rows from Dataframe contain multiple elements, like those:
print(rs_data)
rs1791690, rs1815739, rs2275998
rs6552828
rs1789891
rs1800849, rs2016520, rs2010963, rs4253778
rs1042713, rs1042714, rs4994, rs1801253
I want to obtain a list in which each element (rs….) is separated, something like this:
{'rs1791690', 'rs1815739', 'rs227599', 'rs401681', 'rs2180062', 'rs9018'….}
How can I eliminate sublits or generate a new list without sublists, in which each element is unique.
To generate a new list you could iterate over the old one and throw out the elements you don't like.
Something like this
for i in rs_data:
if i in bad_values:
# do something
else:
# do something else
If you just want to eliminate duplicates it would be the best to use a set
Like this
mynewset = set(rs_data)
I'm making a game of hangman. I use a list to keep track of the word that you are guessing for, and a list of blanks that you fill in. But I can't figure out what to do if for example someone's word was apple, and I guessed p.
My immediate thought was to just find if a letter is in the word twice, then figure out where it is, and when they guess that letter put it in both the first and second spot where that letter is. But I can't find
How to test if two STRINGS are duplicates in a list, and
If I were to use list.index to test where the duplicate letters are how to I find both positions instead of just one.
Create a string for your word
Create a string for user input
Cut your string into letters and keep it on a list/array
Get input
Cut input into letters and keep it on another array
Create a string = "--------" as displayed message
Using a for loop check every position in both array lists and compare them
If yourArray[i] == inputArray[i]
Then change displayedString[i] = inputArray[i] and display message then get another input
If it doesnt match leave "-" sings
Displayed the "---a--b" string
One way to do it would be to go through the list one by one and check if something comes up twice.
def isDuplicate(myList):
a = []
index = 0
for item in myList:
if type(item) == str:
if item in a:
return index
else:
a.append(item)
index += 1
return False
This function goes through the list and adds what it has seen so far into another list. Each time it also checks if the item it is looking at is already in that list, meaning it has already been seen before. If it gets through the whole list without any duplicates, it returns False.
It also keeps track of the index it is on, so it can return that index if it does find a duplicate.
Alternately, If you want to find multiple occurrences of a given string, you would use the same structure with some modifications.
def isDuplicate(myList, query):
index = 0
foundIndexes = []
for item in myList:
if item == query:
foundIndexes.append(index)
index += 1
return foundIndexes
This would return a list of the indexes of all instances of query in myList.
This is my Python code (I am using Python 2.7.6):
cn = ['here,', 'there']
>>> for c in cn:
if c.endswith(','):
c = c[:-1]
>>> print(cn)
['here,', 'there']
As you can see, cn[0] still has the trailing comma even though I did c = c[:-1]. How do I change cn[0] so that it equals 'here' without the trailing comma?
The problem is that you assign a new value to c but do not update the list.
In order to manipulate the list in place you would need to do something like this:
cn = ['here,', 'there']
for index, c in enumerate(cn):
if c.endswith(','):
cn[index] = c[:-1]
print(cn)
['here', 'there']
Enumerate gives you all elements in the list, along with their index, and then, if the string has a trailing comma you just update the list element and the right index.
The problem with your code was that c was simply holding the string 'here,'. Then you created a new string whithou the comma and assigned it to c, this did not affect the list cn. In order to have any effect on the list you need to set the new value at the desired position.
You could also use list comprehension to achive the same result which might be more pythonic for such a small task: (as mentioned by #AdamSmith)
cn = [c[:-1] if c.endswith(',') else c for c in cn]
This does creates a new list from cn where each element is returned unchanged if it does not end with , and otherwise the comma is cut off before returning the string.
Another thing you could use would be the built-in rstrip function, but it would remove all trialing commas, not just one. It would look something like this (again #AdamSmith pointed this out):
cn = map(lambda x: x.rstrip(','), cn)
What you did was copy the string with an edit, and store it in a new variable named c, with a name that clashed with the original c and overrode it.
Since you didn't change the original, it stays unchanged when the new one goes out of scope and gets cleared up.
You could do something like this to make a new list, containing the changed strings:
newlist = [c.strip(',') for c in cn]
Or, more closely to your example while keeping this approach:
cn = [c[:-1] if c.endswith(',') else c for c in cn]
The alternate loop-with-enumerate approach in another answer will work, but I'm avoiding it because in general, changing lists while iterating over them can lead to mistakes unless done carefully, and that makes me think it's a bad habit to get into in cases where there's another reasonably neat approach.
You're changing the loop variable c, but you aren't changing the list cn. To change the list cn, instead of looping through its values, loop through its indexes. Here's an implementation.
for c in range(len(cn)):
if cn[c].endswith(','):
cn[c] = cn[c][:-1]