Im new to python and hit a wall with my last print in my program
I got a list of numbers created with math int(numbers that when printed looks like this
[0, 0, 0, 0] #just with random numbers from 1 - 1000
I want to add text in front of every random number in list and print it out like this
[Paul 0 Frederick 0 Ape 0 Ida 0]
Any help would be appreciated. Thanks !
Sounds like you want to make a dictionary. You could type:
d = dict()
d["Paul"] = random.randint(1,100)
....
print(d)
#output: {"Paul":1, "Fredrick":50, "Ape":25, "Ida":32}
Alternatively there is nothing stopping you from using strings and integers in the same list! Python is not strongly statically typed.
If you have a list of numbers [45,5,59,253] and you want to add names to them you probably need a loop.
nums = [45,5,59,253]
names = ["Paul", "Frederick", "Ape", "Ida"]
d = dict()
i = 0
for n in nums:
d[names[i]] = n
i+=1
or if you wanted a list
nums = [45,5,59,253]
names = ["Paul", "Frederick", "Ape", "Ida"]
list = [x for y in zip(names, nums) for x in y]
You'd have to turn your random integers into strings and add them to the text (string) you want.
Example:
lst=[]
x = str(randint(0,1000))
text = 'Alex'
final_text = text+' '+x
lst.append(final_text)
Just added the space like in your example. It'll just be a little more complex to access the numbers if you do it this way.
Related
I have an empty list which is continuously being appended when data is inputted via the input function e.g.
x = []
After the first input, the list will be automatically appended to something like this:
x = [CAR]
how do I format the list to show something like this:
>>>Encryption 1: CAR
Then after the second input, the list will show :
x = [CAR, SHIP]
>>>Encryption 1: CAR
>>>Encryption 2: SHIP
I have started by creating two lists:
x = []
encryption_labels = [] this is where i am planning the Encryption 1, Encryption 2... and so on. To be formed.
but i am stuck :(
With just one list, you can iterate through the list using enumerate() to get both the index and the value. Then you can print stuff out depending on that:
x = ['CAR', 'SHIP']
for idx, val in enumerate(x):
print(f">>>Encryption {idx + 1}: {val}")
which prints the following:
>>>Encryption 1: CAR
>>>Encryption 2: SHIP
Just iterate through and access the respective index
for i in range(len(x)):
print(encryption_labels[i] + ": " + x[i])
You can convert x to encryption_labels at the end after finishing appending all the elements to x:
encryption_labels = [f">>>Encryption {i+1}: {xi}" for i, xi in enumerate(x)]
I am trying to append a list of 4 letters in my number as a [[a, b, c, d]] type of list.
I am looping through a list and appending the letter to a temp list and then appending it to my main list to make it into a matrix. However, the main list is only storing the number (8, 26) for some reason
ciphertext = "asfgasgsaga"
counter = 0
templist = []
xyz = []
for abc in ciphertext:
if(counter == 5):
print(templist)
xyz.append(templist)
templist.clear()
counter = 0
else:
templist.append(abc);
counter += 1
print(xyz)
The result is for some reason giving [[8, 26]]
The result is not the same as your expected because there some concepts that you need to know about objects in Python:
Immutable Objects: int, float, complex, string, tuple, frozen set, bytes. These kind of data types can't be changed the value after it is created. So that when we assign to another variable, it will copy the value to new variable. E.g:
a = 123
b = a
a = 456
print(b) #123
Mutable Objects: list, dict, set, byte array. These can be changed the value after it is created. And when you assign to another variable, it basically just assign the reference to previous variable like so:
a = []
b = a
a.append(123)
print(b) #[123]
So back to your problem, you're using list to create a list with 4 characters and then append it into another list, it's not append the expected list but instead a reference to it. That's why you got unexpected result.
And about the logic of your code, there are something go wrong, because when counter you will miss 1 character. You actually can switch to use slicing in Python:
ciphertext = "asfgasgsaga"
xyz = [ciphertext[start:start + 4] for start in range(0, len(ciphertext), 4)]
print(xyz) #['asfg', 'asgs', 'aga']
I'm using List Comprehension to append to xyz instead of call append function, create step like: 0:4, 4:8, 8:12, ... voila
Hope that helpful for you.
Just as #zvone says, don's use the same array and clear it, because they ref the same memory;
ciphertext = "asfgasgsaga"
counter = 0
templist = []
xyz = []
for abc in ciphertext:
if(counter == 4):
print(templist)
xyz.append(templist)
templist = [] # <--- use a new empty array
counter = 0
else:
templist.append(abc);
counter += 1
print(xyz)
Also, the correct logic(handle the letters less than 4) should be:
ciphertext = "asfgasgsaga"
counter = 0
templist = []
xyz = []
for abc in ciphertext:
templist.append(abc);
counter += 1
if(counter == 4):
print(templist)
xyz.append(templist)
templist = []
counter = 0
if templist:
xyz.append(templist)
print(xyz)
Just see #Toan Quoc Ho's answer, which should make more sense. Just leave the answer here to compare your origin logic.
I am working on a problem wherein I paste a set of numbers, and I want to put the even and odd list elements from these numbers and put them in their own list, then add them together for a 3rd list.
Here is my code:
#list of numbers
start = """
601393 168477
949122 272353
944397 564134
406351 745395
281988 610822
451328 644000
198510 606886
797923 388924
470601 938098
578263 113262
796982 62212
504090 378833
"""
x = start.split()
#turn string into list elements then append to a list
step_two = []
step_two.append(x)
print step_two
# doublecheck that step_two is a list with all the numbers
step_three = step_two[1::2]
#form new list that consists of all the odd elements
print step_three
#form new list that consists of all the even elements
step_four = step_two[0::2]
print step_four
#add ascending even/odd element pairs together and append to new list
final_step = [x + y for x, y in zip(step_three, step_four)]
print final_step
This code yields these results:
"""
"Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
[['601393', '168477', '949122', '272353', '944397', '564134', '406351', '745395'
, '281988', '610822', '451328', '644000', '198510', '606886', '797923', '388924'
, '470601', '938098', '578263', '113262', '796982', '62212', '504090', '378833']
]
[]
[['601393', '168477', '949122', '272353', '944397', '564134', '406351', '745395'
, '281988', '610822', '451328', '644000', '198510', '606886', '797923', '388924'
, '470601', '938098', '578263', '113262', '796982', '62212', '504090', '378833']
]
[]
"""
Why is my list in step_three and step_four not working? I am not sure why my [::] functions aren't registering.
Thanks in advance for any advice.
You were too explicit here in step two:
x = start.split()
#turn string into list elements then append to a list
step_two = []
step_two.append(x)
x is already the list you need. step_two creates a new list and adds the previous list to it, so instead of ['601393', '168477',...], you have [['601393', '168477',...]].
To fix this, simply call the split string step_two and proceed from there:
step_two = start.split()
The reason your lists don't split the numbers into odd and even is that your code assumes that the list alternates between them - you take every other index for each list generation, but the numbers aren't arranged that way in the original string.
You'll need to do an 'evenness' test:
step_two = start.split()
step_three = []
step_four = []
for item in step_two:
if int(item) % 2: # If it divides evenly by two, it returns 0, or False
step_three.append(item)
else:
step_four.append(item)
The lists step_three and step_four will now correctly contain only odds or evens.
Here is how I would generate the odd list, hope it helps!
import sys
# Provided main(), calls mimic_dict() and mimic()
def main():
#list of numbers
start = '601393 168477 949122 272353 944397 564134 406351 745395 281988 610822 451328 644000 198510 606886 797923 388924470601 938098 578263 113262 796982 62212 504090 378833'
x = start.split()
#turn string into list elements then append to a list
step_two = []
# gives you 23 items in your list "step_two" instead of 1 item with original append on it's own
for s in x:
step_two.append(s)
#print (step_two)
# print (step_two)
# doublecheck that step_two is a list with all the numbers
i = 1
step_three_odd_list = []
while i < len(step_two):
currentodditem = step_two[i]
step_three_odd_list.append(currentodditem)
i = i + 2
#form new list that consists of all the odd elements
print (step_three_odd_list)
if __name__ == '__main__':
main()
The [::] that you have going on are extended slices, not tests for even and odd:
https://docs.python.org/release/2.3.5/whatsnew/section-slices.html
Also, since those numbers are in a string, when you split them you end up with strings, not integers that you test with math. So, you need to convert them to integers.
This post clued me in on a quick even/odd test: Check if a number is odd or even in python
This code seems to do what you want, if I understand it correctly:
start = """
601393 168477
949122 272353
944397 564134
406351 745395
281988 610822
451328 644000
198510 606886
797923 388924
470601 938098
578263 113262
796982 62212
504090 378833
"""
# List comprehension to generate a list of the numbers converted to integers
step_two = [int(x) for x in start.split()]
# Sort the list
step_two.sort()
# Create new lists for even and odd
even_numbers = []
odd_numbers = []
# For each item in the list, test for even or odd and add to the right list
for number in step_two:
if (number % 2 == 0):
#even
even_numbers.append(number)
else:
#odd
odd_numbers.append(number)
print(even_numbers)
print(odd_numbers)
final_step = [x + y for x, y in zip(even_numbers, odd_numbers)]
print(final_step)
You can use list comprehensions to create two lists odd and even :
import itertools
start = """
601393 168477
949122 272353
944397 564134
406351 745395
281988 610822
451328 644000
198510 606886
797923 388924
470601 938098
578263 113262
796982 62212
504090 378833
"""
lst = start.split()
even = [int(i) for i in lst if int(i) % 2 == 0]
odd = [int(i) for i in lst if int(i) % 2 != 0]
Then you can zip these 2 lists but as even and odd are not the same length, you have to use itertools.zip_longest() in Python 3 and itertools.izip_longest() in Python 2. You will then have a list of tuples:
final_step = [i for i in itertools.zip_longest(even,odd)]
[(949122, 601393),
(564134, 168477),
(281988, 272353),
(610822, 944397),
(451328, 406351),
(644000, 745395),
(198510, 797923),
(606886, 470601),
(388924, 578263),
(938098, 378833),
(113262, None),
(796982, None),
(62212, None),
(504090, None)]
You can create the third list result to sum() the values using an other list comprehension but you have to make sure not to try to sum the tuples where one value is None.
result = [sum(e) if e[1] is not None else e[0] for e in final_step]
The final output:
[1550515,
732611,
554341,
1555219,
857679,
1389395,
996433,
1077487,
967187,
1316931,
113262,
796982,
62212,
504090]
This is a simple program but I am finding difficulty how it is actually working.
I have database with 3 tuples.
import matplotlib.pyplot as plt
queries = {}
rewrites = {}
urls = {}
for line in open("data.tsv"):
q, r, u = line.strip().split("\t")
queries.setdefault(q,0)
queries[q] += 1
rewrites.setdefault(r,0)
rewrites[r] += 1
urls.setdefault(u,0)
urls[u] += 1
sQueries = []
sQueries = [x for x in rewrites.values()]
sQueries.sort()
x = range(len(sQueries))
line, = plt.plot(x, sQueries, '-' ,linewidth=2)
plt.show()
This is whole program,
Now
queries.setdefault(q,0)
This command will set the values as 0 , if key i,e and q is not found.
queries[q] += 1
This command will increment the value of each key by 1 if key is there.
Same we continue with all tuples.
Then,
sQueries = [x for x in rewrites.values()]
Then we store the values from Dictionary rewrites , to List Squeries
x = range(len(sQueries))
This command I am not getting what is happening. Can anyone please explain.
len(sQueries)
gives number of elements in your list sQueries
x = range(len(sQueries))
will create a list x containing elements from 0,1,... to (but not including) length of your sQueries array
This:
sQueries = []
sQueries = [x for x in rewrites.values()]
sQueries.sort()
is an obtuse way of writing
sQueries = rewrites.values()
sQueries = sorted(sQueries)
in other words, sort the values of the rewrites dictionary. If, for the sake of argument, sQueries == [2, 3, 7, 9], then len(sQueries) == 4 and range(4) == [0, 1, 2, 3].
So, now you're plotting (0,2), (1,3), (2,7), (3,9), which doesn't seem very useful to me. It seems more likely that you would want the keys of rewrites on the x-axis, which would be the distinct values of r that you read from the TSV file.
length = len(sQueries) # this is length of sQueries
r = range(length) # this one means from 0 to length-1
so
x = range(len(sQueries)) # means x is from 0 to sQueries length - 1
I have a list of phone numbers that have been dialed (nums_dialed).
I also have a set of phone numbers which are the number in a client's office (client_nums)
How do I efficiently figure out how many times I've called a particular client (total)
For example:
>>>nums_dialed=[1,2,2,3,3]
>>>client_nums=set([2,3])
>>>???
total=4
Problem is that I have a large-ish dataset: len(client_nums) ~ 10^5; and len(nums_dialed) ~10^3.
which client has 10^5 numbers in his office? Do you do work for an entire telephone company?
Anyway:
print sum(1 for num in nums_dialed if num in client_nums)
That will give you as fast as possible the number.
If you want to do it for multiple clients, using the same nums_dialed list, then you could cache the data on each number first:
nums_dialed_dict = collections.defaultdict(int)
for num in nums_dialed:
nums_dialed_dict[num] += 1
Then just sum the ones on each client:
sum(nums_dialed_dict[num] for num in this_client_nums)
That would be a lot quicker than iterating over the entire list of numbers again for each client.
>>> client_nums = set([2, 3])
>>> nums_dialed = [1, 2, 2, 3, 3]
>>> count = 0
>>> for num in nums_dialed:
... if num in client_nums:
... count += 1
...
>>> count
4
>>>
Should be quite efficient even for the large numbers you quote.
Using collections.Counter from Python 2.7:
dialed_count = collections.Counter(nums_dialed)
count = sum(dialed_count[t] for t in client_nums)
Thats very popular way to do some combination of sorted lists in single pass:
nums_dialed = [1, 2, 2, 3, 3]
client_nums = [2,3]
nums_dialed.sort()
client_nums.sort()
c = 0
i = iter(nums_dialed)
j = iter(client_nums)
try:
a = i.next()
b = j.next()
while True:
if a < b:
a = i.next()
continue
if a > b:
b = j.next()
continue
# a == b
c += 1
a = i.next() # next dialed
except StopIteration:
pass
print c
Because "set" is unordered collection (don't know why it uses hashes, but not binary tree or sorted list) and it's not fair to use it there. You can implement own "set" through "bisect" if you like lists or through something more complicated that will produce ordered iterator.
The method I use is to simply convert the set into a list and then use the len() function to count its values.
set_var = {"abc", "cba"}
print(len(list(set_var)))
Output:
2