Is there a way I can simplify the long for loop I have in my example with a few nested for loops? I'm writing to a CSV in the long loop.
Here's the long loop with:
int_1 = 1
int_2 = 2
interface = 41
for i in range(1, 5):
# t2-a-1
writer.writerow(
[
f"t2-a-1 Twe1/0/{int_1}",
f"t1-a-{i} Twe1/0/{interface}",
]
)
writer.writerow(
[
f"t2-a-1 Twe1/0/{int_2}",
f"t1-a-{i} Twe1/0/{interface + 1}",
]
)
# t2-a-2
writer.writerow(
[
f"t2-a-2 Twe1/0/{int_1}",
f"t1-a-{i} Twe1/0/{interface + 2}",
]
)
writer.writerow(
[
f"t2-a-2 Twe1/0/{int_2}",
f"t1-a-{i} Twe1/0/{interface + 3}",
]
)
# t2-a-3
writer.writerow(
[
f"t2-a-3 Twe1/0/{int_1}",
f"t1-a-{i} Twe1/0/{interface + 4}",
]
)
writer.writerow(
[
f"t2-a-3 Twe1/0/{int_2}",
f"t1-a-{i} Twe1/0/{interface + 5}",
]
)
# t2-a-4
writer.writerow(
[
f"t2-a-4 Twe1/0/{int_1}",
f"t1-a-{i} Twe1/0/{interface + 6}",
]
)
writer.writerow(
[
f"t2-a-4 Twe1/0/{int_2}",
f"t1-a-{i} Twe1/0/{interface + 7}",
]
)
int_1 += 2
int_2 += 2
The output that I need should look like this:
t2-a-1 Twe1/0/1,t1-a-1 Twe1/0/41
t2-a-1 Twe1/0/2,t1-a-1 Twe1/0/42
t2-a-2 Twe1/0/1,t1-a-1 Twe1/0/43
t2-a-2 Twe1/0/2,t1-a-1 Twe1/0/44
t2-a-3 Twe1/0/1,t1-a-1 Twe1/0/45
t2-a-3 Twe1/0/2,t1-a-1 Twe1/0/46
t2-a-4 Twe1/0/1,t1-a-1 Twe1/0/47
t2-a-4 Twe1/0/2,t1-a-1 Twe1/0/48
t2-a-1 Twe1/0/3,t1-a-2 Twe1/0/41
t2-a-1 Twe1/0/4,t1-a-2 Twe1/0/42
t2-a-2 Twe1/0/3,t1-a-2 Twe1/0/43
t2-a-2 Twe1/0/4,t1-a-2 Twe1/0/44
t2-a-3 Twe1/0/3,t1-a-2 Twe1/0/45
t2-a-3 Twe1/0/4,t1-a-2 Twe1/0/46
t2-a-4 Twe1/0/3,t1-a-2 Twe1/0/47
t2-a-4 Twe1/0/4,t1-a-2 Twe1/0/48
t2-a-1 Twe1/0/5,t1-a-3 Twe1/0/41
t2-a-1 Twe1/0/6,t1-a-3 Twe1/0/42
t2-a-2 Twe1/0/5,t1-a-3 Twe1/0/43
t2-a-2 Twe1/0/6,t1-a-3 Twe1/0/44
t2-a-3 Twe1/0/5,t1-a-3 Twe1/0/45
t2-a-3 Twe1/0/6,t1-a-3 Twe1/0/46
t2-a-4 Twe1/0/5,t1-a-3 Twe1/0/47
t2-a-4 Twe1/0/6,t1-a-3 Twe1/0/48
t2-a-1 Twe1/0/7,t1-a-4 Twe1/0/41
t2-a-1 Twe1/0/8,t1-a-4 Twe1/0/42
t2-a-2 Twe1/0/7,t1-a-4 Twe1/0/43
t2-a-2 Twe1/0/8,t1-a-4 Twe1/0/44
t2-a-3 Twe1/0/7,t1-a-4 Twe1/0/45
t2-a-3 Twe1/0/8,t1-a-4 Twe1/0/46
t2-a-4 Twe1/0/7,t1-a-4 Twe1/0/47
t2-a-4 Twe1/0/8,t1-a-4 Twe1/0/48
This is what I've tried, but it needs some tweaking:
int_1 = 1
int_2 = 2
interface = 41
for i in range(1, 5):
for j in range(8):
if j % 2 == 0:
thisint = int_1
else:
thisint = int_2
print(f"t2-a-1 Twe1/0/{thisint},t1-a-{i} Twe1/0/{interface + j}")
int_1 += 2
int_2 += 2
Which produces:
t2-a-1 Twe1/0/1,t1-a-1 Twe1/0/41
t2-a-1 Twe1/0/2,t1-a-1 Twe1/0/42
t2-a-1 Twe1/0/1,t1-a-1 Twe1/0/43
t2-a-1 Twe1/0/2,t1-a-1 Twe1/0/44
t2-a-1 Twe1/0/1,t1-a-1 Twe1/0/45
t2-a-1 Twe1/0/2,t1-a-1 Twe1/0/46
t2-a-1 Twe1/0/1,t1-a-1 Twe1/0/47
t2-a-1 Twe1/0/2,t1-a-1 Twe1/0/48
t2-a-2 Twe1/0/3,t1-a-2 Twe1/0/41
t2-a-2 Twe1/0/4,t1-a-2 Twe1/0/42
t2-a-2 Twe1/0/3,t1-a-2 Twe1/0/43
t2-a-2 Twe1/0/4,t1-a-2 Twe1/0/44
t2-a-2 Twe1/0/3,t1-a-2 Twe1/0/45
t2-a-2 Twe1/0/4,t1-a-2 Twe1/0/46
t2-a-2 Twe1/0/3,t1-a-2 Twe1/0/47
t2-a-2 Twe1/0/4,t1-a-2 Twe1/0/48
t2-a-3 Twe1/0/5,t1-a-3 Twe1/0/41
t2-a-3 Twe1/0/6,t1-a-3 Twe1/0/42
t2-a-3 Twe1/0/5,t1-a-3 Twe1/0/43
t2-a-3 Twe1/0/6,t1-a-3 Twe1/0/44
t2-a-3 Twe1/0/5,t1-a-3 Twe1/0/45
t2-a-3 Twe1/0/6,t1-a-3 Twe1/0/46
t2-a-3 Twe1/0/5,t1-a-3 Twe1/0/47
t2-a-3 Twe1/0/6,t1-a-3 Twe1/0/48
t2-a-4 Twe1/0/7,t1-a-4 Twe1/0/41
t2-a-4 Twe1/0/8,t1-a-4 Twe1/0/42
t2-a-4 Twe1/0/7,t1-a-4 Twe1/0/43
t2-a-4 Twe1/0/8,t1-a-4 Twe1/0/44
t2-a-4 Twe1/0/7,t1-a-4 Twe1/0/45
t2-a-4 Twe1/0/8,t1-a-4 Twe1/0/46
t2-a-4 Twe1/0/7,t1-a-4 Twe1/0/47
t2-a-4 Twe1/0/8,t1-a-4 Twe1/0/48
How can I tweak the simplified code to do what I want?
Given your output, you could maybe use:
interface = 41
for i in range(4):
for j in range(8):
print(f't2-a-{i+1} Twe1/0/{j+1},t1-a-{j//2+1} Twe1/0/{interface+2*i+j%2}')
NB. using print for quick reproducibility here, replace with writer.writerow in your context.
Output:
t2-a-1 Twe1/0/1,t1-a-1 Twe1/0/41
t2-a-1 Twe1/0/2,t1-a-1 Twe1/0/42
t2-a-1 Twe1/0/3,t1-a-2 Twe1/0/41
t2-a-1 Twe1/0/4,t1-a-2 Twe1/0/42
t2-a-1 Twe1/0/5,t1-a-3 Twe1/0/41
t2-a-1 Twe1/0/6,t1-a-3 Twe1/0/42
t2-a-1 Twe1/0/7,t1-a-4 Twe1/0/41
t2-a-1 Twe1/0/8,t1-a-4 Twe1/0/42
t2-a-2 Twe1/0/1,t1-a-1 Twe1/0/43
t2-a-2 Twe1/0/2,t1-a-1 Twe1/0/44
t2-a-2 Twe1/0/3,t1-a-2 Twe1/0/43
t2-a-2 Twe1/0/4,t1-a-2 Twe1/0/44
t2-a-2 Twe1/0/5,t1-a-3 Twe1/0/43
t2-a-2 Twe1/0/6,t1-a-3 Twe1/0/44
t2-a-2 Twe1/0/7,t1-a-4 Twe1/0/43
t2-a-2 Twe1/0/8,t1-a-4 Twe1/0/44
t2-a-3 Twe1/0/1,t1-a-1 Twe1/0/45
t2-a-3 Twe1/0/2,t1-a-1 Twe1/0/46
t2-a-3 Twe1/0/3,t1-a-2 Twe1/0/45
t2-a-3 Twe1/0/4,t1-a-2 Twe1/0/46
t2-a-3 Twe1/0/5,t1-a-3 Twe1/0/45
t2-a-3 Twe1/0/6,t1-a-3 Twe1/0/46
t2-a-3 Twe1/0/7,t1-a-4 Twe1/0/45
t2-a-3 Twe1/0/8,t1-a-4 Twe1/0/46
t2-a-4 Twe1/0/1,t1-a-1 Twe1/0/47
t2-a-4 Twe1/0/2,t1-a-1 Twe1/0/48
t2-a-4 Twe1/0/3,t1-a-2 Twe1/0/47
t2-a-4 Twe1/0/4,t1-a-2 Twe1/0/48
t2-a-4 Twe1/0/5,t1-a-3 Twe1/0/47
t2-a-4 Twe1/0/6,t1-a-3 Twe1/0/48
t2-a-4 Twe1/0/7,t1-a-4 Twe1/0/47
t2-a-4 Twe1/0/8,t1-a-4 Twe1/0/48
If your intended output is correct, then you can simply use
thisint = 41
for i in range(1, 5):
for j in range(8):
print("t2-a-{i} Twe1/0/{j+1},t1-a-{j//2+1} Twe1/0/{thisint + j % 2}")
thisint += 2
The following code uses 2 for loop and divides the elements for the output, in 3 parts:
first_element: it depends from the value of i and j
int_ value: it depends only from j
interface_ (that depends from the value of interface and j)
interface = 41
for i in range(1, 5):
for j in range(0, 8):
# set the first_element
first_element = f"t2-a-{i} Twe1/0/{j+1}"
# set int_ value
int_ = j//2 + 1
# set interface value
interface_ = interface + j%2
print([first_element, f"t1-a-{int_} Twe1/0/{interface_}", ])
#writer.writerow([first_element, f"t1-a-{int_} Twe1/0/{interface_}", ])
interface += 2
The output of print() is the following:
['t2-a-1 Twe1/0/1', 't1-a-1 Twe1/0/41']
['t2-a-1 Twe1/0/2', 't1-a-1 Twe1/0/42']
['t2-a-1 Twe1/0/3', 't1-a-2 Twe1/0/41']
['t2-a-1 Twe1/0/4', 't1-a-2 Twe1/0/42']
['t2-a-1 Twe1/0/5', 't1-a-3 Twe1/0/41']
['t2-a-1 Twe1/0/6', 't1-a-3 Twe1/0/42']
['t2-a-1 Twe1/0/7', 't1-a-4 Twe1/0/41']
['t2-a-1 Twe1/0/8', 't1-a-4 Twe1/0/42']
['t2-a-2 Twe1/0/1', 't1-a-1 Twe1/0/43']
['t2-a-2 Twe1/0/2', 't1-a-1 Twe1/0/44']
['t2-a-2 Twe1/0/3', 't1-a-2 Twe1/0/43']
['t2-a-2 Twe1/0/4', 't1-a-2 Twe1/0/44']
['t2-a-2 Twe1/0/5', 't1-a-3 Twe1/0/43']
['t2-a-2 Twe1/0/6', 't1-a-3 Twe1/0/44']
['t2-a-2 Twe1/0/7', 't1-a-4 Twe1/0/43']
['t2-a-2 Twe1/0/8', 't1-a-4 Twe1/0/44']
['t2-a-3 Twe1/0/1', 't1-a-1 Twe1/0/45']
['t2-a-3 Twe1/0/2', 't1-a-1 Twe1/0/46']
['t2-a-3 Twe1/0/3', 't1-a-2 Twe1/0/45']
['t2-a-3 Twe1/0/4', 't1-a-2 Twe1/0/46']
['t2-a-3 Twe1/0/5', 't1-a-3 Twe1/0/45']
['t2-a-3 Twe1/0/6', 't1-a-3 Twe1/0/46']
['t2-a-3 Twe1/0/7', 't1-a-4 Twe1/0/45']
['t2-a-3 Twe1/0/8', 't1-a-4 Twe1/0/46']
['t2-a-4 Twe1/0/1', 't1-a-1 Twe1/0/47']
['t2-a-4 Twe1/0/2', 't1-a-1 Twe1/0/48']
['t2-a-4 Twe1/0/3', 't1-a-2 Twe1/0/47']
['t2-a-4 Twe1/0/4', 't1-a-2 Twe1/0/48']
['t2-a-4 Twe1/0/5', 't1-a-3 Twe1/0/47']
['t2-a-4 Twe1/0/6', 't1-a-3 Twe1/0/48']
['t2-a-4 Twe1/0/7', 't1-a-4 Twe1/0/47']
['t2-a-4 Twe1/0/8', 't1-a-4 Twe1/0/48']
Related
i'm writing function that gets an array of stock prices and calculates the best buy and sale profit.
Each value in the array is the stock price and the index number is the hour of the price.
how can keep indexes of the prices i want to buy and sell?
def inputValidation(price):
isValidPrice = False
# Validate input type is a number
if isinstance(price, int) or isinstance(price, float):
# Validate number > 0
if price < 0:
isValidPrice = False
else:
isValidPrice = True
return isValidPrice
def arraySizeValidation(arr):
if 2 < len(arr) < 24:
return True
else:
return False
def stockManagement(arr):
potentialProfit = 0
maxProfitValue = 0
currentMaxValue = 0
minIndex = 0
maxIndex = len(arr)
tempArr = arr
if arraySizeValidation(arr):
for price in reversed(arr):
# Validates input
if inputValidation(price):
print("current max:", currentMaxValue)
print("price:", price)
currentMaxValue = max(currentMaxValue, price)
potentialProfit = currentMaxValue - price
minIndex = arr.index(price)
maxIndex = arr.index(currentMaxValue)
print("potential_profit:", potentialProfit)
maxProfitValue = max(potentialProfit, maxProfitValue)
print("maxProfitValue:", maxProfitValue)
print("-------------")
print("\n")
else:
print("Array size not valid")
arr1 = [97,101,2,99,3,25,37]
arr2 = [13, 15, 7, "s", "s", 25, 37]
hours = ["12:00 AM", "01:00 AM", "02:00 AM", "03:00 AM", "04:00 AM", "05:00 AM", "06:00 AM", "07:00 AM",
"08:00 AM", "09:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "13:00 PM", "14:00 PM", "15:00 PM", "16:00 PM",
"17:00 PM", "18:00 PM", "19:00 PM", "20:00 PM", "21:00 PM", "22:00 PM", "23:00 PM"]
stockManagement(arr1)
I suggest that you save the price and its indexes in a list, then return it at the end:
def stockManagement(arr):
maxProfitValue = 0
currentMaxValue = 0
# The list that will be returned at the end
index_list = []
minIndex = 0
maxIndex = len(arr)
for price in reversed(arr):
# Validates input
if inputValidation(price):
print("current max:", currentMaxValue)
print("price:", price)
currentMaxValue = max(currentMaxValue, price)
potentialProfit = currentMaxValue - price
minIndex = arr.index(price)
maxIndex = arr.index(currentMaxValue)
if maxProfitValue > potentialProfit:
minIndex = arr.index(price)
maxIndex = arr.index(currentMaxValue)
print(minIndex)
print(maxIndex)
print("potential_profit:",potentialProfit)
maxProfitValue = max(potentialProfit, maxProfitValue)
# We add to the list a nested list that contains the price and its indexes
index_list.append([price, minIndex, maxIndex])
print("maxProfitValue:", maxProfitValue)
print("-------------")
print("\n")
print(minIndex)
print(maxIndex)
print(maxProfitValue)
# We return the result
return index_list
def inputValidation(price):
isValidPrice = False
if isinstance(price,int) or isinstance(price,float):
isValidPrice = True
return isValidPrice
arr1 = [101,15,2,99,3,25,37]
arr2 = [13,15,7,"s","s",25,37]
# Do whatever you want with the indexes
print(stockManagement(arr1))
Output:
[[37, 6, 6], [25, 5, 6], [3, 4, 6], [99, 3, 3], [2, 2, 3], [15, 1, 3], [101, 0, 0]]
I have the following dataset
test_table = spark.createDataFrame(
[
("US", "CA", "S", "2022-10-01",100, 10, 1),
("US", "CA", "M", "2022-10-01",100, 15, 5),
("US", "CA", "L", "2022-10-01",100, 20, 10),
("US", "CA", "S", "2022-10-01",200, 10, 1),
("US", "CA", "M", "2022-10-01",200, 15, 5),
("US", "CA", "L", "2022-10-01",200, 20, 10),
("US", "CA", "S", "2022-10-02",100, 11, 1),
("US", "CA", "M", "2022-10-02",100, 13, 3),
("US", "CA", "L", "2022-10-02",100, 17, 7),
("US", "CA", "S", "2022-10-02",200, 11, 1),
("US", "CA", "M", "2022-10-02",200, 13, 3),
],
schema=["country_code","state_code","size","dt","store_id","ttl_sold","ttl_returned"]
)
I then do some aggregations and end up with 2 columns (latest_payload, prev_payload). These 2 columns have the following datatype.
w = Window.partitionBy("country_code", "state_code", "size", "store_id").orderBy("dt").rangeBetween(Window.unboundedPreceding,0)
w2 = Window.partitionBy("country_code", "state_code", "size").orderBy("dt")
df_w_cumulative_sum = (
test_table
.withColumn("cumulative_ttl_sold", F.sum("ttl_sold").over(w))
.withColumn("cumulative_ttl_returned", F.sum("ttl_returned").over(w))
.groupBy("dt","country_code", "state_code", "size")
.agg(F.collect_list(F.create_map(F.col("store_id"), F.struct(F.col("cumulative_ttl_sold"), F.col("cumulative_ttl_returned")))).alias("latest_payload"))
.withColumn("prev_payload", F.lag(F.col("latest_payload"), 1).over(w2))
.where(F.col("dt") == "2022-10-02")
)
row
dt
country_code
state_code
size
latest_payload
prev_payload
1
2022-10-01
US
CA
L
[{"100":{"cumulative_ttl_sold":20,"cumulative_ttl_returned":10}},{"200":{"cumulative_ttl_sold":20,"cumulative_ttl_returned":10}}]
null
2
2022-10-01
US
CA
M
[{"100":{"cumulative_ttl_sold":15,"cumulative_ttl_returned":5}},{"200":{"cumulative_ttl_sold":15,"cumulative_ttl_returned":5}}]
null
3
2022-10-01
US
CA
S
[{"100":{"cumulative_ttl_sold":10,"cumulative_ttl_returned":1}},{"200":{"cumulative_ttl_sold":10,"cumulative_ttl_returned":1}}]
null
4
2022-10-02
US
CA
L
[{"100":{"cumulative_ttl_sold":37,"cumulative_ttl_returned":17}}]
[{"100":{"cumulative_ttl_sold":20,"cumulative_ttl_returned":10}},{"200":{"cumulative_ttl_sold":20,"cumulative_ttl_returned":10}}]
5
2022-10-02
US
CA
M
[{"100":{"cumulative_ttl_sold":28,"cumulative_ttl_returned":8}},{"200":{"cumulative_ttl_sold":28,"cumulative_ttl_returned":8}}]
[{"100":{"cumulative_ttl_sold":15,"cumulative_ttl_returned":5}},{"200":{"cumulative_ttl_sold":15,"cumulative_ttl_returned":5}}]
6
2022-10-02
US
CA
S
[{"100":{"cumulative_ttl_sold":21,"cumulative_ttl_returned":2}},{"200":{"cumulative_ttl_sold":21,"cumulative_ttl_returned":2}}]
[{"100":{"cumulative_ttl_sold":10,"cumulative_ttl_returned":1}},{"200":{"cumulative_ttl_sold":10,"cumulative_ttl_returned":1}}]
Expected Output for row 4
{'100': {'cumulative_ttl_sold': 37, 'cumulative_ttl_returned': 17}, '200': {'cumulative_ttl_sold': 20, 'cumulative_ttl_returned': 10}}
Attempted Solution: Gives me the wrong values for each row
#F.udf(
MapType(
IntegerType(),
StructType([
StructField("cumulative_ttl_sold", LongType(), False),
StructField("cumulative_ttl_sold", LongType(), False)
])
)
)
def merge_payloads(lastest_payload, prev_payload):
payload: Dict[int, Dict[str, int]] = {}
if prev_payload is not None:
for latest in lastest_payload:
for k,v in latest.items():
payload[k] = v
for prev in prev_payload:
for k,v in prev.items():
if k not in payload.keys():
payload[k]=v
else:
break
else:
for latest in lastest_payload:
for k, v in latest.items():
payload[k] = v
return payload
Give this all the correct decorators and such, and it'll do what you're looking for...
def merge_payloads(latest_payload, prev_payload):
return dict(y for x in [*prev_payload, *latest_payload] for y in x.items())
latest = [{'100': {'cumulative_ttl_sold': 37, 'cumulative_ttl_returned': 17}}]
prev = [{'100': {'cumulative_ttl_sold': 20, 'cumulative_ttl_returned': 10}},
{'200': {'cumulative_ttl_sold': 20, 'cumulative_ttl_returned': 10}}]
print(merge_payloads(latest, prev))
# Output:
{'100': {'cumulative_ttl_sold': 37, 'cumulative_ttl_returned': 17},
'200': {'cumulative_ttl_sold': 20, 'cumulative_ttl_returned': 10}}
I've a list in which I have to split it in 9 parts but I got a list of lists, and I want to get a list of tuples. I don´t know if I can do this on python.
data= [1, 'PURECO', 'WOMAN', 'MARISCAL', 'MULLER', 28, 'DIABETES', 'APC', 2020-01-03, 2, 'CAO', 'WOMAN', 'CAL Y MAYOR', 'URTIZ', 34, 'OBESIDAD TIPO 1', 'APC', 2020-01-03]
I tried this:
data = [data[i:i+9] for i in range(0, len(data),9)]
I got this:
data= [[1, 'PURECO', 'WOMAN', 'MARISCAL', 'MULLER', 28, 'DIABETES', 'APC', 2020-01-03],[ 2, 'CAO', 'WOMAN', 'CAL Y MAYOR', 'URTIZ', 34, 'OBESIDAD TIPO 1', 'APC', 2020-01-03]]
But, I want to get this:
data= [(1, 'PURECO', 'WOMAN', 'MARISCAL', 'MULLER', 28, 'DIABETES', 'APC', 2020-01-03),(2, 'CAO', 'WOMAN', 'CAL Y MAYOR', 'URTIZ', 34, 'OBESIDAD TIPO 1', 'APC', 2020-01-03)]
Your solution is almost finished, you can just convert to tuple:
data = [tuple(data[i:i+9]) for i in range(0, len(data),9)]
^ ^
this will convert inner lists to tuples
>>> [tuple(data[i:i+9]) for i in range(0, len(data),9)]
[(1, 'PURECO', 'WOMAN', 'MARISCAL', 'MULLER', 28, 'DIABETES', 'APC', '2020-01-03'), (2, 'CAO', 'WOMAN', 'CAL Y MAYOR', 'URTIZ', 34, 'OBESIDAD TIPO 1', 'APC', '2020-01-03')]
You can try this:
list_of_tuples = [tuple(item) for item in data]
The following codes run the result of unexpected, i think that is somewhat weird, firstly define the featfun():
featfun <- function(yi_1, yi, i) {
all_fea <- list(c(1, 2, 2),
c(1, 2, 3),
c(1, 1, 2),
c(2, 1, 3),
c(2, 1, 2),
c(2, 2, 3),
c( 1, 1),
c( 2, 1),
c( 2, 2),
c( 1, 2),
c( 1, 3),
c( 2, 3))
weights <- c(1,1,0.6,1,1,0.2,1,0.5,0.5,0.8,0.8,0.5)
idx1 <- 0; idx2 <- 0
if (list(c(yi_1, yi, i)) %in% all_fea) {
idx1 <- which(all_fea %in% list(c(yi_1, yi, i)))
}
if (list(c(yi, i)) %in% all_fea) {
idx2 <- which(all_fea %in% list(c(yi, i)))
}
if (idx1 != 0 & idx2 != 0) {
return(list(c(1, weights[idx1]), c(1, weights[idx2])))
} else if (idx1 != 0 & idx2 == 0) {
return(list(c(1, weights[idx1])))
} else if (idx1 == 0 & idx2 != 0) {
return(list(c(1, weights[idx2])))
} else {
return(NA)
}
}
> featfun(1,1,2)
[[1]]
[1] 1.0 0.6
[[2]]
[1] 1.0 0.8
I combine the featfun() with for loops:
> for (k in seq(2,3)) {
+ cat("k=",k,"\n")
+ for (i in seq(1, 2)) {
+ cat("i=", i,"\n")
+ print(featfun(1, i, k))
+ }
+ }
k= 2
i= 1
[[1]]
[1] 1.0 0.6
i= 2
[[1]]
[1] 1 1
[[2]]
[1] 1.0 0.5
k= 3
i= 1
[[1]]
[1] 1.0 0.8
i= 2
[[1]]
[1] 1 1
As we can see, when k = 2,i = 1, it only return the first element “[1] 1.0 0.6” , and the second element is missing, it is not the same as the result of featfun(1,1,2).
Further, I rewrite the codes by using python. Following is the python codes:
def featfun(yi_1, yi, i):
all_fea = [
[1,2,2],
[1,2,3],
[1,1,2],
[2,1,3],
[2,1,2],
[2,2,3],
[ 1,1],
[ 2,1],
[ 2,2],
[ 1,2],
[ 1,3],
[ 2,3]]
weights = [1,1,0.6,1,1,0.2,1,0.5,0.5,0.8,0.8,0.5]
idx1 = 999
idx2 = 999
if [yi_1,yi,i] in all_fea:
idx1 = all_fea.index([yi_1, yi, i])
if [yi, i] in all_fea:
idx2 = all_fea.index([yi, i])
if (idx1!=999)&(idx2!=999):
return [[1,weights[idx1]],[1,weights[idx2]]]
elif (idx1!=999)&(idx2==999):
return [1,weights[idx1]]
elif (idx1==999)&(idx2!=999):
return [1,weights[idx2]]
else:
return None
featfun(1,1,2) returns [[1, 0.6], [1, 0.8]].
then I combine python_based featfun with for loops again:
for k in [2,3]:
for i in [1,2]:
return featfun(1,i,k)
following is the return results, the correct result, that is the same as the answer in textbook.
[[1, 0.6], [1, 0.8]]
[[1, 1], [1, 0.5]]
[1, 0.8]
[[1, 1], [1, 0.5]]
what happen with my R codes ? Or it seems that something wrong in R?
I hope someone can help me! Thanks!
Okay, I'm not fully sure why this issue is coming up, but it'a numerical precision issue. When you use seq(1,2) or seq(2,3) they are integers, the all_fea list is numeric, and for some reason (this is unusual) the matching isn't working because of that. If you make the all_fea list items integers, then it works:
all_fea <- list(c(1L, 2L, 2L),
c(1L, 2L, 3L),
c(1L, 1L, 2L),
c(2L, 1L, 3L),
c(2L, 1L, 2L),
c(2L, 2L, 3L),
c( 1L, 1L),
c( 2L, 1L),
c( 2L, 2L),
c( 1L, 2L),
c( 1L, 3L),
c( 2L, 3L))
The above is a manual way. Alternately you could leave it as-is and add the line all_fea = lapply(all_fea, as.integer). Anyway, after that change your loop works as expected.
I want to write a program that gives me the Day of the Year using a dictionary.
import sys
Month = str(sys.argv[1])
Day = int(sys.argv[2])
m = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
Months = {"Jan": 31, "Feb": 28, "Mar": 31, "Apr": 30, "May": 31, "Jun": 30, "Jul": 31, "Aug": 31, "Sep": 30, "Oct": 31, "Nov": 30, "Dec": 31}
a = m.index(Month)
b = m[0:a]
for i in range(len(b)):
c = b[i]
d = Months[c]
e = sum(d) + Day
print(e)
When I run it, it gives me:
File "dayoftheyear.py", line 12, in <module>
e = sum(d) + Day
TypeError: 'int' object is not iterable
Please, help me fix it.
sum function except a iterable (list, tuple, ...) argument. And d is an int.
You could sum all days in a loop like
...
e = 0
for i in range(len(b)):
c = b[i]
d = Months[c]
e = e + d
e = e + Day
print(e)
sum() is designed to work on an iterable object such as a list of numbers. e.g. sum([1, 5, 7]). It does not quite apply in your situation.
The following might help. As an alternative to finding in the index, you could just stop when the correct month is reached:
import sys
user_month = str(sys.argv[1]).title()
user_day = int(sys.argv[2])
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
days_in_month = {"Jan": 31, "Feb": 28, "Mar": 31, "Apr": 30, "May": 31, "Jun": 30, "Jul": 31, "Aug": 31, "Sep": 30, "Oct": 31, "Nov": 30, "Dec": 31}
total = 0
for month in months:
if user_month == month:
break
total += days_in_month[month]
total += user_day
print(total)