Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I am required to take 52 random outputs of cards. I got that in a for loop. The problem is, I need to save that output inside a variable.`
import random
r=random.randint(0, 9)
cards={'Spades':r, 'Clubs':r, 'Hearts':r, 'Diamonds':r,'Jack':10, 'King':10, 'queen':10,"Aces":1}
print(cards)
cards2={}
for i in range(52):
global res
res = key, val = random.choice(list(cards.items()))
print("Your deck contains " + str(res))
cards2.update(i) # All output should go in here
I tried using cards2.update, but it didn't work.
I also tried using cards2.(keys).
I just need to create 52 random samples and store them as dictionary value pairs.
First remove the double assignment (res = key, val). And I don't see any point in using a global variable here. Just do _dict[key] = value as shown below, and it will work fine. Also remember that you can’t get all 52 random cards, because if the key exists then the value will be replaced.
import random
r = random.randint(0, 9)
cards = {'Spades':r, 'Clubs':r, 'Hearts':r, 'Diamonds':r,'Jack':10, 'King':10, 'queen':10,"Aces":1}
print(cards)
cards2 = {}
for i in range(52):
key, val = random.choice(list(cards.items()))
cards2[key] = val
print(cards2)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to make some code that lets me use a function to get the price of items and sum them together quickly.
So a simplified version of my code currently looks like this:
#Header = {Item, Price, Service Tax}
Dairy = {'Milk': [2.30, 0.16, 'Yes'],
'Butter': [4.50, 0.32, 'No']}
Cart = []
Cart.append('Milk')
Cart.append('Butter')
Collective = []
Collective.append(Cart[0])
print(sum(Collective))
This results in either Unhashable error or nothing being printed at all, when I'm looking for the sum of 2.30 and 4.50.
Just try using this code:
print(sum([sublist[0] for sublist in Dairy.values()]))
Output:
6.8
If you mean that Dairy contains lots of stuff, but you only want some information, whose keys are in Cart, then you can do this:
#Header = {Item, Price, Service Tax}
Dairy = {'Milk': [2.30, 0.16, 'Yes'],
'Butter': [4.50, 0.32, 'No'],
'other': [42.50, 0.42, 'No']}
Cart = []
Cart.append('Milk')
Cart.append('Butter')
Collective = [Dairy[item][0] for item in Cart]
print(sum(Collective))
Output: 6.8
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to loop through this csv that has country, data, and a number I need to extract. The file looks like this:
b'/O_o/\ngoogle.visualization.Query.setResponse({"version":"0.6","reqId":"0output=csv","status":"ok","sig":"1241529276","table":{"cols":[{"id":"A","label":"Entity","type":"string"},{"id":"B","label":"Week","type":"number","pattern":"General"},{"id":"C","label":"Day","type":"date","pattern":"yyyy-mm-dd"},{"id":"D","label":"Flights
2019
(Reference)","type":"number","pattern":"General"},{"id":"E","label":"Flights","type":"number","pattern":"General"},{"id":"F","label":"%
vs 2019
(Daily)","type":"number","pattern":"General"},{"id":"G","label":"Flights
(7-day moving
average)","type":"number","pattern":"General"},{"id":"H","label":"% vs
2019 (7-day Moving
Average)","type":"number","pattern":"General"},{"id":"I","label":"Day
2019","type":"date","pattern":"yyyy-mm-dd"},{"id":"J","label":"Day
Previous
Year","type":"date","pattern":"yyyy-mm-dd"},{"id":"K","label":"Flights
Previous
Year","type":"number","pattern":"General"}],"rows":[{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,1)","f":"2020-09-01"},{"v":129.0,"f":"129"},{"v":64.0,"f":"64"},{"v":-0.503875968992248,"f":"-0,503875969"},{"v":71.5714285714286,"f":"71,57142857"},{"v":-0.291371994342291,"f":"-0,2913719943"},{"v":"Date(2019,8,3)","f":"2019-09-03"},{"v":"Date(2019,8,3)","f":"2019-09-03"},{"v":129.0,"f":"129"}]},{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,2)","f":"2020-09-02"},{"v":92.0,"f":"92"},{"v":59.0,"f":"59"},{"v":-0.358695652173913,"f":"-0,3586956522"},{"v":70.0,"f":"70"},{"v":-0.300998573466476,"f":"-0,3009985735"},{"v":"Date(2019,8,4)","f":"2019-09-04"},{"v":"Date(2019,8,4)","f":"2019-09-04"},{"v":92.0,"f":"92"}]},{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,3)","f":"2020-09-03"},{"v":96.0,"f":"96"},{"v":67.0,"f":"67"},{"v":-0.302083333333333,"f":"-0,3020833333"},{"v":70.1428571428571,"f":"70,14285714"},{"v":-0.30354609929078,"f":"-0,3035460993"},{"v":"Date(2019,8,5)","f":"2019-09-05"},{"v":"Date(2019,8,5)","f":"2019-09-05"},{"v":96.0,"f":"96"}]},{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,4)","f":"2020-09-04"},{"v":103.0,"f":"103"},{"v":89.0,"f":"89"},{"v":-0.135922330097087,"f":"-0,1359223301"},{"v":69.2857142857143,"f":"69,28571429"},{"v":-0.312056737588652,"f":"-0,3120567376"},{"v":"Date(2019,8,6)","f":"2019-09-06"},{"v":"Date(2019,8,6)","f":"2019-09-06"},{"v":103.0,"f":"103"}]},{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,5)","f":"2020-09-05"},{"v":94.0,"f":"94"},{"v":53.0,"f":"53"},{"v":-0.436170212765957,"f":"-0,4361702128"},{"v":68.8571428571429,"f":"68,85714286"},{"v":-0.314366998577525,"f":"-0,3143669986"},{"v":"Date(2019,8,7)","f":"2019-09-07"},{"v":"Date(2019,8,7)","f":"2019-09-07"},{"v":94.0,"f":"94"}]}, ...
In there it says Albania, which is a country with data I need to extract. For example:
{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,4)","f":"2020-09-04"},{"v":103.0,"f":"103"},{"v":89.0,"f":"89"},{"v":-0.135922330097087,"f":"-0,1359223301"},{"v":69.2857142857143,"f":"69,28571429"},{"v":-0.312056737588652,"f":"-0,3120567376"},{"v":"Date(2019,8,6)","f":"2019-09-06"},{"v":"Date(2019,8,6)","f":"2019-09-06"},{"v":103.0,"f":"103"}]}
How would I write a python script to loop over the entire csv file and find every occurrence of the word "Albania", save it, then go a little further and get the date "2020-09-04", and then get the number -0.1359?
You can read this using json.
import json
with open('txtfile1.txt') as input:
data = input.read()
#Select the part where the dictionary start (after "rows":)
nesteddict = json.loads(data[(data.find('rows')+6):])
finallist = []
for x in nesteddict:
sublist = []
sublist.append(x['c'][0]['v'])
sublist.append(x['c'][2]['f'])
sublist.append(x['c'][5]['v'])
finallist.append(sublist)
This will output
[['Albania', '2020-09-01', -0.503875968992248], ['Albania', '2020-09-02', -0.358695652173913], ['Albania', '2020-09-03', -0.302083333333333], ['Albania', '2020-09-04', -0.135922330097087], ['Albania', '2020-09-05', -0.436170212765957]]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have below list:
list_a = ['USD', 'Notional Amount:', 'USD', '50,000,000.00', 'KRW', 'Notional Amount:', 'KRW', '53,585,000,000']
list_a include Currency code like 'usd' and amount like 50,000,000.00
By using python, I want to classify following:
am_list = [50000000,53585000000]
cu_list = ['USD','USD','KRW' 'KRW']
Anyone who will solve it easily give me solution please.
Just loop over it and check the type of element:
import re
list_a = ['USD', 'Notional Amount:', 'USD', '50,000,000.00', 'KRW', 'Notional Amount:', 'KRW', '53,585,000,000']
am_list = []
cu_list = []
for element in list_a:
# if element is number like combination of "number" and "," and "."
if re.match('^[\d\.,]+$', element):
am_list.append(element)
# curency symble 2 or 3 letters
if re.match('^[A-Z]{2,3}$', element):
cu_list.append(element)
print(am_list) # ['50,000,000.00', '53,585,000,000']
print(cu_list) # ['USD', 'USD', 'KRW', 'KRW']
If you want to make sure it's a valid amount that looks like this : '50,000,000.00', '50.00', '50,000.00', '50' ... etc:
re.match('^(\d+,)*\d+(\.\d+)?$', element)