I need to shuffle multiple lists from text files so that their shuffle orders are consistent:
Input:
List 1 = 1,2,3,4,5
List 2 = 5,4,8,7,9
Expected output:
List 1: 2,3,4,1,5
List 2: 4,8,7,5,9
How can I do this using command prompt?
Thanks and appreciate it.
Related
I have a list of fruits and there are some duplicates in the list. I am not after the unique items in the list nor the count of the number of unique items. If I wanted thsoe details I would simply use a set.
I want to instead calculate the number of fruits in the list that are not duplicates i.e lemon,orange,tomato,passionfruit = 4
Here's my code which works ok but is slow as it uses nested loops.
fruits=['apple','pear','pear','apple','strawberry','lemon','orange','strawberry','tomato','passionfruit']
fruits_len=len(fruits)
uniq=0
for loop1 in range(0,fruits_len):
flag=0
for loop2 in range (0,fruits_len):
if loop1==loop2:
continue
if fruits[loop1]==fruits[loop2]:
flag=1
break
if flag==0:
uniq+=1
print(f'There are {uniq} fruits not duplicated in the fruits list ')
The code yields the following correct result.
runfile('C:/A/untitled0.py', wdir='C:/A')
There are 4 fruits not duplicated in the fruits list
The only problem is that on much larger lists my code would be slow.I want to resolve the above in the most efficient way possible. I tried to devise a solution using list comprehension but its awkward because the two nested loops have code in between them.
How can I make my code run much faster? (bragging rights to he/she who can come up with the fastest solution)
Thanks Peter
You could use a dict to store the counts of the elements from the array and select the elements that have only one occurrance
fruits=['apple','pear','pear','apple','strawberry','lemon','orange','strawberry','tomato','passionfruit']
histogram = {}
for item in fruits:
if item not in histogram:
histogram[item] = 1
else:
histogram[item] += 1
print(len([ i for i in histogram if histogram[i] == 1]))
Outputs
4
Time
CPU times: user 2 µs, sys: 1 µs, total: 3 µs
Wall time: 6.2 µs
I have the following code which I have to build upon (i.e. it can't be written a different way). I know there are other better ways of achieving the end result, but I want to use this code and then repeat it to make a list.
from random import choice
number_list = range(1,1001) # Creates a list from 1 to 1000
random_from_list = choice(number_list) # Chooses a random number from the list
I want to now repeat the choice function above 100 times, then print that list of 100 random numbers that I have chosen from my list of 1000 numbers. I have read up on "for" loops but I can't see how to apply it here.
If you don't need to build up your list you could just print them one at a time:
for _ in range(100):
print(choice(number_list))
If you want to build your list first you can use a "list comprehension":
choices = [choice(number_list) for _ in range(100)]
print(choices)
for i in range(100):
print(choice(number_list))
I have a list of elements (list) wich if formatted like this:
3823,La Canebiere,LOCATION
3949,La Canebiere,LOCATION
3959,Phocaeans,LOCATION
3990,Paris,LOCATION
323,Paris,LOCATION
3222,Paris,LOCATION
Some location names (elt[1]) may appear two or more times in the list, but with a different elt[0](id number). What I'm trying to achieve is adding frequency of elt[1] in the list, adding this frequency to each elt and discarding noun (elt[1]) duplicates. For my example the new tab would be :
3823,La Canebiere,LOCATION, 2
3959,Phocaeans,LOCATION,1
3990,Paris,LOCATION,3
I tried a count method and dictionnary for counting frequency but I don't know how to create the new list that would maintain the original list (without duplicates) plus the frequency. I'm using python 3. Thank you in advance if you can help !
Given the following list:
colors=['#c85200','#5f9ed1','lightgrey','#ffbc79','#006ba4','dimgray','#ff800e','#a2c8ec'
,'grey','salmon','cyan','silver']
And this list:
Hospital=['a','b','c','d']
After I get the number of colors based on the length of the list - 'Hospital':
num_hosp=len(Hospital)
colrs=colors[:num_hosp]
colrs
['#c85200', '#5f9ed1', 'lightgrey', '#ffbc79']
...and zip the lists together:
hcolrs=zip(Hospitals,colrs)
Next, I'd like to be able to select 1 or more colors from hcolrs if given a list of one or more hospitals from 'Hospitals'.
Like this:
newHosps=['a','c'] #input
newColrs=['#c85200','lightgrey'] #output
Thanks in advance!
Pass the result of zip to the dict constructor to make lookup simple/fast:
# Don't need to slice colors; zip stops when shortest iterable exhausted
hosp_to_color = dict(zip(Hospitals, colors))
then use it:
newHosps = ['a','c']
newColrs = [hosp_to_color[h] for h in newHosps]
I want to create a loop of zips I have sample.csv file with the below entries:
> 1 2 3 4
> a b c d
> apple banana cat dog
and have the below code:
sample= open("sample.csv)
lines = sample.readlines()
testcol = []
for l in lines:
zipped = zip(testcol ,l)
The output is:
[(('1','a'),'apple'),(('2','b'),'banana'),(('3','c'),'cat'),(('4','d'),'dog')]
but what i want is:
[('1','a','apple'),('2','b','banana'),('3','c','cat'),('4','d','dog')]
The reason why i have to put it in loops is because my sample.csv may contain arbitrary number of rows.
This should do the job:
sample = open("sample.csv)
lines = [line.split() for line in sample.readlines()] #splitting on whitespace to create list of lists
zipped = zip(*lines)
See Unpacking Argument Lists:
The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in range() function expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple.