As set('ate')==set('aet') is True, why the result comes like below?
Input: list(set('ate'))
Output: ['e', 'a', 't']
Input: list(set('aet'))
Output: ['a', 't', 'e']
I want an explanations for how the output is produced. To me the element's order of output is random.
I have tried with
x = set('ate')
x.pop()
# 'e'
x.pop()
# 'a'
x.pop()
# 't'
Same problem, the order makes me confused.
Sets are unordered collections; lists are ordered. A set is equal to another set if it contains the same elements, regardless of order.
However, a list is an ordered collection. Lists are equal if and only if they contain the same elements in the same order.
Related
I'm starting with a "text-type" file that I'm parsing (line by line) into lists that I'm manipulating. I'm using the lists: inputs, gateInput, gateOutput, gateType, nodeLevel, and gateList to manipulate the data as I need it. Presently, all the information parsed out correctly and I'm currently at an impasse.
I'm taking from a list of lists (gateInput) and I want to compare the contents with (inputs) to see if all the gateInputs are inputs. If so, I'm wanting to update the nodeLevel to reflect as such.
def compareInputs(x):
count = 0
for l in x:
for i in inputs:
if l in i:
print(x)
#---------------------------------------
for (g,h) in zip(gateList,nodeLevel):
for (a,b) in zip(gateOutput,gateInput):
if g == a:
compareInputs(b)
The two for loops and an if are what I'm using to get to where I send the item (b) to the function compareInputs. In compareInputs, I'm able to get things to cycle through, but it compares (lets say 'a') to every item list. Lets say that the len(list) is 3. So it compares 'a' three times, then goes to 'b' 3 times and then 'c' three times.
What I'm attempting to see is if list is comprised of only elements from inputs = ['a', 'b', 'c']. I do understand that this is a lot. It's not easy for me. I have a lot of things working against me. Any help would be appreciated.
Me printing in my function was to see if I'm getting the correct information to where I need it, and I am. Just need help with how to compare it to get the answer I'm after.
sample data
['c']
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
["a'", 'b', 'c']
["a'", 'b', 'c']
['c']
['a', "b'"]
['a']
['b']
['a', 'b', "c'"]
['a', 'b', "c'"]
["b'", 'c']
['b', "c'"]
These would be the component 'l' that is taken from 'x'. 'x' being a list of lists.
appreciate it if someone can answer this.
I have a list called as oldList which contains something like this:
oldList = ['a','b','c','d']
Then I use random.shuffle(oldList) to get a random list and append those into randomList.
How can I check if I run again random.shuffle(oldList), and if the randomize list is already being appended into randomList, that randomize list would not be selected or append into randomList.
The shuffle() function returns nothing, it modifies an object in-place as pointed out by juanpa.arrivillaga.
I suspect you are looking for the sample() function which returns a list of items in a random order. In other words, sample() chooses a random sample from a population without replacement (where each item in the population has an equal chance to be picked). In the following example k is the random sample length.
from random import sample
example_list = ['a','b','c','d']
example_list.extend(sample(population=example_list, k=4))
print(example_list)
The order of the last four items will change each time you run this but the result will look like the following:
['a', 'b', 'c', 'd', 'b', 'd', 'c', 'a']
In the above example I used extend() instead of append() because the sample function outputs a list. append() would output something like this:
['a', 'b', 'c', 'd', ['b', 'd', 'c', 'a']]
I am sure this questions is already here, but I cannot find the answer. Basically, I need to find the number of times a set of three strings is in a list, let´s say we have:
list=['a','b','b','c', 'a', 'c', 'd']
In this case, we have two triplets of a,b,c strings. It can be found like this:
all(x in list for x in ['a', 'b', 'c']) --> True
But how can I count the number of times this triplet appears in the list, which in the list example would be 2?
Count all of the letters individually, and take the min to handle cases where you have more of some letters than others (the min is the number of complete triplets, whereas the max would include incomplete triplets).
>>> my_list = list=['a','b','b','c', 'a', 'c', 'd']
>>> min(my_list.count(x) for x in ['a', 'b', 'c'])
2
I'm learning python and I have been trying to make an automatic list of lists. For example:
The next list, which I have to split to get a list of separated letters, and then join them again to make a two lists of separated letters
lists=[['a,b,c,h'],['d,e,f,g']]
print('list length', len(lists))
splited=[]
splited1=lists[0][0].split(',')
print(splited1) #['a', 'b', 'c', 'h']
splited2=lists[1][0].split(',')
print(splited2) #['d', 'e', 'f', 'g']
listcomb=[splited1,splited2]
print(listcomb) #[['a', 'b', 'c', 'h'], ['d', 'e', 'f', 'g']]
This is what I want to get, a list that have 2 lists, but in the case I have to get more lists inside that list i want to make it automatic with a for loop.
My try, but it didn't work
listcomb2=zip(splited1,splited2)
print(listcomb2)
sepcomb = list()
print(type(sepcomb))
for x in range(len(lists)):
sep=lists[x][0].split(',')
sepcomb[x]=[sep]
print(sepcomb)
I'm having problems with splitting the letters and then joining them in a new list. Pls help
Make some tweak in your code. As we can see lenght of sepcomb is 0 so use append method to avoid this problem. As sepcomb[x]=[sep] is is assignment to x-index but it x index doesn't exist so, it will raise error
change:
for x in range(len(lists)):
sep=lists[x][0].split(',')
sepcomb[x]=[sep]
to
for x in range(len(lists)):
sep=lists[x][0].split(',')
sepcomb.append(sep)
Method-2
sepcomb = list(i[0].split(',') for i in lists)
You can simply do the following:
final=[splinted1]+[splinted2]
Or a better way directly from the lists variable would be:
final=[value[0].split(',') for value in lists]
Here you go:
lists = [['a,b,c,h'],['d,e,f,g']]
listcomb = []
for each in lists:
splited = each[0].split(',')
listcomb.append(splited)
print(listcomb)
I want to create a list say chelsea_fc that will populate the value "EPL Champions" in even indexes and "Manager Sacked" at odd indexes till a given range. (dont want to hard code the range)
I am getting confused as how to do it. Please help
Literally write it as you would say it!
>>> ['a' if i % 2 else 'b' for i in range(10)]
['b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a']
You can do this (or any other list that repeat itself) like this:
chelsea_fc = ['Manager Sacked', 'EPL Champions']*(range_of_choice/2)
print(chelsea_fc)